Jump to content

[SOLVED] Counting Code, probs pretty simple

Recommended Posts

Heya once more,


I wrote a blackjack code a while back as some of you may remember but i am finding a bug in this function


function getTotal($player)

	$current_total = 0;

	$new_total = 0;

	//$ace_total = 0;

	foreach($player as $counting)

		$current_total += $counting['value'];


	foreach($player as $counting)

		if($counting['value'] == 1 && $new_total < 11 && ($new_total  + 11) <= 21)
			$counting['value'] = 11;
		elseif($counting['value'] == 11 && $new_total >= 11 && $new_total + 1 <= 21)
			$counting['value'] = 1;

		$new_total += $counting['value'];


	return $new_total;



The function, will count the value of the cards in your hand. Now the problem, the function counts the cards in the order they are in and the problem with this is that it needs to determine whether an ace is worth 1 or 11 by counting the value of the other cards, see the problem, if it counts the ace first it will make it 11 then that can make a player bust as they then might have a 8 and a 3 with it in which it should count ace as 1.


Ok, hope that made sense, if not please say.


Any ideas how to fix this, cus i thought i had but odviously not




Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.