Introduction
Requirement
CS 8: Introduction to Computer Programming with Python
Spring 2016
Project 2
Assigned: Wednesday, February 24 Due: Tuesday, March 29 11:59 PM
Overview
Blackjack, or 21, is the most widely-played casino banking game in the world. The goal is
to build a hand of cards that values as close to 21 as possible without exceeding it. First,
here is the terminology used in the game.
Hit: Take another card for your hand
Stand: Stop taking cards
Bust: Exceed a value of 21 in your hand
Push: Tie with the dealer
The cards are valued as follows.
Rank Value
Ace 11 or 1
2–10 Face value
Jack, Queen, King 10
Of particular note, an Ace can be considered either value 11 or 1. Given rational strategy,
it will be considered 11 unless that will cause a bust, in which case it will be considered 1.
In each hand, play proceeds as follows.
1. The dealer takes one card, which the player can see.
2. The player is given two cards.
3. The player has the option to hit, or take one additional card. The player can hit as
many times as desired, until they reach or exceed 21, or decide to stand (stop taking
cards).
1
4. If the player did not bust (exceed 21), the dealer takes cards until she reaches 17 or
higher (i.e., the dealer will hit on 16 or fewer points, and stand on 17 or more points).
5. If the player busts, she immediately loses, and the dealer will not draw more cards.
If the dealer busts, the player wins. If the player and dealer have the same total, it is
a push. Otherwise, the winner is the one with higher point total.
6. If the player loses, she loses her bet. If the player wins, she wins her bet. If the hand
is a push, the bet is returned (i.e., no gain or loss).
In this project, you will program a functional Blackjack game by first coding the basic logic
of the game, then gradually adding features.
2
Lab Session 1, Feb 24–Feb 25
Activity 1
In this activity, you will code the logic of the game using simplifications, rather than tackling
a full Blackjack game all at once. Instead of selecting a card and then determining its value,
you will instead randomly generate a card’s value from between 2 and 11. You will also
assume that the bet is always $25.
Note that randomly generating a card’s value is not an accurate way to simulate draws. In a
real deck, there are more cards worth 10 than there are, e.g., cards worth 5. Consider other
ways in which this version of Blackjack will not be exactly the same as real-life Blackjack.
You will add features to make the game more realistic in later activities.
1. Write a main() function that asks the user for their name, and initializes their starting
money to $1,000.
2. Write a loop in main() that will play a hand of blackjack, then ask the user if they want
to play another hand (as long as they still have money to bet). You will play a hand
by calling the play_hand(name) function (which does not yet exist). This function
should return the change in the player’s money (positive if they win, negative if they
lose), and you should use this value to update their amount after each hand.
3. Write the play_hand(name) function. In this function, you will need to track the
running total of the cards that the dealer and player have been dealt. Deal one card
to the dealer and two to the player. Prompt the player for their action (hit/stay)
until they reach or exceed 21, or choose to stay. Then, deal cards to the dealer until
they reach or exceed 17. Determine the winner, and return the change in the player’s
money (i.e., 25 if they won, -25 if they lost, and 0 if the hand was a push).
Here is an example run after completing this stage: