Using c++ For this program, The objective of this program is to create a game ca
ID: 3876215 • 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
Here is your c++ war game code:
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.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();
}
}
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.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
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
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.