Python and Probability: Simulating Blackjack Card Counting with Python Code

Python has widespread applications in this technological era. From being used in developing Machine Learning and Data Science applications to developing and accentuating the features of almost every popular game, Python does it all.

One such popular card game is Blackjack. For those who enjoy card games like poker and casinos, blackjack is nothing new. For those unfamiliar, blackjack is a card game you can play against a dealer. That’s right! In this game, you play against the dealer of the house (usually the Casino). The idea is simple: the dealer has a deck of cards, and he distributes two cards each to the players and himself.

In order to win this game, the player should have a total of 21 cards in his hand (the hand value). The player should have a hand value of 21 before the dealer, which is also higher than the dealer’s hand value. He can also win if the initial two cards sum up to 21 (a 10 card and an Ace). Otherwise, the player loses. (In game terminology, the player busts.)

As someone said, knowing this game makes you familiar with all the other card games. There is much more to this exciting game.

Let us understand the rules of this game, shall we?

Basics of Blackjack

We all know the basics of card games. To reiterate, the cards with numbers (2 through 10) are called numeric cards. We calculate the sum of the numbers on these cards.

Then we have the face cards (the Ace, King, Queen, and Jack). The King, Queen, and Jack have a value of 10, and the Ace has a value of 1 or 11, depending on the house.

The sum of these cards must be exactly 21 for the player. In such cases, the player wins. If the hand value of the player is not 21, the player must at least have a value higher than the dealer’s at hand.

Luckily for the beginners, we have a detailed post on the game Blackjack.

Simulating Blackjack Card Counting With Python

Card Counting means nothing more than guessing what or which cards could be in the shoe. There are a few techniques to guess the probable cards remaining. Many people who are experts in card counting claim that the player is more likely to win when there are cards worth 10 or 11 in the shoe. It reduces the chance of the house winning by 1%.

If we understand how card counting works, it is not a big deal to write Python code for it. To know how to write your Python code for simulating blackjack card counting, start by understanding the basics of the blackjack game and why counting cards can be helpful. To understand how this strategy works, it’s a good idea to do a little research and try a blackjack site yourself. You can find trusted casino platforms you can safely test in expert reviews, which saves you time when you want to try out your card-counting skills.

Let us see a simple implementation of card counting that can be used to train the players to execute perfect card counting.

```num_on_cards= {
'2': 1,
'3': 1,
'4': 1,
'5': 1,
'6': 1,
'7': 0,
'8': 0,
'9': 0,
'0': -1,
'J': -1,
'Q': -1,
'K': -1,
'A': -1
}
deck = 8
def main():
while True:
inp = input('Enter a card or "exit" to quit:')
if inp.lower() == 'exit':
break
elif not inp:
continue
cards = len(inp)
count = 0
for card in inp:
count += num_on_cards.get(card.upper(), 0)
played = cards / 52.0
truecount = count / (deck - played)
print('Count: {}'.format(count))
print('True Count: {}'.format(truecount))
if __name__ == '__main__':
main()
```

We need to store the hi-lo representation of the cards somewhere, so we create a dictionary called num_on_cards. The number of decks is set to 8.

Inside the main function, we are trying to get input from the user. If the input is ‘`exit`‘, the code terminates. Otherwise, it is an infinite loop trying to get the user’s input. Then the length of the input we provide is stored in cards. The number we provide is checked with the keys in the dictionary. The number of cards already played is calculated by dividing the input by 52. Lastly, the true count is printed. The true count can be useful in changing the strategy.

Summary

Blackjack is a popular game involving luck and strategy. Having a good knowledge of the cards present in the shoe will give the player a chance to beat the dealer.

In this post on the card-counting simulator, we learned about the basic rules of the game and used Python to create a basic card-counting strategy.