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

Using c++ For this program, The objective of this program is to create a game ca

ID: 3876382 • Letter: U

Question

Using c++ For this program, The objective of this program is to create a game called war in which two players who draw the highest card wins. If the two players draw a cards that has the same, then it's a tie. Afterwards, announce who won the game. The program needs to look exactly like the sample provided below.

The program should have two classes. Deck.h, Deck.cpp       Card.h, Card.cpp

Here is a sample of the program.

There are 52 cards in the deck. . . .dealing. . . . One for you. . . Two ofDiamonds [2 2] One for me... Ace of Hearts You Win!!!! Wanna play again? (yes There are 50 cards in the deck. . . .dealing. . . . One for you. . . Queen of Spades

Explanation / Answer

Here is the c++ war game program files:

Main.cpp:

#include <iostream>

#include "Card.h";

#include "Deck.h";

#include <string>

#include <algorithm>

#include <io.h>

#include <fcntl.h>

using namespace std;

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;

}

}

}

}

};

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;

};

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;

}

Card.h

#ifndef CARD_H

#define CARD_H

class Card {

public:

Card();

Card(int suit, int rank);

void setCard(int suit, int rank);

int getValue();

void display();

private:

int rank;

int suit;

};

#endif

deck.h

#include "Card.h";

#ifndef DECK_H

#define DECK_H

class Deck {

public:

Deck();

Card deal();

void shuffle();

int cardsLeft();

void displayDeck();

private:

Card storage[52];

int size;

};

#endif

Card.cpp:

#include <iostream>

#include "Card.h";

#include <string>

using namespace std;

Card::Card()

{

rank;

suit;

}

Card::Card(int s, int r)

{

rank = r;

suit = s;

}

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

{

rank = r;

suit = s;

}

int Card::getValue()

{

return rank;

}

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

#include <iostream>

#include "Deck.h";

#include "Card.h";

#include <ctime>;

#include <stdlib.h>

using namespace std;

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++;

}

}

}

Card Deck::deal()

{

size--;

Card card1 = storage[size];

return card1;

}

int Deck::cardsLeft()

{

return size;

}

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;

}

}

void Deck::displayDeck()

{

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

storage[i].display();

}

}

Comment if you have any problem in above code

And give it a thumbs up if it solved your problem :)

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