Today I completed a small python code that was really fun to work on. It’s not really a data analysis code per se. But it’s a code that allowed me to challenge my coding skills: creating a blackjack game!
I started this code during the Python course on Kaggle and made it into a proper game afterwards.
The Assignment: A Blackjack Function
The assignment was to write a simple function. Given two hands of cards, it would return which one was winning according to the blackjack rules.
The tricky part was to take in account the fact that in the blackjack rules, aces can have two different values: 1 and 11. It would change the outcome of winning hands, and therefore needed to be taken in account.
The result of this function allowed me to check which hand was the winner. But the assignment only limited to testing the function for a few combinations of hands.
Therefore, I decided to add a fun element to it: interactivity!
The idea was to allow the player to “hit” or “stand” after the initial dealing of the first two cards. Both player’s and dealer’s hand are shown in the prompt to help the player make a decision.
To generate the cards, I created a dictionary containing all cards. And I used the “random” library to choose cards for the hands.
After the initial deal, the user is prompted to type “hit” if they want another card or “stand” if they don’t. Both player’s and dealer’s hands are stored in lists that will be passed on to the blackjack_hand_greater_than() function created earlier to determine who wins.
The Dealer’s Rules
While the player is entirely free to add as many cards as they want to their hand, the dealer doesn’t have the same luxury. According to the Bicycle Cards website, here are the rules for the dealer:
When the dealer has served every player, the dealers face-down card is turned up. If the total is 17 or more, it must stand. If the total is 16 or under, they must take a card. The dealer must continue to take cards until the total is 17 or more, at which point the dealer must stand. If the dealer has an ace, and counting it as 11 would bring the total to 17 or more (but not over 21), the dealer must count the ace as 11 and stand. The dealer’s decisions, then, are automatic on all plays, whereas the player always has the option of taking one or more cards.
So I wrote a function which would return True is the dealer should hit and False if they should stand. Every time the player is prompted to make a choice, they would see what are their current hand and the dealer’s hand.
When the player decides to stand, the code calculates final hands for both the player and the dealer, as well as their respective totals. It uses the original function blackjack_hand_greater_than() to return the winner of the round.
Note: there are many different rules of blackjack (according to Wikipedia, there are over 100 variations!). This code is based on the simplest and most used rules.
Using this code, it would be relatively simple to implement a “bet” function that would allow the user to bet money on each hand. It would also be interesting to call this function a large number of times and extrapolate data from it. For example, we could calculate the probability of winning given an initial hand. The possibilities are infinite!