| CSCI 240 | Spring 2017 |
For this assignment, write a modified/simplified version of the game Twenty-One. This will be done by re-using the Card class from Program 8 and using a pre-defined class named DeckOfCards.
The concept of the simplified version of Twenty-One is, well, simple. It's a two-player game that uses a deck of 52 cards (no jokers). Each player will draw at most 3 cards in an attempt to get a total of 21 or less. If the player draws a total of 21, they'll be awarded 15 points. If the player draws a total less than 21, they'll be awarded 10 points. A total greater than 21 means the player busted and will not get any points. Once a player has completed their turn, the other player will take their turn. This process will continue until the deck of cards is empty (ie. all of the cards have been drawn from the deck and there are no more cards).
After all of the cards have been drawn, display the score for each player and declare a winner for the game. If the two players have the same score, declare the game a draw.
Note 1: It is possible for a player to draw less than 3 cards. This can occur if a player draws a total of 21 before drawing 3 cards or if the all of the cards are drawn from the deck.
Note 2: For this game, an ace will always have a value of 11, the face cards (Jack, Queen, and King) will all have a value of 10, and the remaining cards will have a value equal to their face value.
This class has been pre-written and will be used to implement the game. The code can be found at: http://faculty.cs.niu.edu/~byrnes/csci240/pgms/assign10.cpp
Add the code for the Card class where indicated in the CPP file.
The DeckOfCards class is used to simulate a deck of 52 playing cards. It contains the following data members:
MAX_CARDS an integer symbolic constant that represents the maximum number of cards in a deck of cards. Its value is 52.
NUM_SUITS an integer symbolic constant that represents the number of different suits for the cards in the deck. Its value is 4.
CARDS_PER_SUIT an integer symbolic constant that represents the number of different cards for each suit in the deck. Its value is 13.
deck an array of 52 Card objects. It represents the actual deck of cards.
topCard an integer that represents the subscript of the card that is on the top of the deck. It's initial value is -1 to represent that no cards have been removed from the deck.
The constructor and methods for the DeckOfCards class are as follows:
DeckOfCards(): the class has a single constructor that takes no arguments. It initializes all 52 elements of the deck array to the 52 cards in a standard deck of playing cards. It also shuffles the deck and initializes the topCard data member to -1 to indicate that no cards have been removed from the deck.
Card draw(): this method draws a card from the top of the deck. It takes no arguments and returns a Card object: the card that is drawn from the deck.
void shuffle(): this method shuffles all 52 cards in the deck. It takes no arguments and returns nothing.
bool isEmpty(): this method determines if all of the cards have been drawn from the deck. It takes no arguments and returns a boolean value: true if the deck is empty (no more cards to draw) or false is the deck is not empty (there are still cards that can be drawn)
Set the seed value that is used by the random number generator by using the srand() function. Pass the value 0 to the srand function.
Create a DeckOfCards object that will used for the game.
Create a Card object. This will be used to hold the card that is drawn from the deck while the game is being played.
Create and initialize at least 4 integers. They will be used to hold the number of the player who is currently drawing cards, the sum of the cards that the player has drawn, player 1's score, and player 2's score.
Write a loop that will execute as long as the deck of cards is not empty. Inside of the loop:
Initialize the sum of the hand that the player has drawn to 0
Write a loop that will execute 3 times and as long as the deck of cards is not empty and as long as the sum of the cards that the player has drawn is less than 21. Inside of the loop:
Draw a card from the deck of cards
Display the card that was drawn
Increment the sum of the cards that the player has drawn based on the card that was drawn.
Display the sum of the cards that the player has drawn
If the sum of the cards that the player has drawn is greater than 21, display a message telling the player that they've busted/lost. Otherwise, the player has won some points (either 10 or 15). Display a congratulatory message that includes the player number and the number of points that they won. Also, increment the appropriate players score.
Change the number of the player who is currently drawing cards to the other player. (Hint: think about using a decision statement.)
Once the deck of cards is empty, display the scores for both players and declare a winner. If the scores are equal, declare the game a "draw".
Each method that is required must have a documentation box like a function.
Hand in a copy of the source code using Blackboard.
You may use any value in the srand() call, but make sure that 0 is used in the version that is handed in for grading.
As a reminder, for those of using a Mac, the output will probably not match this output completely.
Player 1:
2 of Diamonds Total: 2
5 of Hearts Total: 7
9 of Clubs Total: 16
Congratulations player 1! You've won 10 points!
--------------------------------
Player 2:
Ace of Hearts Total: 11
10 of Hearts Total: 21
Congratulations player 2! You've won 15 points!
--------------------------------
Player 1:
6 of Diamonds Total: 6
7 of Diamonds Total: 13
Ace of Spades Total: 24
Sorry player 1 -- you've busted!
--------------------------------
Player 2:
3 of Clubs Total: 3
Queen of Diamonds Total: 13
Queen of Spades Total: 23
Sorry player 2 -- you've busted!
--------------------------------
Player 1:
Queen of Clubs Total: 10
5 of Spades Total: 15
Jack of Spades Total: 25
Sorry player 1 -- you've busted!
--------------------------------
Player 2:
3 of Spades Total: 3
King of Spades Total: 13
6 of Hearts Total: 19
Congratulations player 2! You've won 10 points!
--------------------------------
Player 1:
Ace of Diamonds Total: 11
10 of Clubs Total: 21
Congratulations player 1! You've won 15 points!
--------------------------------
Player 2:
2 of Hearts Total: 2
King of Hearts Total: 12
Ace of Clubs Total: 23
Sorry player 2 -- you've busted!
--------------------------------
Player 1:
2 of Spades Total: 2
5 of Diamonds Total: 7
9 of Hearts Total: 16
Congratulations player 1! You've won 10 points!
--------------------------------
Player 2:
8 of Clubs Total: 8
King of Diamonds Total: 18
6 of Spades Total: 24
Sorry player 2 -- you've busted!
--------------------------------
Player 1:
6 of Clubs Total: 6
4 of Clubs Total: 10
4 of Spades Total: 14
Congratulations player 1! You've won 10 points!
--------------------------------
Player 2:
7 of Spades Total: 7
8 of Diamonds Total: 15
3 of Hearts Total: 18
Congratulations player 2! You've won 10 points!
--------------------------------
Player 1:
4 of Hearts Total: 4
9 of Diamonds Total: 13
10 of Diamonds Total: 23
Sorry player 1 -- you've busted!
--------------------------------
Player 2:
4 of Diamonds Total: 4
King of Clubs Total: 14
7 of Clubs Total: 21
Congratulations player 2! You've won 15 points!
--------------------------------
Player 1:
10 of Spades Total: 10
3 of Diamonds Total: 13
Queen of Hearts Total: 23
Sorry player 1 -- you've busted!
--------------------------------
Player 2:
9 of Spades Total: 9
5 of Clubs Total: 14
Jack of Hearts Total: 24
Sorry player 2 -- you've busted!
--------------------------------
Player 1:
7 of Hearts Total: 7
Jack of Clubs Total: 17
8 of Spades Total: 25
Sorry player 1 -- you've busted!
--------------------------------
Player 2:
2 of Clubs Total: 2
Jack of Diamonds Total: 12
8 of Hearts Total: 20
Congratulations player 2! You've won 10 points!
--------------------------------
Player 1 score: 45
Player 2 score: 60
Player 2 won!
For up to 5 points of extra credit, add code that will make the game more personal by using player names rather than "Player 1" and "Player 2".
At the start of the game, ask each player their name and store them in separate character arrays. Use a length of 20 characters for each array. You may assume that only first names will be entered and that the names will not have spaces - this means that the standard input operator (>>) may still be used for input.
Throughout the rest of the code, substitute the player names in place of "Player 1" and "Player 2" throughout the output.
Note about extra credit: the points will ONLY be awarded if the required portions of the assignment work correctly. In other words, don't take short cuts in the rest of the program because it is assumed that 5 extra points will be awarded.
This is just an example. The names should be able to change each time the program is run.
Player 1, what is your name? Frank
Player 2, what is your name? Jamie
Frank:
2 of Diamonds Total: 2
5 of Hearts Total: 7
9 of Clubs Total: 16
Congratulations Frank! You've won 10 points!
--------------------------------
Jamie:
Ace of Hearts Total: 11
10 of Hearts Total: 21
Congratulations Jamie! You've won 15 points!
--------------------------------
Frank:
6 of Diamonds Total: 6
7 of Diamonds Total: 13
Ace of Spades Total: 24
Sorry Frank -- you've busted!
--------------------------------
Jamie:
3 of Clubs Total: 3
Queen of Diamonds Total: 13
Queen of Spades Total: 23
Sorry Jamie -- you've busted!
--------------------------------
Frank:
Queen of Clubs Total: 10
5 of Spades Total: 15
Jack of Spades Total: 25
Sorry Frank -- you've busted!
--------------------------------
Jamie:
3 of Spades Total: 3
King of Spades Total: 13
6 of Hearts Total: 19
Congratulations Jamie! You've won 10 points!
--------------------------------
Frank:
Ace of Diamonds Total: 11
10 of Clubs Total: 21
Congratulations Frank! You've won 15 points!
--------------------------------
Jamie:
2 of Hearts Total: 2
King of Hearts Total: 12
Ace of Clubs Total: 23
Sorry Jamie -- you've busted!
--------------------------------
Frank:
2 of Spades Total: 2
5 of Diamonds Total: 7
9 of Hearts Total: 16
Congratulations Frank! You've won 10 points!
--------------------------------
Jamie:
8 of Clubs Total: 8
King of Diamonds Total: 18
6 of Spades Total: 24
Sorry Jamie -- you've busted!
--------------------------------
Frank:
6 of Clubs Total: 6
4 of Clubs Total: 10
4 of Spades Total: 14
Congratulations Frank! You've won 10 points!
--------------------------------
Jamie:
7 of Spades Total: 7
8 of Diamonds Total: 15
3 of Hearts Total: 18
Congratulations Jamie! You've won 10 points!
--------------------------------
Frank:
4 of Hearts Total: 4
9 of Diamonds Total: 13
10 of Diamonds Total: 23
Sorry Frank -- you've busted!
--------------------------------
Jamie:
4 of Diamonds Total: 4
King of Clubs Total: 14
7 of Clubs Total: 21
Congratulations Jamie! You've won 15 points!
--------------------------------
Frank:
10 of Spades Total: 10
3 of Diamonds Total: 13
Queen of Hearts Total: 23
Sorry Frank -- you've busted!
--------------------------------
Jamie:
9 of Spades Total: 9
5 of Clubs Total: 14
Jack of Hearts Total: 24
Sorry Jamie -- you've busted!
--------------------------------
Frank:
7 of Hearts Total: 7
Jack of Clubs Total: 17
8 of Spades Total: 25
Sorry Frank -- you've busted!
--------------------------------
Jamie:
2 of Clubs Total: 2
Jack of Diamonds Total: 12
8 of Hearts Total: 20
Congratulations Jamie! You've won 10 points!
--------------------------------
Frank score: 45
Jamie score: 60
Jamie won!