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

In this homework, you will make a simualtion of the card game War. You can read

ID: 3841750 • Letter: I

Question

In this homework, you will make a simualtion of the card game War. You can read more about war at here.

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.

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.

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 assignment, 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 here.

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 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.

Explanation / Answer

  WAR is a simple game played by 2 people. One card is dealt to each player, face up. The highest value card wins. This program is created by using C++

As given in question we have to create code for card.h and card.cpp files, a deck class in deck.h and deck.cpp and The game will be played in main.cpp.

The code for:

card.h

#ifndef Card_h

#define Card_h

class Card {

public:

                // create a “blank” card

                Card();

                // constructor to create a card, setting the suit and rank

                Card(int suit, int rank);

                // set an existing card to a particular value

                void setCard(int suit, int rank);

                // return the point value of the card. Ace = 1, 2 thru 10, face cards can be 11, 12,

                int getValue();

                // display the card

                void display();

private:

                int rank;

                int suit;

};

Card.cpp

#include <iostream>

using namespace std;

#include "Card.h";

#include <string>

// create a “blank” card

Card::Card()

{

                rank;

                suit;

}

// constructor to create a card, setting the suit and rank

Card::Card(int s, int r)

{

                rank = r;

                suit = s;

}

// set an existing card to a particular value

void Card::setCard(int s, int r)

{

                rank = r;

                suit = s;

}

// return the point value of the card. Ace = 1, 2 thru 10, face cards can be 11, 12,

int Card::getValue()

{

                return rank;

}// display the card

void Card::display()

{

                string displayString;

                displayString.append("   ");

                string displayDesign;

                switch (rank) {

                case 11: displayString.append("Jack");

                                break;

                case 12: displayString.append("Queen");

                                break;

                case 13: displayString.append("King");

                                break;

                case 1: displayString.append("Ace");

                                break;

                default: displayString.append(to_string(rank));

                                break;

                }

                displayString.append(" of ");

                switch (suit) {

                case 0: displayString.append("Spades");

                                switch (rank) {

                                case 10:

                                                displayDesign.append(" ------- [10     ] [   *   ] [ * * ] [ *   * ] [ * * ] [   *   ] [    10] ------- ");

                                                break;

                                case 11:

                                                displayDesign.append(" ------- [J      ] [   *   ] [ * * ] [ ***** ] [* * *] [   *   ] [      J] ------- ");

                                                break;

                                case 12:

                                                displayDesign.append(" ------- [Q      ] [   *   ] [ * * ] [ ***** ] [* * *] [   *   ] [      Q] ------- ");

                                                break;

                                case 13:

                                                displayDesign.append(" ------- [K      ] [   *   ] [ * * ] [ ***** ] [* * *] [   *   ] [      K] ------- ");

                                                break;

                                case 1:

                                                displayDesign.append(" ------- [A      ] [   *   ] [ * * ] [ ***** ] [* * *] [   *   ] [      A] ------- ");

                                                break;

                                default:

                                                displayDesign.append(" ------- [" + to_string(rank) + "      ] [   *   ] [ * * ] [ ***** ] [* * *] [   *   ] [      " + to_string(rank) + "] ------- ");

                                                break;

                                }

                                break;

                case 1: displayString.append("Hearts");

                                switch (rank) {

                                case 10: displayDesign.append(" ------- [" + to_string(rank) + "     ] [ ** ** ] [* * *] [ *   * ] [ * * ] [   *   ] [     " + to_string(rank) + "] ------- ");

                                                break;

                                case 11: displayDesign.append(" ------- [J      ] [ ** ** ] [* * *] [ *   * ] [ * * ] [   *   ] [      J] ------- ");

                                                break;

                                case 12: displayDesign.append(" ------- [Q      ] [ ** ** ] [* * *] [ *   * ] [ * * ] [   *   ] [      Q] ------- ");

                                                break;

                                case 13: displayDesign.append(" ------- [K      ] [ ** ** ] [* * *] [ *   * ] [ * * ] [   *   ] [      K] ------- ");

                                                break;

                                case 1: displayDesign.append(" ------- [A      ] [ ** ** ] [* * *] [ *   * ] [ * * ] [ *   ] [      A] ------- ");

                                                break;

                                default: displayDesign.append(" ------- [" + to_string(rank) + "      ] [ ** ** ] [* * *] [ *   * ] [ * * ] [   *   ] [      " + to_string(rank) + "] ------- ");

                                                break;

                                }

                                break;

                case 2: displayString.append("Diamonds");

                                switch (rank) {

                                case 10:

                                                displayDesign.append(" ------- [10     ] [   *   ] [ * * ] [ *   * ] [ * * ] [   *   ] [     10] ------- ");

                                                break;

                                case 11:

                                                displayDesign.append(" ------- [J      ] [   *   ] [ * * ] [ *   * ] [ * * ] [   *   ] [      J] ------- ");

                                                break;

                                case 12:

                                                displayDesign.append(" ------- [Q      ] [   *   ] [ * * ] [ *   * ] [ * * ] [   *   ] [      Q] ------- ");

                                                break;

                                case 13:

                                                displayDesign.append(" ------- [K      ] [   *   ] [ * * ] [ *   * ] [ * * ] [   *   ] [      K] ------- ");

                                                break;

                                case 1:

                                                displayDesign.append(" ------- [A      ] [   *   ] [ * * ] [ *   * ] [ * * ] [   *   ] [      A] ------- ");

                                                break;

                                default:

                                                displayDesign.append(" ------- [" + to_string(rank) + "      ] [   *   ] [ * * ] [ *   * ] [ * * ] [   *   ] [      " + to_string(rank) + "] ------- ");

                                                break;

                                }

                                break;

                case 3: displayString.append("Clubs");

                                switch (rank) {

                                case 10:

                                                displayDesign.append(" ------- [10     ] [   *   ] [* * *] [ *** ] [   *   ] [   *   ] [   * 10] ------- ");

                                                break;

                                case 11:

                                                displayDesign.append(" ------- [J      ] [   *   ] [* * *] [ *** ] [   *   ] [   *   ] [   * J] ------- ");

                                                break;

                                case 12:

                                                displayDesign.append(" ------- [Q      ] [   *   ] [* * *] [ *** ] [   *   ] [   *   ] [   * Q] ------- ");

                                                break;

                                case 13:

                                                displayDesign.append(" ------- [K      ] [   *   ] [* * *] [ *** ] [   *   ] [   *   ] [   * K] ------- ");

                                                break;

                                case 1:

                                                displayDesign.append(" ------- [A      ] [   *   ] [* * *] [ *** ] [   *   ] [   *   ] [   * A] ------- ");

                                                break;

                                default:

                                                displayDesign.append(" ------- [" + to_string(rank) + "      ] [   *   ] [* * *] [ *** ] [   *   ] [   *   ] [   * " + to_string(rank) + "] ------- ");

                                                break;

                                }

                                break;

                }

                cout << displayString

                                << displayDesign << endl;

}

Deck.h

#include "Card.h";

#ifndef DECK_H

#define DECK_H

class Deck {

public:

                // constructor which creates a deck of 52 cards

                Deck();

                // deal a card to player

                Card deal();

                // shuffle the cards in the deck

                void shuffle();

                //Count the cards left in the deck

                int cardsLeft();

                //show the deck;

                void displayDeck();

                //            //

                ///*       void reinitialize();*/

private:

                Card storage[52];

                int size;

};

deck.cpp

#include <iostream>

using namespace std;

#include "Deck.h";

#include "Card.h";

#include <ctime>;

#include <stdlib.h>

// constructor which creates a deck of 52 cards

Deck::Deck()

{

                size = 52;

                int counter = 0;

                for (int i = 0; i < 4; i++) {

                                for (int j = 1; j < 14; j++) {

                                                storage[counter].setCard(i, j);

                                                counter++;

                                }

                }

}

//deal a card

Card Deck::deal()

{

                size--;

                //storage[size].getValue();

                Card card1 = storage[size];

                return card1;

}

// shuffle the cards in the deck

void Deck::shuffle()

{

                srand(time(0));

                int a, b;

                for (int i = 0; i < size; i++) {

                                a = rand() % size;

                                b = rand() % size;

                                Card c1 = storage[a];

                                storage[a] = storage[b];

                                storage[b] = c1;

                }

}

//Count the cards left in the deck

int Deck::cardsLeft()

{

                return size;

}

//show the deck;

void Deck::displayDeck()

{

                for (int i = 0; i < size; i++) {

                                storage[i].display();

                }

}

main.cpp

#include <iostream>

using namespace std;

#include "Card.h";

#include "Deck.h";

#include <string>

#include <algorithm>

#include <io.h>

#include <fcntl.h>

//method for playing the game

void game(Deck deck1) {

                cout << "Get ready to play WAR!!!" << endl;

                bool wannaPlay = true;

                while (wannaPlay) {

                                cout << "There are " << deck1.cardsLeft() << " card in the deck." << endl;

                                cout << "...dealing..." << endl;

                                cout << "One for you..." << endl;

                                Card player1 = deck1.deal();

                                player1.display();

                                cout << "One for me..." << endl;

                                Card player2 = deck1.deal();

                                player2.display();

                                if (player1.getValue() > player2.getValue()) {

                                                cout << "You Win!!!" << endl;

                                }

                                else if (player1.getValue() < player2.getValue()) {

                                                cout << "I Win!!!" << endl;

                                }

                                else {

                                                cout << "It's a Tie!!!" << endl;

                                }

                                cout << endl << "Wanna play again? (yes/no)" << endl;

                                string choice;

                                cin >> choice;

                                transform(choice.begin(), choice.end(), choice.begin(), ::tolower);

                                string aa = "yes";

                                string bb = "no";

                                if (choice == aa) {

                                                wannaPlay = true;

                                                if (deck1.cardsLeft() == 0) {

                                                                cout << "Game Over!!!" << endl

                                                                                << "Goodbye" << endl;

                                                                wannaPlay = false;

                                                }

                                }

                                else if (choice == bb) {

                                                wannaPlay = false;

                                                cout << endl << "Goodbye" << endl;

                                }

                                else if (choice != aa || choice != bb) {

                                                cout << "Invalid input" << endl;

                                                bool invalid = true;

                                                while (invalid) {

                                                                cin >> choice;

                                                                if (choice == aa) {

                                                                                invalid = false;

                                                                }

                                                                else if (choice == bb) {

                                                                                invalid = false;

                                                                                wannaPlay = false;

                                                                                cout << "Goodbye" << endl;

                                                                }

                                                }

                                }

                }

};//end game emthod

//menu method for display

void menu() {

                cout << "1)          Get a new card deck" << endl;

                cout << "2)          Show all remaining cards in the deck" << endl;

                cout << "3)          Shuffle" << endl;

                cout << "4)          Play WAR!" << endl;

                cout << "5)          Exit " << endl;

};//end menu method

//main class

int main() {

                int userChoice;

                bool wChoice = true;

                Deck deck1;

                while (wChoice) {

                                menu();

                                cin >> userChoice;

                                switch (userChoice) {

                                case 1:

                                                cout << "New card deck created" << endl;

                                                break;

                                case 2:

                                                deck1.displayDeck();

                                                break;

                                case 3:

                                                deck1.shuffle();

                                                deck1.displayDeck();

                                                break;

                                case 4:

                                                game(deck1);

                                                break;

                                case 5:

                                                cout << "Goodbye" << endl;

                                                wChoice = false;

                                                break;

                                }

                }

                system("PAUSE");

                return 0;

}//end main

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