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

In this problem you will make a simualtion of the card game War in c++. You can

ID: 3839490 • Letter: I

Question

In this problem you will make a simualtion of the card game War in c++. You can read more about war at https://en.wikipedia.org/wiki/War_%28card_game%29.

War is a card game played by 2 people. This card game is entirely random. There is no point where the player's decisions change the outcome of the game. This means that we can have a computer program play the game against itself and the outcome will be the same as with two people.

Cannot use: #include <map> or #include <deque>

Likely needed: #include <iostream> #include<algorithms> #include<string> maybe#include<vector>

A Single Card

The game is played with standard poker cards. Make card.h and card.cpp files. A card has a Suit and a Rank. There are four suits: Clubs, Spades, Diamonds, and Hearts. There are thirteen ranks in order from smallest to largest 2-10, Jack, Queen, King, and Ace.

You must use the below enum types for the Ranks and Suits.

          enum Rank {TWO,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE,TEN,JACK,QUEEN,KING,ACE};
                    enum Suit {CLUBS,SPADES,DIAMONDS,HEARTS};
                   

Create a card class that has a rank and suit. Your class MUST meet the following requirements. You may add additional functionality to complete the problem, but there will be a penalty if any of these functions are not defined.

Default Constructor

Accessor Methods for the Rank and Suit

Overloaded output operator <<

Overloaded comparison operators ==,<.>

A Deck of Cards

The game is played with a deck of cards. At the start, there is one deck. This deck is shuffled. Then the deck is cut in half and each player gets their own deck with half the cards.

Create a deck class in deck.h and deck.cpp. The deck class must meet the following requirements (although you will likely decide on additional methods/attributes):

Deck can be shuffled.

Deal a single card from the TOP of the deck. (return a Card)

Deal a stack of int n cards from the TOP of the deck. (return a Deck)

Find the number of cards in the deck.

Add a new card to the BOTTOM of the deck.

How to Shuffle a Vector

An easy shuffle to implement is the Fisher-Yates Shuffle. This will work to shuffle your deck.

You can get random numbers by using the rand command. Learn more about it http://www.cplusplus.com/reference/cstdlib/rand/.

The Game of War!

The game will be played in main.cpp. Here is an overview of how the game should be run. An example output is attached in war_game.txt.

Create a new Deck with 52 cards.

Shuffle the deck and deal each player half the cards.

While both player still have Cards do the following:

Each player takes the top card from their deck.

The player with the larger card (by rank) takes both cards and puts them at the bottom of their deck.

If both cards have equal ranks, then War breaks out.

Both players draw two new cards from the top of the deck.

The first card is placed face down, the second face up.

If either play does not have enough cards, they immediately lose.

The player that has the highest face up card wins.

If the face up cards have the same value, then War breaks out.

When a winner is decided, they get all the cards that have been draw since the original tie.

The player that ran out of cards is the loser, announce which player was victorious.

Your main.cpp will print out every card battle that takes place. It will also print out a line to tell the user when a war has broken out. Finally, your program will print out the winner of the game (Player 1 or Player 2). Refer to the sample output file at the top of this assignment.

Depending on how you put the cards back on the bottom of the deck you can create an infinite loop. One way to help avoid this is to always put the larger of the two cards on the bottom first. In other words, say you have cards c1 and c2. If c1>c2, then put c1 first on the bottom of your deck, then c2. Otherwise first put c2 on the bottom followed by c1.

What to Submit:

main.cpp - Main War Game

card.h - Header for Card

card.cpp - Implementation for Card

deck.h - Header for Deck

deck.cpp - Implementation for Deck

war_game.txt:

Explanation / Answer

I have designed all the major functionality after looking into requirements for the given scenario.It is used for predicting the next emphasis of the loop, whether to check and confirm that the nextCard will become the currentCard, which helps in solving the prediction to be efficient.

I have created the main.cpp, card.h, card.cpp, deck.h, deck.cpp and included the comments for each part of the code with the final output of it.

Let me explain you in step-by-step procedure:-

Step-1:

Main.Cpp Program:-

#include <vector>
#include <list>
#include <stack>
#include <iostream>
#include <string>

namespace cardgame
{

   class CardGame
   {

public:
   std::list deck;
   ComputerPlayer *computer;
   Player *human;
   std::stack discardPile;

   virtual ~CardGame()
   {
       delete computer;
       delete human;
   }

   CardGame();

private:
   void shuffle();


public:
   virtual void start();


namespace cardgame
{

   CardGame::CardGame()
   {
   std::vector<wchar_t> suit = {L'H', L'S', L'D', L'C'};
   deck = std::list();

   for (int i = 0; i < 4; i++)
   {
   for (int j = 1; j <= 13; j++)
   {
   Card tempVar(j, suit[i]);
   deck.push_back(&tempVar);
   }
   }

   discardPile = std::stack();

   human = new Player();
   computer = new ComputerPlayer();
   }

   void CardGame::shuffle()
   {

   Collections::shuffle(static_cast<std::vector>(deck));

   this->deck = static_cast<std::list>(deck);

   }

   void CardGame::start()
   {
   shuffle();

   for (int i = 0; i < 4; i++)
   {
   human->addCard(deck.remove(0));
   computer->addCard(deck.remove(0));
   }
   int turn = 0;
   int choice;
   Card *current;
   Card *top;
   Scanner *in_Renamed = new Scanner(System::in);
   while (!human->hasWon() && !computer->hasWon())
   {
   if (deck.empty())
   {
   deck.addAll(discardPile);
   discardPile.clear();
   shuffle();
   }

   if (turn == 0)
   {
   std::wcout << L"Your cards are: " << std::endl;
   human->displayCards();
   std::wcout << L"" << std::endl;
   if (discardPile.empty())
   {
   choice = 2;
   std::wcout << L"The discard pile is empty, draw a card." << std::endl;
   std::wcout << L"" << std::endl;

   }
   else
   {
   top = discardPile.top();
   std::wcout << L"The top of the discard pile is the " << top << std::endl;
   std::wcout << L"Do you want to pick up the " << top->toString() << L"(1) or draw a card (2)" << std::endl;
   choice = in_Renamed->nextInt();
   }
   }
   }
  
if (choice == 1)
{
current = discardPile->pop();
std::wcout << L"You picked up " << current << std::endl;

}
else
{
current = deck->remove(0);
std::wcout << L"You drew " << current << std::endl;
}
human::addCard(current);
std::wcout << L"Now your cards are " << std::endl;
human::displayCards();
std::wcout << L"" << std::endl;
std::wcout << L"Which one do you want to discard ?" << std::endl;
choice = in_Renamed::nextInt();

discardPile::push(human::discard(choice - 1));
}
else
{

if (!discardPile->isEmpty() && discardPile->peek().getValue() == computer::getMaxValue())
{
top = discardPile->pop();
std::wcout << L"I will pick up the " << top << std::endl;
computer::addCard(top);

}
else
{
std::wcout << L"I will draw a new card." << std::endl;
current = deck->remove(0);
computer::addCard(current);
}
current = computer::discard();
discardPile::push(current);
std::wcout << L"I will discard the " << current << std::endl;
}

turn = 1 - turn;
}

}

public:
void finish()
{
std::wcout << L"--------------------------" << std::endl;
std::wcout << L"My cards are " << std::endl;
computer::displayCards();
std::wcout << L"--------------------------" << std::endl;
std::wcout << L"Your cards are " << std::endl;
human::displayCards();
std::wcout << L"--------------------------" << std::endl;
if (human::hasWon())
{
std::wcout << L"You have won !" << std::endl;
}
else
{
std::wcout << L"the computer wins !" << std::endl;
}
std::wcout << L"--------------------------" << std::endl;
}

static void main(std::wstring[] args)
{
CardGame *game = new CardGame();
game->start();
game->finish();
}
}

Step-2:

Card.h Program:-

namespace cardgame
{


   class ComputerPlayer : public Player
   {

private:
   int decideDiscard();

public:
   virtual Card *discard();

   virtual int getMaxValue();
   };
}

Step-3:

Card.Cpp Program:-

namespace cardgame
{

   int ComputerPlayer::decideDiscard()
   {
   int pValue = cards->get(0).getValue();
   int index = 0;
   int count = 1;
   int min = 1;
   for (int i = 1; i < cards->size(); i++)
   {
   if (cards->get(i).getValue() != pValue)
   {
   if (count < min)
   {
   min = count;
   index = i - 1;
   pValue = cards->get(i).getValue();

   }
   count = 1;

   }
   else
   {
   count++;
   }
   }
   return index;
   }

   Card *ComputerPlayer::discard()
   {
   return cards->remove(decideDiscard());
   }

   int ComputerPlayer::getMaxValue()
   {
   int pValue = cards->get(0).getValue();
   int count = 1;
   int max = 1;

   for (int i = 1; i < cards->size(); i++)
   {
   if (cards->get(i).getValue() != pValue)
   {
   if (count > max)
   {
   max = count;
   pValue = cards->get(i).getValue();
   }
   count = 1;
   }
   else
   {
   count++;
   }
   }
   return pValue;
   }
}

Step-4:

deck.h Program:-

#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

namespace cardgame
{
   class deck
   {

public:
   std::wstring name;
   std::vector cards;

   deck(const std::wstring &playerName);

   deck();

   virtual void addCard(Card *card);

   virtual Card *discard(int index);

   virtual Card *getCard(int index);

   virtual bool hasWon();

   virtual void displayCards();

   };
}

Step-5:

deck.Cpp Program:-

namespace cardgame
{

   deck::deck(const std::wstring &playerName)
   {
   name = playerName;
   }

   deck::deck()
   {
   cards = std::vector();
   }

   void deck::addCard(Card *card)
   {
   cards.push_back(card);
   std::sort(cards.begin(), cards.end());
   }

   Card *deck::discard(int index)
   {
   return cards.erase(cards.begin() + index);
   }

   Card *deck::getCard(int index)
   {
   return cards[index];
   }

   bool deck::hasWon()
   {
   int value = cards[0].getValue();
   for (int i = 1; i < cards.size(); i++)
   {
   if (cards[i].getValue() != value)
   {
   return false;
   }
   else
   {
   }
   }

   return true;
   }

   void deck::displayCards()
   {
   for (int i = 0; i < cards.size(); i++)
   {
   std::wcout << i << 1 << L". " << cards[i] << std::endl;
   }
   }
}

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