War (C++ Program). (Don\'t just copy and paste. I need the functions to be exact
ID: 3878293 • Letter: W
Question
War (C++ Program). (Don't just copy and paste. I need the functions to be exactly like the one provided below on the classes. )
Note: This program will also need to display the actual cards just like the sample below.
Classes:
1)The Deck class which will create the deck of cards Deck.h , Deck.cpp
2)The Card class which creates cards Card.h , Card.cpp
The main logic of the program will be in the main class. You will use the Card class and the CardDeck class to play the game.
Here the methods you will need to build. Feel free to add more if you need them.
public class CardDeck
public Deck( // constructor which creates a deck of 52 cards
public void resetDeck // reset’s the deck so it looks like a new deck.
public Card deal( ) // deal a card
public int cardsLeft( ) // return the number of cards left in the deck.
public void shuffle( ) // shuffle the cards in the deck
public displayDeck// show all the cards in the deck.
public class Card
public Card(); // Creates a blank card.
public Card (char s, char r ) // constructor to create a card, setting the suit and rank
public void setCard(char s, char r); // a method to set an existing blank card to a particular value
public int getValue( ) // return the point value of the card. Ace = 1, 2 thru 10, face cards = 10
public void displayCard( ) // method to display the card
Sample output.
You will need to provide a menu with the following choices:
1)Get a new card deck
2)Show all the cards that reamin in the deck
3)Shuffle
4)Play WAR!
5)Exit
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 SpadesExplanation / Answer
Here is your c++ war game(cards and deck) program:
Main class:
#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;
}
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
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();
}
}
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
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;
}
I hope this solves your problem
Comment if you have any problem in above code
And please give it a thumbs up if it solved your problem :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.