When reviewing the game of Three Card War how would we break down the game into
ID: 3693264 • Letter: W
Question
When reviewing the game of Three Card War how would we break down the game into it's basic elements?
What C++ classes would we need to implement the game?
Identify these basic classes, choose a class and describe how you might implement the .h file with the appropriate member functions for the class.
( The card game Three Card War is a simple card game for two players. The game uses a 52 card conventional deck. To begin the deck is shufed and each player draws three cards. The players may look at their three cards. The remaining cards are placed in a pile facedown between the two players. The players decide who is going first.The player going frst selects one of his three cards and places it face up.
The opposing player selects one of her three cards and places it face up.
If the ranks of both cards are the same (a push), then both players retain their
cards and set them aside. Otherwise, the player with the highest-ranking card
keeps both cards (again, setting them aside).
After playing the round, each player draws one card from the deck to replace
the card just played with the frst playing drawing frst.
The player that won the round is now the frst player for the next round unless
there was a push then the original frst player goes frst again.
The game ends when the deck is exhausted and both players have played all of
their three cards.
The player with the most cards set aside wins.
Card ranking begins with the 2 as the lowest card going up to the ten, then jack, queen, king, and ace. )
Explanation / Answer
While implementing this Game of War, we need to decide the classes and the approached that we will be using in this program
to implement.
Use of Templates:-
Templates allows programmers to define data structures capable of working on any other data type.
For this Game of War we need to have a suit and a rank of the card.
The suit of the cards can be represented by the following enmeration type.
enum suits {club, diamond, heart, spade};
The rank can be represented by an integer from 1 to 13, becuase we have 13 cards for one suit.
1) The classes requireed to implement this game are:-
a) Card
b) Deck
These are the omly classes used, and have been implemented in the second part of the question.
2)
The class Card has been defined as follows:-
class Card
{
public:
// Constructors
Card(); // initialize a card with default values
Card(suits, int); // initialize a card with given values
int getRank() const;
suits getSuit() const;
protected:
// Member variables (attributes)
int rank;
suits suit;
// Output a card to an output stream
friend ostream& operator<<(ostream&, const Card&);
// Define ordering relationships on cards
friend bool operator<(const Card&, const Card&);
friend bool operator>(const Card&, const Card&);
friend bool operator==(const Card&, const Card&);
};
After that we define the constructor of the class Card.
Card::Card()
// Initialize a card to default values
{
rank = 1; // the ace
suit = spade;
}
Card::Card(suits sv, int rv)
// Initialize a card to given values
{
rank = rv;
suit = sv;
}
We can also use the alternate version of the Card constructor. Below are them:-
Card::Card() : rank(1), suit(spade) {}
Card::Card(suits sv, int rv) : rank(rv), suit(sv) {}
Inputs/Outputs of the cards:-
We defined the inputs and the output using the inserion operator. We just need to extend the function.
ostream& operator<<(ostream& out, const Card& aCard)
{
switch(aCard.rank)
{
case 1: out << "Ace"; break;
case 11: out << "Jack"; break;
case 12: out << "Queen"; break;
case 13: out << "King"; break;
default: out << aCard.rank; break;
}
switch(aCard.suit)
{
case diamond: out << " of Diamonds"; break;
case spade: out << " of Spades"; break;
case heart: out << " of Hearts"; break;
case club: out << " of Clubs"; break;
}
return out;
}
Compare the Card values:-
After taking the inputs, we need to comapre the card values.
The implementation of the comparison method is defined below:-
bool operator<(const Card& c1, const Card& c2)
{ return c1.rank < c2.rank; }
bool operator>(const Card& c1, const Card& c2)
{ return c1.rank > c2.rank; }
bool operator==(const Card& c1, const Card& c2)
{ return c1.rank == c2.rank; }
Now we use the class Deck
class Deck
{
public:
// constuctor
Deck(); // creates an initial deck
// operations on a deck (methods)
void shuffle(); // randomly change the order of the cards
bool isEmpty() const; // return true of empty; false otherwise
Card draw(); // return the next card
protected:
// member variables (attributes)
Card cards[52]; // hold collection of cards
int topCard; // index one greater than last card
};
Defining the constructor of the deck class.
Deck::Deck()
// Construct a deck;
{
topCard = 0;
for (int i = 1; i <= 13; ++i)
{
Card c1(diamond, i);
Card c2(spade, i);
Card c3(heart, i);
Card c4(club, i);
cards[topCard++] = c1;
cards[topCard++] = c2;
cards[topCard++] = c3;
cards[topCard++] = c4;
}
}
It is very much required to shuffle the deck of the cards.So, to implement the shuffle
function, we use the below implementation.
void Deck::shuffle()
{
random_shuffle(cards, cards+52);
}
random_shuffle is the member of the library algorithm, which helps in generating the random numbers.
Check whether the deck is empty or not. For this we imlemented teh boolean function as defined as:-
bool Deck::isEmpty() const
{
return topCard <= 0;
}
Similarly the member function have been defined whrn the players draw the card from the deck.
Card Deck::draw()
{
if (isEmpty())
throw "Empty Deck";
return cards[--topCard];
}
Now, we will have the real class called Player, which are mostly involved to complete the above class defined.
class Player
{
public:
// constructor
Player(Deck&);
// operators
Card draw();
void addPoints(int);
int score() const;
void replaceCard(Deck&);
protected:
Card myCards[3];
int myScore;
int removedCard;
};
Implementation of the player class.
Player::Player(Deck& aDeck)
{
myScore = 0;
for (int i = 0; i < 3; i++)
myCards[i] = aDeck.draw();
removedCard = 0;
}
Card Player::draw()
{
removedCard = rand() % 3;
return myCards[removedCard];
}
void Player::addPoints(int howMany)
{ myScore += howMany; }
int Player::score() const
{ return myScore; }
void Player::replaceCard(Deck& aDeck)
{ myCards[removedCard] = aDeck.draw(); }
Afterall we have the driver program that calls the above required class and its member functions
and completes the Game of War.
int main()
{
Deck theDeck;
theDeck.shuffle();
Player player1(theDeck);
Player player2(theDeck);
while (!theDeck.isEmpty())
{
Card card1 = player1.draw();
cout << "Player 1 draws " << card1 << endl;
Card card2 = player2.draw();
cout << "Player 2 draws " << card2 << endl;
if (card1 == card2)
{
player1.addPoints(1);
player2.addPoints(1);
cout << "Players tie" << endl;
}
else if (card1 > card2)
{
player1.addPoints(2);
cout << "Player 1 wins round" << endl;
}
else
{
player2.addPoints(2);
cout << "Player 2 wins round" << endl;
}
player1.replaceCard(theDeck);
player2.replaceCard(theDeck);
}
cout << "Player 1 score " << player1.score() << endl;
cout << "Player 2 score " << player2.score() << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.