tags:

views:

374

answers:

5

My kiddo had a homework assignment to write Blackjack in Java. I helped him a little but for the most part he did it all himself and it actually plays pretty well. He even caught an error I didn't see in how it was calculating hand values. However, there is a snag that he hasn't dealt with and every solution I can think of is really complicated and way beyond what he's going to be able to easily code up with his still rudimentary Java skills.

The Ace. In fact, not just one Ace, there's four of them and you could possibly get all four of them in a single hand. How do you elegantly deal with calculating the value of a hand of cards when there's one or more Aces, each of which might be valued at one or eleven. I feel like there should be a graceful algorithm for it, but I'm not seeing it. Of course, part of it could just be that I'm tired, but maybe you can help.

A: 

The problem is that it's not determined: you can count (as I understand the rules) an Ace as either 1 or 11. But you know you're not going to count it as 11 every time, because you'll bust.

The only solution I think is to compute the score for each possible value of the ace where the sum <= 21.

Charlie Martin
+10  A: 

Just treat each ace as 11. Then while the value is over 21, subtract 10 from your total for each ace in your hand.

Unknown
Nice and elegant, other than having to look more than once at the cards. +1
Michael Myers
You don't necessarily have to look more than once. Just keep a count of aces the first time you look.
lc
+2  A: 

No matter what every ace should be counted to sum value as 11, then when the total sum has reached over 21 subtract 10 from hand, but the thing is you must make sure you keep a count of how many times you subtract 10 and how many times you add 11( an ace),

add 11 >= subtract 10 -must always be satisfied

alorithm example:

int sum=0;
int ace=0;
int subtract=0;
while(!busted or !stay)
{
  Hitme(value);
  if(value=11)ace++;
  sum+=value;
  if(sum>21) 
  {
      if(ace>=1)
      {
         if(ace>=subtract)
         {
           sum-=10
           subtract++;
         }
         else
         {
            busted;
         }
      }
      else
      {
          busted;
      }
  }
  else
  {
    hit or stay;
    //of course if sum== 21 then force stay

  }
}
TStamper
+3  A: 

You will only ever use 1 ace for the 11 points. So calculate all but the last ace as 1 and if have 10 or less points, the last ace counts as 10.

Milhous
Imagine a player starting with ace-2. They hit get and ace, hit again get and ace, hit again get and ace. Even with a single deck they've got four! With a multideck shoe it can be worse. Keep a count and discount as needed...
dmckee
+1  A: 

Only 1 Ace ever counts as 11.

So, an alternative method to treating each Ace as 11 would be to treat every Ace as 1. Then add 10 to the total value ( carried regardless of Ace on hand ) and keep that in a separate "high" variable ( value + 10 ). Also create a boolean of ~ ace:true if (any) ace comes up.

And so when checking the score against the dealer; check if the players' hand has (any) Ace, in which case you ( can ) use the "high" value, otherwise ( no Ace ) use the "low" value.

That way King + 9 + Ace ( bad example perhaps ) would be ~ low:20 & high:30 & ace:true - With that information you can check if 30 - 10 will "win the game". So, King + 5 + 5 ( low:20 high:30 ace:false ) will not use it's high value of 30.

I'm using this method so I know when to show an alt. Ace score onscreen; like 3/13 ( Ace + 2 ), using the same ace:true|false boolean I already have. This is surely the same answer as the first one given, but this makes more sense to me :)

elundmark