DeanWhitehouse Posted May 4, 2009 Share Posted May 4, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/156865-solved-counting-code-probs-pretty-simple/ Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 In the loop, if you get to an ACE, store it into a $count variable. It should be an int counting how many aces are in the hand. Then once you count up the rest of the cards, then determine how to treat an ace. Quote Link to comment https://forums.phpfreaks.com/topic/156865-solved-counting-code-probs-pretty-simple/#findComment-826322 Share on other sites More sharing options...
DeanWhitehouse Posted May 4, 2009 Author Share Posted May 4, 2009 Yeah thought so, i just changed the if statements in the second one to use $current_total for the counting sums and seems to work Quote Link to comment https://forums.phpfreaks.com/topic/156865-solved-counting-code-probs-pretty-simple/#findComment-826332 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.