# Lab 5: Vegas Blackjack solution

In this lab we will be writing a text-based computer game based upon a card game, casino
blackjack.
Description and Rules of Blackjack
You can find many descriptions of blackjack online. There are several variants of the game.
The rules we will use are similar to those used in most American casinos. Your program must
adhere to the rules as described below.
Your program will have only a single player (the person running the program) and the dealer,
whose actions will be determined by the program. The player is playing against the dealer.
Blackjack is played in rounds. The cards held by the player during a round are called the
player’s hand. The player’s objective in each round is to obtain a hand value higher than the
dealer’s hand value, without exceeding 21. The value of each card is as follows:
Card 2 3 4 5 6 7 8 9 10 Jack Queen King Ace
Value 2 3 4 5 6 7 8 9 10 10 10 10 11
or 1
(Cards also have ‘suits’: Spades, Clubs, Hearts, or Diamonds, but this is irrelevant to this game.)
The hand value is the sum of the card values in the hand. For example, if the player has a hand
consisting of a 6 and a Queen, the player’s hand value is 16. An Ace can have a value of 11 or 1,
depending on which value is better for the person holding the hand. For example, if the player or
dealer has a hand consisting of a 7 and an Ace, the hand value is 18. However, if the player or
dealer has a hand consisting of a 7, an Ace, and a 6, the hand value is 14, because if the Ace’s
value were 11 the hand value would exceed 21, which is undesirable.
A round of blackjack is played as follows. Two cards are given (dealt) to the player and two
cards to the dealer. The player is shown all of the cards in their hand, but only shown one of the
dealer’s cards. If the player’s hand value at this point equals 21, the player has blackjack, and
the player automatically wins the round unless the dealer also has blackjack, which results in a
tie (known as a push). If the player does not have blackjack, the player may choose to ‘hit’ or
‘stand’. Choosing to hit means one card is added to the player’s hand. The player may continue
to hit as many times as they wish, as long as their hand value does not equal or exceed 21. If the
player’s hand value exceeds 21, the player has busted, and automatically loses the round
regardless of the dealer’s hand. When the player no longer wishes to hit, or has a hand value
equal to 21, the player stands, which transfers play to the dealer.
From this point forward, the player is shown all of the dealer’s cards. The dealer’s hand is
similar to the player’s, but the dealer has no choices to make. If the dealer’s hand value is 21
with only two cards, the dealer has blackjack, and the player loses (unless the player also has
blackjack, as described above). If the dealer does not have blackjack, the dealer must continue to
hit as long as the dealer’s hand value is less than 17, and must stand if the dealer’s hand value is
equal to or greater than 17. The dealer’s Aces are always taken to have a value of 11 unless
doing so would cause the dealer’s hand value to exceed 21 (in casino lingo, we require the dealer
to stand on a soft 17). If the dealer’s hand value exceeds 21, the dealer has busted, and the
player wins unless the player has also busted (in which case the player loses).
After both player and dealer are done, the hand values are compared. The winning hand has the
highest value without exceeding 21. If the hand values are equal, the player and dealer push
(tie). As described above, a blackjack (a hand value of 21 with only two cards in the hand) beats
any other hand, including a hand with more than two cards whose value is 21.
The player places a monetary bet before each round of blackjack. If the player wins the round
with a blackjack, they receive money equal to 1.5 times their bet. If the player wins the round
with any hand other than a blackjack, they receive money equal to their bet. If the player pushes
with the dealer, no money is received or lost. If the player loses the round, they lose money
equal to their bet. For this program, the player will start with 300 dollars, and will be allowed to
bet and play rounds until they run out of money or quit the game. Quitting the game will be
indicated by the player placing a bet of 0 dollars. Players should not be allowed to place a bet
larger than the amount of money they have!
We will assume that the hands are dealt from an infinite deck of cards, so that any card is equally
likely to be dealt at any time. (In the real world, some players use ‘card counting’ and exploit
the finite size of the deck to determine the relative probability of certain cards being dealt.
Casinos often ban players suspected of doing this.) When adding a card to a hand, your program
should randomly select a number from 1 to 13, and add the card corresponding to the number
selected.
Extra credit
5 points of extra credit will be awarded for correctly implementing ‘double down’. Doubling
down is a choice a player may make which is similar to asking for a hit, but with the following
conditions:
• doubling down can only be done when the player’s hand has only two cards (right after
dealing and checking for blackjacks)
• the player’s bet on the round is doubled
• a single card is added to the player’s hand and then the player is required to stand (the
player is not allowed to hit more than once after doubling down).
You may choose to implement a more realistic finite-sized card deck from which the hands are
dealt. However, you will not receive extra credit for doing so.
Detailed Lab Programming Requirements
Your code must use the classes ‘Card’, ‘Hand’, and ‘Blackjack’ described in Card.h, Hand.h and
Blackjack.h files provided by the instructor. The instructor will also provide the main.cpp file.
must use all of the above files with no modifications whatsoever, with one exception: you may
make additions to Blackjack.h in order to accomplish the extra credit task, if you choose to do it.
However, you are welcome to modify these files if desired during the coding and debugging
process. You must write three files: Card.cpp, Hand.cpp, and Blackjack.cpp, which will contain
all of the member function implementations that you’ve written.
My suggestion is that you perform tasks in the following order:
1. Write implementations for the Card member functions, and write a small main-card.cpp
file to test this class alone. Do not proceed to step 2 until Card class is working.
2. Write implementations for the Hand member functions, and write a small main-hand.cpp
file to test class Hand and Card.
3. Finally, write implementations for the Blackjack member functions.
Turnin Procedures
You have the choice to use any ONE of the following submission methods for this lab:
1. You may upload your source code to the deepthought cluster and submit it using the
turnin script, as you did for Lab 0. You MUST ensure that your code compiles and runs
properly on deepthought if you choose this method.
Depending on your section, from your home directory enter one of the following at the
command prompt.
turnin-ece2036a Lab5
or
turnin-ece2036b Lab5
This automatically copies everything in your Lab5 directory to a place that we can access (and
2. You may bring your laptop to the TA’s office hours before the due date and time and
your code compiles and runs properly. The demonstrations will be conducted on a firstcome,
first-served basis during the TA’s posted office hours. If the TA’s office hours end
while you are still waiting in line, you must either attend another TA office hour before
the due date/time to try again, or use submission method 1.
If a student’s program runs correctly and produces the desired output, the student has the
potential to get a 100 on his or her lab; however, TA’s will randomly look through this set of
“perfect-output” programs to look for other elements of meeting the lab requirements. The table
below shows typical deductions that could occur.
In addition, if a student’s code does not compile, then he or she will have an automatic 30%
deduction on the lab. Code that compiles, but does not match the sample output can incur a
deduction from 10% to 30% depending on how poorly the output matches the output specified
by the lab. This is in addition to the other deductions listed below or due to the student not
attempting the entire assignment.
Element Percentage
Deduction
Details
Does Not Compile 30% Program does not compile on deepthought cluster
Does Not Match
Output
10%-30% The program compiles but doesn’t match all output
exactly
PROGRAMS
Element Percentage
Deduction
Details
Implement all Card
member functions
10% Does not fully implement all Card member functions
described in the provided Card.h file.
Implement all Hand
member functions and
10% Does not fully implement all member functions and
operator overloads described in the provided Hand.h
file.
Element Percentage
Deduction
Details
Implement all
Blackjack member
functions
10% Does fully implement all member functions described in
the provided Blackjack.h file.
Dynamic allocation 10% Does not dynamically allocate and delete new Cards
correctly.
Clear SelfDocumenting
Coding
Styles
5%-15% This can include incorrect indentation, using unclear
variable names, unclear comments, or compiling with
warnings.
LATE POLICY
Element Percentage
Deduction
Details
First Day 20% By Monday after the due date
Each Additional Day 20% per day The weekend (Sat/Sun) will count as one day
Sample output from completed code
bash-3.2\$ ./a.out
Player starts with 300 dollars
Round 1
Enter bet in dollars, 0 to quit: 20
Dealer's first card is: Queen
Player's hand is: Six Jack
Player's hand value is 16
What would you like to do? Enter (h)it or (s)tand: h
Player's hand is: Six Jack Four
Player's hand value is 20
What would you like to do? Enter (h)it or (s)tand: s
Dealer's hand is: Queen Five
Dealer's hand value is 15
Dealer's hand is: Queen Five Queen
Dealer's hand value is 25
Dealer's hand is bust. Player wins!
Player has 320 dollars
Round 2
Enter bet in dollars, 0 to quit: 30
Dealer's first card is: Eight
Player's hand is: Six Ten
Player's hand value is 16
What would you like to do? Enter (h)it or (s)tand: h
Player's hand is: Six Ten Jack
Player's hand value is 26
Dealer's hand is: Eight Four
Dealer's hand value is 12
Dealer's hand is: Eight Four Jack
Dealer's hand value is 22
Player's hand is bust.
Player has 290 dollars
Round 3
Enter bet in dollars, 0 to quit: 300
You don't have that much money! Enter a lower bet.
Enter bet in dollars, 0 to quit: 50
Dealer's first card is: Queen
Player's hand is: Nine Ace
Player's hand value is 20
What would you like to do? Enter (h)it or (s)tand: h
Player's hand is: Nine Ace Five
Player's hand value is 15
What would you like to do? Enter (h)it or (s)tand: h
Player's hand is: Nine Ace Five Seven
Player's hand value is 22
Dealer's hand is: Queen Four
Dealer's hand value is 14
Dealer's hand is: Queen Four Queen
Dealer's hand value is 24
Player's hand is bust.
Player has 240 dollars
Round 4
Enter bet in dollars, 0 to quit: 100
Dealer's first card is: Six
Player's hand is: Five King
Player's hand value is 15
What would you like to do? Enter (h)it or (s)tand: h
Player's hand is: Five King Five
Player's hand value is 20
What would you like to do? Enter (h)it or (s)tand: s
Dealer's hand is: Six Six
Dealer's hand value is 12
Dealer's hand is: Six Six King
Dealer's hand value is 22
Dealer's hand is bust. Player wins!
Player has 340 dollars
Round 5
Enter bet in dollars, 0 to quit: 200
Dealer's first card is: King
Player's hand is: Queen Seven
Player's hand value is 17
What would you like to do? Enter (h)it or (s)tand: s
Dealer's hand is: King Queen
Dealer's hand value is 20
Dealer wins.
Player has 140 dollars
Round 6
Enter bet in dollars, 0 to quit: 100
Dealer's first card is: Six
Player's hand is: Five Eight
Player's hand value is 13
What would you like to do? Enter (h)it or (s)tand:
h
Player's hand is: Five Eight Queen
Player's hand value is 23
Dealer's hand is: Six Nine
Dealer's hand value is 15
Dealer's hand is: Six Nine Four
Dealer's hand value is 19
Player's hand is bust.
Player has 40 dollars
Round 7
Enter bet in dollars, 0 to quit: 50
You don't have that much money! Enter a lower bet.
Enter bet in dollars, 0 to quit: 20
Dealer's first card is: Six
Player's hand is: Four Two
Player's hand value is 6
What would you like to do? Enter (h)it or (s)tand: h
Player's hand is: Four Two Eight
Player's hand value is 14
What would you like to do? Enter (h)it or (s)tand: h
Player's hand is: Four Two Eight Queen
Player's hand value is 24
Dealer's hand is: Six Three
Dealer's hand value is 9
Dealer's hand is: Six Three Four
Dealer's hand value is 13
Dealer's hand is: Six Three Four Four
Dealer's hand value is 17
Player's hand is bust.
Player has 20 dollars
Round 8
Enter bet in dollars, 0 to quit: 0