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

For this program, I need to simulate the card game WAR. The issue I am facing is

ID: 3573911 • Letter: F

Question

For this program, I need to simulate the card game WAR. The issue I am facing is that I'm getting an error in the main program, that's telling me '<<' no operator found which takes a right-hand operand of type 'Card'. Same goes for == and >, which are used in the if/else if/else statements below it. Any guidance as to what I did wrong is greatly appreciated.

#include<cctype> //Required for toupper()
#include<string> //Required for string
#include<iostream> //Required for ostream
#include<vector> //Required for Vector
#include<ctime> //Required for time()
#include<algorithm> //Required for random_shuffle()
using namespace std;

class Card {

public:
   //Constructors
   Card();           //default
   Card(char asuit, int aRank); //parameterized
   //Accessors
   int getRank() const;
   char getSuit() const;
   void displayCard(ostream& outstream) const;

private:
   char suit;
   int rank;
};


/*------------------------------------------------------------------*/
//Card.cpp - Card class implementation//
/*-----------------------------------------------------------------*/

//Constructors
Card::Card() :rank(2), suit('S')
{
}
Card::Card(char ch, int i) : rank(i)
{
   suit = toupper(ch);
}
//Accessor Methods
int Card::getRank() const
{
   return rank;
}

char Card::getSuit() const
{
   return suit;
}

//Formatted Display Method
void Card::displayCard(ostream& out) const
{
   string suitString;
   switch (suit){
   case 'S':
       suitString = "Spades";
       break;
   case 'H':
       suitString = "Hearts";
       break;
   case 'D':
       suitString = "Diamonds";
       break;
   case 'C':
           suitString = "Clubs";
           break;
       default:
           suitString = "Invalid Suit";
   }
   if (rank >= 2 && rank<11){
       out << rank << "of" << suitString;
   }
   else{
       switch (rank){
       case 11:
           out << "JACK of " << suitString;
           break;
       case 12:
           out << "Queen of " << suitString;
           break;
       case 13:
           out << "KING of " << suitString;
           break;
       case 14:
           out << "ACE of " << suitString;
           break;
       }
   }
   return;
}

/*----------------------------------------------------------------*/
//CardDeck.h
/*-----------------------------------------------------------------*/

class CardDeck{
public:
CardDeck();
void shuffleDeck();
bool isEmpty() const; //return true if empty
Card draw();
private:
vector<Card> theDeck;
vector<Card> deltCards;
};


/*------------------------------------------------------------------------*/
/*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());
}
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;
   void 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 == 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's score is " << player1.score() << endl;
   cout << "Player 2's score is " << player2.score() << endl;

   getchar();
   getchar();
   return 0;
}

Explanation / Answer

Card card1 = player1.draw();
Card card2 = player2.draw();

These card1 and card2 are objects of Card. Thus you will not be able to compare these objects unless you have some operator overloading.

Compare the rank of card1 with card2 and see which player has the highest rank.

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