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

The console output should be relitively long as shown below. The game should be

ID: 3842201 • Letter: T

Question

The console output should be relitively long as shown below. The game should be played until all cards are gone. Deck instructions are very specific. FOLLOW PROGRAM LOGIC FLOW BELOW. NONFUNCTIONAL ANSWERS WILL BE REPORTED.

War is a card game played by 2 people. https://en.wikipedia.org/wiki/War_(card_game)

MUST HAVE IMPLEMNTATION OF FIVE FILES.

Shuffle with srand using the time for randomization.

Can only use #include random #include iterator #include iostream #include vector #include string #include ctime(to seed srand) #include algorithm and implemented header files

Shuffle MUST BE RANDOMIZED with

________________________________________________________________

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 the War continues....

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.

WARNING: 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 when you have a "non-war" case. 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

________________________________________________________________

card.cpp

#include <iostream>

#include <string>

#include "card.h"

using namespace std;

//default constructor

card::card() {

  

  

}

//alternate constructor

card::card(Suit a, Rank b) {

suit_ = a;

rank_ = b;

}

//gets the rank

Rank card::getRank() const {

  

return rank_;

  

}

//gets the suit

Suit card::getSuit() const {

  

return suit_;

}

//sets the rank

void card::setRank(Rank a) {

rank_ = a;

}

//sets the suit

void card::setSuit(Suit a) {

suit_ = a;

}

//uses a switch statement to set the name of the suit and the other which sets the rank to a string variable to be printed out as well.

ostream & operator<<(ostream& os, const card &myCard) {

string sname, rname;

if (myCard.getSuit() == HEARTS)

{

sname = "H";

}

else if(myCard.getSuit() == CLUBS)

{

sname = "C";

}

else if(myCard.getSuit() == SPADES)

{

sname = "S";

}else if(myCard.getSuit() == DIAMONDS)

{

sname = "D";

}

  

if (myCard.getRank()== TWO)

{

rname = "2";

}

else if (myCard.getRank() == THREE)

{

rname = "3";

}

else if (myCard.getRank() == FOUR)

{

rname = "4";

}

else if (myCard.getRank() == FIVE)

{

rname = "5";

}

else if (myCard.getRank() == SIX)

{

rname = "6";

}

else if (myCard.getRank() == SEVEN)

{

rname = "7";

}

else if (myCard.getRank() == EIGHT)

{

rname = "8";

}

else if (myCard.getRank() == NINE)

{

rname = "9";

}

else if (myCard.getRank() == TEN)

{

rname = "10";

}

else if (myCard.getRank() == JACK)

{

rname = "J";

}

else if (myCard.getRank() == QUEEN)

{

rname = "Q";

}

else if (myCard.getRank() == KING)

{

rname = "K";

}

else if (myCard.getRank() == ACE)

{

rname = "A";

}

os << sname << rname;

sname.clear();

rname.clear();

  

return os;

}

//compares equality between the ranks of two different cards.

bool operator ==( card a , card b) {

  

if (a.getRank() == b.getRank()) {

return true;

}

else {

return false;

}

}

//compares two cards using the cards ranks.

bool operator <( card a, card b) {

  

if (a.getRank() < b.getRank()) {

return true;

}

else {

return false;

}

  

}

//compares two cards using the cards ranks.

bool operator >( card a, card b) {

  

if (a.getRank() > b.getRank()) {

return true;

}

else {

return false;

}

}

card.h

#ifndef _WAR_CARDS_

#define _WAR_CARDS_

#include <iostream>

#include <string>

#include <vector>

using namespace std;

//enum to create all of 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};

class card {

  

private:

Rank rank_;

Suit suit_;

  

  

public:

//default constructor

card() ;

//constructs a card with parameters set already

card(Suit a, Rank b);

//sets the rank

void setRank(Rank a);

//sets the suit

void setSuit(Suit a);

//gets the rank

Rank getRank() const;

//gets the suit

Suit getSuit() const;

};

//to output to the console

ostream & operator<<(ostream&a, card const &b);

//to check if it is equal

bool operator ==( card a, card b);

//to check if it is less

bool operator <( card a, card b);

//to check if it is greater than.

bool operator >( card a, card b);

#endif

________________________________________________________________

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.

________________________________________________________________

example console output:

Explanation / Answer

please find the below code:

#include <iostream>

#include <string>

#include "card.h"

using namespace std;

card::card() {

}

card::card(Suit a, Rank b) {

suit_ = a;

rank_ = b;

}

Rank card::getRank() const {

return rank_;

}

Suit card::getSuit() const {

return suit_;

}

void card::setRank(Rank a) {

rank_ = a;

}

void card::setSuit(Suit a) {

suit_ = a;

}

ostream & operator<<(ostream& os, const card &myCard) {

string sname, rname;

if (myCard.getSuit() == HEARTS)

{

sname = "H";

}

else if(myCard.getSuit() == CLUBS)

{

sname = "C";

}

else if(myCard.getSuit() == SPADES)

{

sname = "S";

}else if(myCard.getSuit() == DIAMONDS)

{

sname = "D";

}

if (myCard.getRank()== TWO)

{

rname = "2";

}

else if (myCard.getRank() == THREE)

{

rname = "3";

}

else if (myCard.getRank() == FOUR)

{

rname = "4";

}

else if (myCard.getRank() == FIVE)

{

rname = "5";

}

else if (myCard.getRank() == SIX)

{

rname = "6";

}

else if (myCard.getRank() == SEVEN)

{

rname = "7";

}

else if (myCard.getRank() == EIGHT)

{

rname = "8";

}

else if (myCard.getRank() == NINE)

{

rname = "9";

}

else if (myCard.getRank() == TEN)

{

rname = "10";

}

else if (myCard.getRank() == JACK)

{

rname = "J";

}

else if (myCard.getRank() == QUEEN)

{

rname = "Q";

}

else if (myCard.getRank() == KING)

{

rname = "K";

}

else if (myCard.getRank() == ACE)

{

rname = "A";

}

os << sname << rname;

sname.clear();

rname.clear();

return os;

}

bool operator ==( card a , card b) {

if (a.getRank() == b.getRank()) {

return true;

}

else {

return false;

}

}

bool operator <( card a, card b) {

if (a.getRank() < b.getRank()) {

return true;

}

else {

return false;

}

}

bool operator >( card a, card b) {

if (a.getRank() > b.getRank()) {

return true;

}

else {

return false;

}

}

card.h

#ifndef _WAR_CARDS_

#define _WAR_CARDS_

#include <iostream>

#include <string>

#include <vector>

using namespace std;

//enum to create all of 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};

class card {

private:

Rank rank_;

Suit suit_;

public:

card() ;

card(Suit a, Rank b);

void setRank(Rank a);

void setSuit(Suit a);

Rank getRank() const;

Suit getSuit() const;

};

ostream & operator<<(ostream&a, card const &b);

bool operator ==( card a, card b);

bool operator <( card a, card b);

bool operator >( card a, card b);

#endif

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