Lab 5: Blackjack
Overview
In this lab, you will use a provided Card
class to create a simplified game of [Blackjack], where a user draws cards in an attempt to get exactly 21 points. You read a deck of cards in from a input file, and write the game’s results to an output file.
-
Please email me a code related question before class on Thurs 6 Dec.
-
Due Fri 7 Dec 2018 at midnight
Instructions
-
Please fork and edit the following project:
-
You may not modify Card.h.
-
Write a card-drawing function:
-
Take a const reference to a vector of cards (
x
) and a reference to an integer position index (ipos
), and return aCard
object (result
). -
Select the element of
x
at indexipos
, incrementipos
, and return the selected card. -
After
ipos
has been incremented, check ifipos
equalsx.size()
. If so, setipos
equal to 0 (i.e., start over in deck).
-
-
Write a hand-scoring function:
-
Take a const reference to a vector of cards (
x
) and a bool (aceHigh
), and return an unsigned integer (i.e., a uint). -
Add up the the points of each element in
x
and return their sum. -
When scoring each card, pass the
aceHigh
parameter to theCard
points()
member function.
-
-
Write a program that follows the instructions below.
-
Ask 2 different people to play your game.
-
Submit your Repl.it URL and Zip file:
-
For this assignment, you do not need to record or submit output from the runs.
Your program should:
-
For each run, count the number of wins, losses, and draws (where a user chooses to stop drawing before reaching 21).
-
Greet the user, ask them for (and store) their name.
-
Create a vectors of Cards (
deck
)-
Read in the file
input_deck.txt
-
Create one card object per line
-
Add each card to
deck
-
-
Contain an outer
while(true)
loop (one loop per hand):-
Inform the user that a new hand has started
-
Create a vector of cards (
hand
) -
Use the card-drawing function to select 2 cards from the deck and add them to the hand
-
Show the user their cards
-
Ask the user if they want to score aces high or low.
-
-
Contain an inner
while(true)
loop:-
With the scoring function, score the hand and print the result
-
If the score exceeds 21, inform the user they’ve lost this hand and
break
-
If the score is exactly 21, inform the user they’ve won and
break
-
Otherwise, ask the user if they want to draw another card.
-
If yes, draw another card from the deck and
continue
-
Otherwise,
break
-
-
-
At the end of the outer loop, ask the user if they want to continue playing
-
If so,
continue
-
Otherwise,
break
-
-
At the end of the program, output the user’s name, total hands played, wins, losses, and draws to the
results.txt
file.-
This output should be appended to the file.
-