Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design and implement a class for a deck of cards, named DeckOfCards. It will hav

ID: 3823942 • Letter: D

Question

Design and implement a class for a deck of cards, named DeckOfCards. It will have suit
and rank as its private data members. It will also have the necessary method to return a
card with a random suit and rank. Then, use this class to deal the cards to N players. As
an example, you can deal 13 cards each to 4 players. The dealt cards will be stored outside
the class. You will utilize the function rand() to generate a pseudo random number and the
function srand() to seed the random number generator. Remember that you are drawing
from the deck, without replacement.

Explanation / Answer

Here is the CardDeck.h:

#include "Card.cpp"
/*----------------------------------------------------------------*/
//CardDeck.h
/*-----------------------------------------------------------------*/
class CardDeck{
public:
CardDeck();
void shuffleDeck();
bool isEmpty() const; //return true if empty
Card draw();
private:
vector<Card> theDeck;
vector<Card> deltCards;
};

And CardDeck.cpp is:

#include <iostream>
#include "CardDeck.h"
using namespace std;
/*------------------------------------------------------------------------*/
/*CardDeck.cpp */
/*------------------------------------------------------------------------*/

CardDeck::CardDeck(){
/*13 cards in each suit */
/*52 new card */
for (int i = 2; i<15; ++i) {
theDeck.push_back(Card('S', i));
theDeck.push_back(Card('H', i));
theDeck.push_back(Card('D', i));
theDeck.push_back(Card('C', i));
}
srand(time(NULL)); //Must seed RNG 1time!
}
Card CardDeck::draw(){
/*Draw and return one card. */
if (theDeck.empty()) {
exit(1);
}
Card aCard = theDeck.back(); //Draw card.
theDeck.pop_back(); //Remove card
//Retain card for shuffle.
deltCards.push_back(aCard);
return(aCard);
}
void CardDeck::shuffleDeck() {
/*Replace drawn cards */
for (int i = 0; i<deltCards.size(); ++i){
theDeck.push_back(deltCards[i]);
}
//Clear the vector
deltCards.resize(0);
//Use the top level function from algorithm
random_shuffle(theDeck.begin(), theDeck.end());
}
bool CardDeck::isEmpty() const {
   if(theDeck.size() == 0)
       return true;
   return false;  
}
class Player
{
public:
Player(CardDeck&);
Card draw();
void addpoints(int);
int score() const;
void replaceCard(CardDeck&);
private:
Card myCards[3];
int myScore;
int removedCard;
};
Player::Player(CardDeck &aCardDeck)
{
myScore = 0;
for (int i = 0; i < 3; i++)
myCards[i] = aCardDeck.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(CardDeck& aCardDeck)
{
myCards[removedCard] = aCardDeck.draw();
}

int main()
{
CardDeck theDeck;
theDeck.shuffleDeck();
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.getRank()==card2.getRank())
{
player1.addpoints(1);
player2.addpoints(1);
cout << "Players tie" << endl;
}
else if (card1.getRank()>card2.getRank())
{
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's score is " << player1.score() << endl;
cout << "Player 2's score is " << player2.score() << endl;
getchar();
//getchar();
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote