write a modified/simplified version of the game Twenty-One. use: The Card class
ID: 3830795 • Letter: W
Question
write a modified/simplified version of the game Twenty-One. use:
The Card class is used to represent a single playing card. Use the following class definition: 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.
DeckOfCards class
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)
Implementing the game (suggested logic)
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".
Programming Requirements
Each method that is required must have a documentation box like a function.
Hand in a copy of the source code using Blackboard.
Note
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.
Output
As a reminder, for those of using a Mac, the output will probably not match this output completely.
Explanation / Answer
Here is the code for the question. Sample output is attached. Please don't forget to rate the answer if it helped . Thank you very much.
/***************************************************************
CSCI 240 Program 10 Spring 2017
Programmer:
Section:
Date Due:
Purpose:
***************************************************************/
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
#include <algorithm>
using namespace std;
/********** Put the Card class definition between these lines **********/
class Card
{
private:
static const string FACE[14];
int faceVal;
char suit;
public:
Card();
void setCard(int faceVal_in, char suitVal_in);
int getFaceValue();
int getCardValue();
char getSuit();
string toString();
};
const string Card::FACE[]={"","Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};
/***************************************************************************/
//Definition for a class that represents a deck of cards
class DeckOfCards
{
public:
DeckOfCards();
Card draw();
void shuffle();
bool isEmpty();
private:
static const int MAX_CARDS = 52; //Maximum number of cards in a deck
static const int NUM_SUITS = 4; //Number of card suits
static const int CARDS_PER_SUIT = 13; //Number of cards of each suit
Card deck[MAX_CARDS]; //The deck of cards
int topCard; //The subscript of the card on the top of the deck
};
int main()
{
srand(time(0));
Card card;
int turn, total, scores[2]={0,0};
DeckOfCards deck;
while(!deck.isEmpty())
{
cout<<"Player "<<(turn+1)<<": "<<endl;
total = 0;
for(int i =0; i < 3 && !deck.isEmpty(); i++)
{
card = deck.draw();
total += card.getCardValue();
cout<<card.toString()<<" Total: "<<total<<endl;
if(total == 21)
break;
}
if(total == 21)
{
cout<<"Congratulations Player "<<(turn+1)<<"! You won 15 points"<<endl;
}
else if(total < 21)
{
cout<<"Congratulations Player "<<(turn+1)<<"! You won 10 points"<<endl;
}
else
{
cout<<"Sorry Player "<<(turn+1)<<"! -- You've busted!"<<endl;
}
scores[turn] += total;
cout<<"----------------"<<endl;
turn = 1 - turn;
}
cout<<"Player 1 score: "<<scores[0]<<endl;
cout<<"Player 2 score: "<<scores[1]<<endl;
if(scores[0] > scores[1])
{
cout<<"Player 1 wins."<<endl;
}
else if(scores[0] < scores[1])
{
cout<<"Player 2 wins."<<endl;
}
else
{
cout<<"Its a draw!"<<endl;
}
return 0;
}
/********** Code the Card class methods between these lines **********/
//constructor
Card::Card()
{
faceVal = 0;
suit = ' ';
}
//set the facevalue and suit for the card
void Card::setCard(int faceVal_in, char suitVal_in)
{
faceVal = faceVal_in;
suit = suitVal_in;
}
//get facevalue
int Card::getFaceValue()
{
return faceVal;
}
char Card::getSuit()
{
return suit;
}
//returns the value of this card for the twenty one game. if Ace the value is 11 and for jack, queen , king, its 10
//Other cards return their facevalue
int Card::getCardValue()
{
if(faceVal == 1) //Ace
return 11;
else if(faceVal >=11 && faceVal <= 13) //jack, queen, king
return 10;
else
return faceVal;
}
//return the string representation of the card
string Card::toString()
{
string val = FACE[faceVal]+" of ";
if(suit == 'C')
val += "Clubs";
else if(suit == 'D')
val += "Diamonds";
else if(suit == 'H')
val += "Hearts";
else if(suit == 'S')
val += "Spades";
return val;
}
/*************************************************************************/
/***************************************************************
Constructor
Use: This creates a DeckOfCards object and then shuffles the
cards
Arguments: none
Note: -1 is used to signal that no cards have been removed from
the deck
***************************************************************/
DeckOfCards::DeckOfCards()
{
//An array of the 4 possible values for the card suit
char suitVals[NUM_SUITS] = { 'C', 'D', 'H', 'S' };
int cardSub = 0; //subscript to process the deck of cards
//Go through all 52 spots in the array of Cards and put a card
//at the location
for( int suitSub = 0; suitSub < NUM_SUITS; suitSub++ )
{
//For each of the suits, put in all of the cards for the suit
for( int faceVal = 1; faceVal <= CARDS_PER_SUIT; faceVal++ )
{
//Put the card into the deck
deck[ cardSub ].setCard( faceVal, suitVals[suitSub] );
//Move to the next card in the deck
cardSub++;
}
}
//shuffle the playing cards
shuffle();
//Set the top card location to -1 to indicate the deck is brand new
topCard = -1;
}
/***************************************************************
Method: Card draw()
Use: This method draws a card from the top of the deck
Arguments: none
Returns: a Card object (the card on the top of the deck)
***************************************************************/
Card DeckOfCards::draw()
{
//Increment to get the subscript of the current top card
topCard++;
//return the card that is currently on the top of the deck
return deck[topCard];
}
/***************************************************************
Method: void shuffle()
Use: This method shuffles the deck of cards
Arguments: none
Returns: nothing
Note: this method uses the random_shuffle function that is part
of the algorithm library to shuffle the 52 cards
***************************************************************/
void DeckOfCards::shuffle()
{
//Shuffle all 52 cards that are in the deck
random_shuffle(deck, deck+MAX_CARDS);
}
/***************************************************************
Method: bool isEmpty()
Use: This method determines if the deck of cards is empty( have
all of the cards been drawn)
Arguments: none
Returns: boolean value: true if all of the cards have been drawn
false if there are still cards in the deck
***************************************************************/
bool DeckOfCards::isEmpty()
{
return topCard + 1 >= MAX_CARDS;
}
output
Player 1:
Three of Hearts Total: 3
Nine of Spades Total: 12
Eight of Spades Total: 20
Congratulations Player 1! You won 10 points
----------------
Player 2:
Nine of Hearts Total: 9
Ten of Clubs Total: 19
King of Hearts Total: 29
Sorry Player 2! -- You've busted!
----------------
Player 1:
Jack of Spades Total: 10
Four of Spades Total: 14
Seven of Spades Total: 21
Congratulations Player 1! You won 15 points
----------------
Player 2:
Ace of Diamonds Total: 11
Jack of Hearts Total: 21
Congratulations Player 2! You won 15 points
----------------
Player 1:
Eight of Clubs Total: 8
Ace of Clubs Total: 19
Seven of Clubs Total: 26
Sorry Player 1! -- You've busted!
----------------
Player 2:
Ace of Spades Total: 11
Ten of Spades Total: 21
Congratulations Player 2! You won 15 points
----------------
Player 1:
Eight of Hearts Total: 8
Ten of Hearts Total: 18
Queen of Clubs Total: 28
Sorry Player 1! -- You've busted!
----------------
Player 2:
Six of Spades Total: 6
Three of Diamonds Total: 9
Eight of Diamonds Total: 17
Congratulations Player 2! You won 10 points
----------------
Player 1:
Queen of Spades Total: 10
Jack of Clubs Total: 20
Three of Spades Total: 23
Sorry Player 1! -- You've busted!
----------------
Player 2:
King of Spades Total: 10
Two of Spades Total: 12
Four of Clubs Total: 16
Congratulations Player 2! You won 10 points
----------------
Player 1:
Queen of Hearts Total: 10
Six of Diamonds Total: 16
Queen of Diamonds Total: 26
Sorry Player 1! -- You've busted!
----------------
Player 2:
Two of Diamonds Total: 2
Seven of Diamonds Total: 9
Nine of Diamonds Total: 18
Congratulations Player 2! You won 10 points
----------------
Player 1:
Two of Hearts Total: 2
Ace of Hearts Total: 13
Four of Diamonds Total: 17
Congratulations Player 1! You won 10 points
----------------
Player 2:
Ten of Diamonds Total: 10
Seven of Hearts Total: 17
King of Clubs Total: 27
Sorry Player 2! -- You've busted!
----------------
Player 1:
Three of Clubs Total: 3
Five of Spades Total: 8
Six of Clubs Total: 14
Congratulations Player 1! You won 10 points
----------------
Player 2:
Two of Clubs Total: 2
Five of Hearts Total: 7
Four of Hearts Total: 11
Congratulations Player 2! You won 10 points
----------------
Player 1:
Nine of Clubs Total: 9
Five of Clubs Total: 14
Jack of Diamonds Total: 24
Sorry Player 1! -- You've busted!
----------------
Player 2:
Five of Diamonds Total: 5
Six of Hearts Total: 11
King of Diamonds Total: 21
Congratulations Player 2! You won 15 points
----------------
Player 1 score: 199
Player 2 score: 181
Player 1 wins.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.