Using c++ For this program, The objective of this program is to create a game ca
ID: 3876059 • 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.
Here is a sample of the program.
One for me... [10 [*10] You Win!!!! Wanna play again? (no) Goodbye You will need to provide a menu with the following choices 1) Get a new card deck 2) Show all remaining cards in the deck 3) Shuffle 4) Play WAR! 5) Exit Things to consider You cannot continue the game if there are not enough cards in the deck. How are you going to shuffle? * You must show nice cards - the cards I showed are examples. Feel free to make yours better.Explanation / Answer
main.cpp
#include <iostream>
#include <string>
#include "cards.h"
#include "deck.h"
using namespace std;
//play game
void game(deck d)
{
cards player_1,player_2;
string ch;
bool play_flag = true;
cout << "*****Card War Game*****" << endl;
while (play_flag) //when player wants to play
{
cout << d.count_cards_left() << " cards are there in the deck ! " << endl;
cout << "One for player1..." << endl;
player_1 = d.play_deal();
player_1.display_cards();
cout << "One for player2..." << endl;
player_2 = d.play_deal();
player_2.display_cards();
if (player_1.get_value() > player_2.get_value())
cout << "Player1 Win!!!!" << endl;
else if (player_1.get_value() < player_2.get_value())
cout << "Player2 Win!!!!" << endl;
else
cout << "Tie!!!!" << endl;
cout << endl << "Wanna play again? (yes/no): " << endl;
cin >> ch;
if (ch.compare("yes") == 0)
{
play_flag = true;
if (d.count_cards_left() == 0)
{
cout << "GameOver!!!!" << endl<< "Goodbye" << endl;
play_flag = false;
}
}
else if (ch.compare("no") == 0)
{
play_flag = false;
cout << endl << "Goodbye" << endl;
}
else if (ch.compare("yes") != 0 || ch.compare("no") != 0)
{
cout << "Invalid Input!!!!" << endl;
bool f = true;
while (f)
{
cin >> ch;
if (ch.compare("yes") == 0)
f = false;
else if (ch.compare("no") == 0)
{
f = false;
play_flag = false;
cout << "Goodbye" << endl;
}
}
}
}
}//game method ends
//main_menu method
void main_menu()
{
cout << "Menu: " << endl;
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;
}
//main method
int main()
{
int user_ch;
bool ff = true;
deck d;
while (ff)
{
main_menu();
cout << "Enter your choice: ";
cin >> user_ch;
switch (user_ch)
{
case 1: cout << "New card deck created" << endl;
break;
case 2: d.display_deck();
break;
case 3: d.shuffle_cards();
d.display_deck();
break;
case 4: game(d);
break;
case 5: cout << "Goodbye" << endl;
ff = false;
break;
}//end of switch
}//end of while
return 0;
}
cards.h
#ifndef cards_h
#define cards_h
class cards
{
public:
cards(); // constructor for creating blank cards
cards(int suit, int rank); // constructor for creating cards, setting suit and rank
void set_cards(int suit, int rank); // setting existing cards to particular value
int get_value(); // return rank of the cards
void display_cards(); // display cards
private:
int rank;
int suit;
};
#endif
cards.cpp
#include <iostream>
#include <string>
#include "cards.h"
using namespace std;
// constructor for creating blank cards
cards::cards()
{
rank;
suit;
}
// constructor for creating cards, setting suit and rank
cards::cards(int ss, int rr)
{
suit = ss;
rank = rr;
}
// setting existing cards to particular value
void cards::set_cards(int ss, int rr)
{
suit = ss;
rank = rr;
}
// return rank of the cards
int cards::get_value()
{
return rank;
}
// display cards
void cards::display_cards()
{
string display_card_name, display_card_pattern;
switch (rank) //display the rank of card accordingly
{
case 1: display_card_name.append("Ace ");
break;
case 11: display_card_name.append("Jack ");
break;
case 12: display_card_name.append("Queen ");
break;
case 13: display_card_name.append("King ");
break;
default: display_card_name.append(to_string(rank));
break;
}
display_card_name.append(" of ");
switch (suit) //display the suit of rank
{
case 0: display_card_name.append("Spades");
switch (rank)
{
case 1: display_card_pattern.append(" [A ] [ * ] [ * * ] [ ***** ] [* * *] [ * ] [ A] ");
break;
case 10: display_card_pattern.append(" [10 ] [ * ] [ * * ] [ * * ] [ * * ] [ * ] [ 10] ");
break;
case 11: display_card_pattern.append(" [J ] [ * ] [ * * ] [ ***** ] [* * *] [ * ] [ J] ");
break;
case 12: display_card_pattern.append(" [Q ] [ * ] [ * * ] [ ***** ] [* * *] [ * ] [ Q] ");
break;
case 13: display_card_pattern.append(" [K ] [ * ] [ * * ] [ ***** ] [* * *] [ * ] [ K] ");
break;
default: display_card_pattern.append(" [" + to_string(rank) + " ] [ * ] [ * * ] [ ***** ] [* * *] [ * ] [ " + to_string(rank) + "] ");
break;
}
break;
case 1: display_card_name.append("Hearts");
switch (rank)
{
case 1: display_card_pattern.append(" [A ] [ ** ** ] [* * *] [ * * ] [ * * ] [ * ] [ A] ");
break;
case 10: display_card_pattern.append(" [" + to_string(rank) + " ] [ ** ** ] [* * *] [ * * ] [ * * ] [ * ] [ " + to_string(rank) + "] ");
break;
case 11: display_card_pattern.append(" [J ] [ ** ** ] [* * *] [ * * ] [ * * ] [ * ] [ J] ");
break;
case 12: display_card_pattern.append(" [Q ] [ ** ** ] [* * *] [ * * ] [ * * ] [ * ] [ Q] ");
break;
case 13: display_card_pattern.append(" [K ] [ ** ** ] [* * *] [ * * ] [ * * ] [ * ] [ K] ");
break;
default: display_card_pattern.append(" [" + to_string(rank) + " ] [ ** ** ] [* * *] [ * * ] [ * * ] [ * ] [ " + to_string(rank) + "] ");
break;
}
break;
case 2: display_card_name.append("Diamonds");
switch (rank)
{
case 1:display_card_pattern.append(" [A ] [ * ] [ * * ] [ * * ] [ * * ] [ * ] [ A] ");
break;
case 10:display_card_pattern.append(" [10 ] [ * ] [ * * ] [ * * ] [ * * ] [ * ] [ 10] ");
break;
case 11:display_card_pattern.append(" [J ] [ * ] [ * * ] [ * * ] [ * * ] [ * ] [ J] ");
break;
case 12:display_card_pattern.append(" [Q ] [ * ] [ * * ] [ * * ] [ * * ] [ * ] [ Q] ");
break;
case 13:display_card_pattern.append(" [K ] [ * ] [ * * ] [ * * ] [ * * ] [ * ] [ K] ");
break;
default:display_card_pattern.append(" [" + to_string(rank) + " ] [ * ] [ * * ] [ * * ] [ * * ] [ * ] [ " + to_string(rank) + "] ");
break;
}
break;
case 3: display_card_name.append("Clubs");
switch (rank)
{
case 1:display_card_pattern.append(" [A ] [ * ] [* * *] [ *** ] [ * ] [ * ] [ * A] ");
break;
case 10:display_card_pattern.append(" [10 ] [ * ] [* * *] [ *** ] [ * ] [ * ] [ * 10] ");
break;
case 11:display_card_pattern.append(" [J ] [ * ] [* * *] [ *** ] [ * ] [ * ] [ * J] ");
break;
case 12:display_card_pattern.append(" [Q ] [ * ] [* * *] [ *** ] [ * ] [ * ] [ * Q] ");
break;
case 13:display_card_pattern.append(" [K ] [ * ] [* * *] [ *** ] [ * ] [ * ] [ * K] ");
break;
default:display_card_pattern.append(" [" + to_string(rank) + " ] [ * ] [* * *] [ *** ] [ * ] [ * ] [ * " + to_string(rank) + "] ");
break;
}
break;
} //end of switch suit
cout << display_card_name << display_card_pattern << endl;
}
deck.h
#ifndef deck_h
#define deck_h
#include "cards.h"
class deck
{
public:
deck(); // constructor to create deck of 52 cards
cards play_deal(); // deal card to players
void shuffle_cards(); // shuffle cards
int count_cards_left(); //count cards from left side of deck
void display_deck(); //display deck
private:
cards num_of_cards[52];
int n;
};
#endif
deck.cpp
#include <iostream>
#include <ctime>
#include <stdlib.h>
#include "deck.h"
#include "cards.h"
using namespace std;
// constructor to create deck of 52 cards
deck::deck()
{
n = 52; //size of deck is 52
int count = 0;
for (int i = 0; i < 4; i++)
{
for (int j = 1; j < 14; j++)
{
num_of_cards[count].set_cards(i, j);
count++;
}
}
}
// deal card to players
cards deck::play_deal()
{
n--;
cards card = num_of_cards[n];
return card;
}
// shuffle cards
void deck::shuffle_cards()
{
srand(time(0)); //randomly shuffling cards
int x, y;
for (int i = 0; i < n; i++)
{
x = rand() % n;
y = rand() % n;
cards card = num_of_cards[x];
num_of_cards[x] = num_of_cards[y];
num_of_cards[y] = card;
}
}
//count cards from left side of deck
int deck::count_cards_left()
{
return n;
}
//display deck
void deck::display_deck()
{
for (int i = 0; i < n; i++)
{
num_of_cards[i].display_cards();
}
}
screenshot of the output is provided via this sharable link. please follow the same
https://drive.google.com/open?id=0B482UDpVXbMvdng5UlhJXzM2WUk
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.