Steve’s Blog

Just another compsci weblog

Blackjack Optimal Strategy Calculator

So during my trip to Las Vegas for Black Hat 2008, I played my first game of blackjack at a casino. I had turned on the TV in my room earlier that day and happened to see a program that was explaining basic strategy in blackjack. The basic strategy is this: since ten is the most common card value that will come up, assume that every card you don’t see has a value of ten.

This is pretty simple and I was getting the hang of playing – even getting help from the dealer when I made an incorrect move. However, one situation that kept coming up was when I was delt sixteen and the dealer was showing a ten. All week I was wondering what the correct move is for this scenario, so I decided to develop a program to tell me. I didn’t get around to writing it until the plane ride home, and even then I had to rewrite it twice before it started producing the correct output, but I’m pretty sure it’s correct as it is almost identical to the optimal playing strategy found on Wikipedia and other websites.

The method behind it is actually very simple – there are six main functions: player-hit, player-stand, player-turn, dealer-hit, dealer-stand, dealer-turn.

The player-hit function returns the probability of the player winning if the player hits on the current hand. It does this by calling player-turn for each possible card that could be delt to the player and averaging the results.

The player-turn function returns the probability of the player winning in his current situation. First it evaluates whether the player is over 21 – if so, it returns zero indicating that the player loses. Otherwise, it finds the probabilities of winning if the player should hit and if the player should stand, and returns whichever is higher.

The player-stand function returns the probability of the player winning if the player accepts no more cards for this game. All it really does is call dealer-turn.

The dealer-hit function works exactly like the player-hit function.

The dealer-turn function works similarly to the player-turn function, except that the dealer has strict rules as to whether they can hit or stand – if the dealer is 16 or under, or has 17 with an ace, the dealer must hit. Otherwise, the dealer stands.

The dealer-stand function checks to make sure the dealer isn’t over 21, then it tests to see who has a higher hand value. The function returns 1.0 if the player has a higher hand, 0.5 for a draw, and 0.0 if the dealer has a higher hand.

The value of each hand is stored in two numbers: one number is the sum of all the cards in the hand (with aces holding a value of 11), and the other is the number of aces in the hand. This is done so that some aces can be counted as having a value of one if the hand goes over 21.

And that’s basically it. You can use the probability-table function to produce a table of every starting blackjack scenario which will tell you the optimal move – hit or stand. Note that the table function takes a long time to run. Also, the program assumes an infinite deck, which obviously won’t be the case for any real situation.

Blackjack Optimal Strategy Calculator

4 comments

4 Comments so far

  1. Jake Voytko August 14th, 2008 6:15 pm

    And if the player is under 21, s/he gets served drinks even though they shouldn’t get them.

  2. frank August 14th, 2008 9:25 pm

    “And if the player is under 21, s/he gets served drinks even though they shouldn’t get them.” => if you live in the US, in sane countries you are considered adult over 18, when you actually are.

    Nice blog though!

  3. kme August 15th, 2008 4:29 am

    The full basic strategy also needs to take into account when to double-down or split – and the fact that blackjack pays 3 to 2.

    Also note that the dealer hitting on soft 17 isn’t always the case (“traditional” rules have the dealer stand on soft 17, and this works out better for the player).

  4. slobodan blazeski June 9th, 2009 12:26 pm

    Thanks for the cool little program. May I ask why do you put closing parens on separate line?

Leave a reply