# [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.

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

Thanks,

##### Share on other sites

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.

##### Share on other sites

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

##### 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.

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.