HOMEWORK 07 - SMART BLACKJACK
Concepts
In this homework, we will use an array to create a standard deck of 52 playing cards. Our Blackjack game will become much more realistic now.
Overview
For this assignment, your job is to modify the simple Blackjack game from Homework 06. You may use your prior submission, or our provided solution to Homework06: hw07_startingpoint.cpp
.
The Specifics
In Homework 06, every time we needed a card, we randomly generated one. With the power of arrays, we can do better than that!
Before gameplay starts, create an array of Cards that has a size of 52. Populate the array with all 52 card values (Ace of Hearts through King of Spades).
Modify (or create) your PrintCard()
function so that it will print "Ace", "Jack", "Queen", or "King" in place of 1, 11, 12, and 13 respectively.
At this point, print all cards in your array to ensure you have all 52 cards represented. Once you are satisfied all 52 cards are there, comment out these print statements.
Now, for the most important part, create a function called ShuffleDeck
that takes an array of cards and the size of the array as input. This function randomly selects two cards from the deck and swaps them. This operation should be performed enough times to ensure the entire deck is thoroughly shuffled. To verify the cards have been shuffled, again print all cards in your array. If you are happy with the randomness of the card order, comment out these print statements as well.
Ready to implement "real" Blackjack?? (or at least Blackjack simulated in
Visual Studio?) First, remove the RandomCard()
function. Second, create a function called GetNextCard
that takes the array of cards, the size of the array, and a currentCardIndex
(integer) as input. The function should return a Card as output. The currentCardIndex
keeps track of our place in the deck and the card we should deal next. Naturally, it's initial value should correspond to the "top" of the deck, and the function should return the card at the current position in the deck (i.e., the "top" card in the deck). The function then needs to advance our current index; that is, since we've now "dealt" this card, we don't want to deal it again!
One issue you may have pondered ... what happens if you play
several rounds and the deck runs out of
cards? There are issues with re-shuffling the deck since we are not tracking the
cards currently in play. Thus, instead, we sugget you add code
to re-shuffle the deck if the deck is
"low" on cards. Specifically, when a user requests another game of Blackjack,
re-shuffle the deck
if the currentCardIndex
indicates there are less than
20 cards in the deck.
The final change is to replace every instance of RandomCard()
with GetNextCard()
. And then enjoy your better smarter Blackjack! Just remember, the house always wins. Also note that you know have the starting point to create any card based game (war, poker, go fish, etc.). Hmmm ... could be useful for your final project?
Extra Credit!
For the first extra credit part, allow an Ace to count as 1 or 11. That is, if the user has an Ace, then his/her total should show as 20. If the user chooses to hit and are dealt a 5, then his/her total should become 15. Implementing this correctly will earn you 3 extra points.
For the second extra credit part, if either player is dealt a Blackjack, then the hand should instantly end. Implementing this correctly will earn you 2 extra credit points.
Hints
- You'll want to use a 1D array to store the deck of cards.
- While it is possible to copy and paste a few lines of code (over and over) to generate all 52 cards, it will become quite tedious and you won't enjoy making sure all 52 cards are represented. You will find it much easier to use two nested for loops to accomplish this task.
- zyBooks Section 7.16 will be helpful for your shuffling function.
- If you plan to do the first extra credit, you may want to create a variable
called
currentHand
. This variable could be an array of size X, where X is the largest possible number of cards a given hand could have. It may, however, be smarter to use a vector. (While we have not yet covered vectors in class, zyBooks discusses vectors extensively, i.e., 7.7-7.15). If you have a variable calledcurrentHand
, then an Ace can initially be set as 11 and then later downgraded to a 1. If you do this extra credit, be sure you handle a hand consisting of "A A J 7" (which should total 19).
Submission
You need to submit your solution of this homework (HW07) with four lab assignments: Lab07A, Lab07B, Lab07C, and Lab07D. Detailed generic instructions for submitting homework assignments are available. For homework due this Friday (Mar 11), follow these specific steps:
- create a directory called week07.
- within week07, create five subdirectories: Lab07A, Lab07B, Lab07C, Lab07D, and HW07.
- within your new week07/Lab07A directory, copy in your main.cpp file from your Lab07A solution.
- within your new week07/Lab07B directory, copy in your main.cpp file from your Lab07B solution.
- within your new week07/Lab07C directory, copy in your main.cpp file from your Lab07C solution.
- within your new week07/Lab07D directory, copy in your main.cpp file from your Lab07D solution.
- within your new week07/HW07 directory, copy in your main.cpp file from your HW07 solution.
- compress the week07 directory (see Step 3 here for details).
- submit the week07.zip file to Blackboard (see Steps 5-10 here for details).
- after you submit, download the file and double check it contains all that you think it contains!