by Marcin Moskala

# I used programming to figure out how card counting really works

When I was younger, I loved the movie 21. Great story, acting skills, and obviously this inner dream to win huge and beat the casino. I never learned to count cards, and I’ve never actually played Blackjack. But I always wanted to check if this card counting was a real thing, or just a casino’s decoy splashed on the internet thanks to big money and big dreams.

Today I am programmer. Since I had some extra time between workshop preparations and project development, I decided to finally reveal the truth. So I wrote a minimal program that simulates gameplay with card counting.

How did I do it, and what were the results? Let’s see.

### Model

This is supposed to be a minimal implementation. So minimal that I haven’t even introduced the concept of a card. Cards are represented by the number of points they evaluate to. For example, an Ace is 11 or 1.

The deck is a list of integers, and we can generate it as shown below. Read it as “four 10, number from 2 to 9 and single 11, everything 4 times”:

We define the following function that let’s us multiply the contents of `List`

:

The dealer’s deck is nothing but 6 decks shuffled — in most casinos:

### Card counting

Different card counting techniques suggest different ways to count cards. We will use the most popular one, which evaluates a card as 1 when smaller then 7, -1 for tens and aces, and 0 otherwise.

This is the Kotlin implementation of these rules:

We need to count all used cards. In most casinos, we can see all the cards that were used.

In our implementation, it will be easier for us to count points from cards that are left in the deck and subtract this number from 0. So the implementation can be `0 — this.sumBy { card -> cardValue(card`

) } which is an equivalent `of -this.sumBy { cardValue(it`

) } `o`

ue). This is the sum of points for all used cards.*r -su*mBy(::cardVal

What we are interested in is the so-called “True Count”, which is the number of counted points divided by the number of decks that are left. Normally the player needs to estimate this number.

In our implementation, we can use a much more accurate number and calculate `trueCount`

this way:

#### Betting Strategy

The player always has to decide before the game how much money they bet. Based on this article, I decided to use the rule where the player calculates their betting unit — which is equal to 1/1000 of their money left. Then they calculate the bet as a betting unit times the true count minus 1. I also found out that the bet needs to be between 25 and 1000.

Here is the function:

#### What to do next?

There is one final decision for our player. In every game, player needs to make some actions. To make decisions, the player needs to decide based on the information about their hand and the dealer’s visible card.

We need to represent player and dealer hands somehow. From a mathematical point of view, the hand is nothing else but a list of cards. From the player’s point of view, it is represented by points, the number of unused aces if it can be split, and if it is a blackjack. From the optimization point of view, I prefer to calculate all these properties once and reuse the values, since they are checked over and over again.

So I represented the hand this way:

#### Aces

There is one flaw in this function: what if we pass 21 and we still have an unused Ace? We need to change the Ace from 11 to 1 as long as this is possible. But where should this be done? It could be done in the constructor, but it would be highly misleading if someone set the hand from cards 11 and 11 to have cards 11 and 1.

This behavior should be done in the factory method. After some consideration, this is how I implemented it (there is also plus operator implemented):

Possible decisions are represented as an enumeration (enum):

Time to implement the player’s decision function. There are numerous strategies for that.

I decided to use this one:

I implemented it using the following function. I assumed that folding is not allowed by the casino:

#### Let’s Play!

All we need now is a game simulation. What happens in a game? First, cards are taken and shuffled.

Let’s represent them as a mutable list:

We will need `pop`

functions for it:

We also need to know how much money do we have:

Then we play iteratively until … until when? According to this forum, it is normally until 75% of cards are used. Then cards are shuffled, so we basically start from the beginning.

So we can implement it like that:

The game starts. The casino takes single card:

Other players take cards as well. These are burned cards, but we will burn them later to let the player now include them during the points calculation (burning them now would give player information that is not really accessible at this point).

We also take a card and we make decisions. The problem is that we start as a single player, but we can split cards and attend as 2 players.

Therefore, it is better to represent gameplay as a recursive process:

If we don’t split, the returned value is always a single bet and a final hand.

If we split, the list of two bets and hands will be returned. If we fold, then an empty list is returned.

This is how we should start this function:

After that, the casino dealer needs to play their game. It is much simpler, because they only get a new card when they have less then 17 points. Otherwise he holds.

Then we need to compare our results.

We need to do it for every hand separately:

We can finally burn some cards used by other players. Let’s say that we play with two other people and they use 3 cards on average each:

That’s it! This way the simulation will play the whole dealer’s deck and then it will stop.

At this moment, we can check out if we have more or less money then before:

The simulation is very fast. You can make thousands of simulations in seconds. This way you can easily calculate the average result:

Start with this algorithm and have fun. Here you can play with the code online:

**Blackjack***Kotlin right in the browser.*try.kotlinlang.org

### Results

Sadly my simulated player still loses money. Much less than a standard player, but this counting didn’t help enough. Maybe I missed something. This is not my discipline.

Correct me if I am wrong ;) For now, this whole card-counting looks like a huge scam. Maybe this website just presents a bad algorithm. Although this is the most popular algorithm I found!

These results might explain why even though there have been known card-counting techniques for years — and all these movies were produced (like 21) — casinos around the world still offer Blackjack so happily.

I believe that they know (maybe it is even mathematically proven) that the only way to win with a casino is to not play at all. Like in nearly every other hazard game.

### About the author

Marcin Moskała (@marcinmoskala) is a trainer and consultant, currently concentrating on giving Kotlin in Android and advanced Kotlin workshops (contact form to apply for your team). He is also a speaker, author of articles and a book about Android development in Kotlin.