Blackjack Extensions#
If you’ve completed the Blackjack exercise but are interested in getting more practice, here are two extensions you can try.
Aces#
One major simplification we applied to Blackjack was to assume the game did not use any aces. Aces are complicated because their value can be either 11 or 1, depending on the value of the other cards in a player’s hand.
If a player has a hand with an Ace in it, the card will be worth 11 unless treating the Ace as 11 results in the player busting (going over 21), in which case the Ace is valued as 1. So in a hand that has a 10, a 7, and an Ace, the hand will be valued at 18. But in a hand with a 10 and an Ace, the hand is valued at 21.
The value of an ace is also not fixed within a game. If a player has an Ace in their first two cards, it will always be valued at 11 initially (you can’t bust with two cards). So a hand with a 7 and an Ace is worth 18. But if the player then hits and gets a 7, then the Ace becomes a 1 and the hand is worth 7 + 7 + 1 = 15.
Here’s a couple test cases you can use to validate your results (that assume you’re using the string "A"
for aces):
print("\nTest Case 7: Player gets blackjack (21), wins.")
players_deck = [10, "A", 6]
dealers_deck = [10, 8, 7]
result = play_blackjack(players_deck, dealers_deck)
print("The winner should be Player.")
print(f"Winner from the function was {result}")
print("\nTest Case 8: Player gets to 21.")
players_deck = [5, "A", 8, 7]
dealers_deck = [10, 8, 7]
result = play_blackjack(players_deck, dealers_deck)
print("The winner should be Player.")
print(f"Winner from the function was {result}")
print("\nTest Case 8: Dealer gets to 21.")
players_deck = [10, 8, 7]
dealers_deck = [5, "A", 8, 7]
result = play_blackjack(players_deck, dealers_deck)
print("The winner should be Dealer.")
print(f"Winner from the function was {result}")
Monte Carlo Simulation#
The second extension to Blackjack is to try simulating lots of games of Blackjack to figure out how often the Player is likely to win given the decision rule we chose (hit if less than or equal to 16).
This is an example of what’s called a Monte Carlo Simulation. Monte Carlo simulations are a commonly used tool for estimating conditional probabilities empirically in situations where it would be really difficult to determine them analytically (i.e., by writing out “probability of outcome x if y” times “probability of y if z”, etc.). Basically, you look for something that’s easy to simulate (like creating random decks of cards), then you do that thousands of times and play your games of blackjack with the simulated decks. If you then look at how often Player or Dealer wins, then assuming your programmed the game correctly and simulated decks of cards correctly, then by the law of large numbers your empirical distribution of outcomes will be a good estimate of the “true” likelihood of Player or Dealer winning given the strategies they use.
Monte Carlo simulations are used in finance, sports, engineering, forecasting, climate modelling, politics, and much more.
Simulating Decks of Cards#
A deck of cards has 13 unique card values (2-10, Jack, Queen, King, Ace), each in four suits. Since we don’t care about suits in this class, you can simulate a deck of cards by randomly drawing integers with uniform (equal) probabilities between 2 and 14. Then, since face cards are all worth ten, you can convert values of 11, 12, 13 (the Jacks, Queens, and Kings) to 10 (their value in Blackjack). You can then either drop values of 14 (if you aren’t using Aces), or treat 14 as an Ace (if you did the first extension).
To generate random cards, use the randint
function from the random
library:
import random
card = random.randint(2, 14)
print(card)
6
If you play without aces, and stick to the rules from the original exercises, then you should find the Player wins about 40% of the time.
So to do ONE simulation, you’d need to draw enough cards to make a deck for each player, play the game, and save the result.
Then you’d want to do that a few thousand times.
An Extension on the Extension#
Want to get even more fancy? Change the decision rule the Player uses (so pick a new threshold other than 16) and see what that does to their win percentage.