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

 

<?php
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

 

Thanks,

Blade

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.