Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

War is a card game played by 2 people. https://en.wikipedia.org/wiki/War_(card_game) MUST HAVE IMPLEMNTATION OF FIVE FILES. Shuffle with srand using the time for randomization. Can

War is a card game played by 2 people. https://en.wikipedia.org/wiki/War_(card_game)

MUST HAVE IMPLEMNTATION OF FIVE FILES.

Shuffle with srand using the time for randomization.

Can only use #include random #include iterator #include iostream #include vector #include string #include ctime(to seed srand) #include algorithm and implemented header files

Shuffle MUST BE RANDOMIZED with

-- To shuffle an array a of n elements (indices 0..n-1): for i from n1 downto 1 do j  random integer such that 0  j  i exchange a[j] and a[i] 

________________________________________________________________

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.

________________________________________________________________

What to Submit:

main.cpp - Main War Game

card.h - Header for Card

card.cpp - Implementation for Card

deck.h - Header for Deck

deck.cpp - Implementation for Deck

________________________________________________________________

card.cpp

#include

#include

#include "card.h"

using namespace std;

//default constructor

card::card() {

}

//alternate constructor

card::card(Suit a, Rank b) {

suit_ = a;

rank_ = b;

}

//gets the rank

Rank card::getRank() const {

return rank_;

}

//gets the suit

Suit card::getSuit() const {

return suit_;

}

//sets the rank

void card::setRank(Rank a) {

rank_ = a;

}

//sets the suit

void card::setSuit(Suit a) {

suit_ = a;

}

//uses a switch statement to set the name of the suit and the other which sets the rank to a string variable to be printed out as well.

ostream & operator<<(ostream& os, const card &myCard) {

string sname, rname;

if (myCard.getSuit() == HEARTS)

{

sname = "H";

}

else if(myCard.getSuit() == CLUBS)

{

sname = "C";

}

else if(myCard.getSuit() == SPADES)

{

sname = "S";

}else if(myCard.getSuit() == DIAMONDS)

{

sname = "D";

}

if (myCard.getRank()== TWO)

{

rname = "2";

}

else if (myCard.getRank() == THREE)

{

rname = "3";

}

else if (myCard.getRank() == FOUR)

{

rname = "4";

}

else if (myCard.getRank() == FIVE)

{

rname = "5";

}

else if (myCard.getRank() == SIX)

{

rname = "6";

}

else if (myCard.getRank() == SEVEN)

{

rname = "7";

}

else if (myCard.getRank() == EIGHT)

{

rname = "8";

}

else if (myCard.getRank() == NINE)

{

rname = "9";

}

else if (myCard.getRank() == TEN)

{

rname = "10";

}

else if (myCard.getRank() == JACK)

{

rname = "J";

}

else if (myCard.getRank() == QUEEN)

{

rname = "Q";

}

else if (myCard.getRank() == KING)

{

rname = "K";

}

else if (myCard.getRank() == ACE)

{

rname = "A";

}

os << sname << rname;

sname.clear();

rname.clear();

return os;

}

//compares equality between the ranks of two different cards.

bool operator ==( card a , card b) {

if (a.getRank() == b.getRank()) {

return true;

}

else {

return false;

}

}

//compares two cards using the cards ranks.

bool operator <( card a, card b) {

if (a.getRank() < b.getRank()) {

return true;

}

else {

return false;

}

}

//compares two cards using the cards ranks.

bool operator >( card a, card b) {

if (a.getRank() > b.getRank()) {

return true;

}

else {

return false;

}

}

card.h

#ifndef _WAR_CARDS_

#define _WAR_CARDS_

#include

#include

#include

using namespace std;

//enum to create all of the ranks and suits.

enum Rank { TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE };

enum Suit {CLUBS, SPADES, DIAMONDS, HEARTS};

class card {

private:

Rank rank_;

Suit suit_;

public:

//default constructor

card() ;

//constructs a card with parameters set already

card(Suit a, Rank b);

//sets the rank

void setRank(Rank a);

//sets the suit

void setSuit(Suit a);

//gets the rank

Rank getRank() const;

//gets the suit

Suit getSuit() const;

};

//to output to the console

ostream & operator<<(ostream&a, card const &b);

//to check if it is equal

bool operator ==( card a, card b);

//to check if it is less

bool operator <( card a, card b);

//to check if it is greater than.

bool operator >( card a, card b);

#endif

________________________________________________________________

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.

-- To shuffle an array a of n elements (indices 0..n-1): for i from n1 downto 1 do j  random integer such that 0  j  i exchange a[j] and a[i] 

You can get random numbers by using the rand command.

________________________________________________________________

example console output:

AC vs 5S QH vs 5C 5D vs 3D JH vs AS KS vs 9C 10H vs 2D 7H vs 6D 6C vs 3H JS vs 6H QC vs 4C AH vs 6S 8D vs 2H KC vs QS 8S vs 10S 2S vs AD KH vs 9S 9D vs JC 10D vs 3C 7D vs 9H 8H vs 8C WAR!! KD vs 7C JD vs 7S 3S vs 10C 4H vs 5H 4D vs QD 5S vs JH AC vs AS WAR!! 5C vs 8S 3D vs 2S 5D vs AD 9C vs 9D WAR!! KS vs JC 10H vs 9H 6D vs 3S 7H vs 10C 3H vs 4H 6C vs 5H 6H vs 4D JS vs QD 4C vs 5S QC vs JH 6S vs 5C AH vs QH 2H vs 8S 8D vs 10S QS vs AC KC vs AS 9S vs 5D KH vs AD 3C vs 7H 10D vs 10C WAR!! KD vs 3H 7C vs JS 2C vs QD 8C vs 4C 8H vs 5S 7S vs 2H JD vs 8S 2S vs 8D 3D vs 10S KS vs QS 2D vs AC JC vs KC 7D vs AS 9D vs KH 9C vs AD 9H vs 3C 10H vs 7H 3S vs 7C 6D vs JS 5H vs 2C 6C vs QD 4D vs 2S 6H vs 8D JH vs 3D QC vs 10S 5C vs 2D 6S vs AC QH vs JC AH vs KC 5D vs 7D 9S vs AS KD vs 9D 4S vs KH 3H vs 9C 4H vs AD 10C vs 3S 10D vs 7C 4C vs 6D 8C vs JS 5S vs 6C 8H vs QD 2H vs 6H 7S vs 8D 8S vs 6S JD vs AC QS vs 5D KS vs 7D 3C vs 9S 9H vs AS 7H vs 4S 10H vs KH 2C vs 3H 5H vs 9C 2S vs 4H 4D vs AD 3D vs 4C JH vs 6D 10S vs 8C QC vs JS 2D vs 5S 5C vs 6C JC vs 8H QH vs QD WAR!! KC vs 2H 9D vs 7S KD vs 8D 3S vs JD 10C vs AC 7C vs 3C 10D vs 9S 6S vs 9H 8S vs AS 5D vs 10H QS vs KH 7D vs 2C KS vs 3H 4S vs 5H 7H vs 9C 6D vs 2S JH vs 4H 8C vs 4D 10S vs AD JS vs 3D QC vs 4C 8H vs 2D JC vs 5S KC vs 5C AH vs 6C 2H vs 3S 6H vs JD QD vs 10C QH vs AC 7S vs 6S 9D vs 9H WAR!! 8D vs 8S WAR!! 3C vs 5D 9S vs QS 10D vs KH 2C vs 4S 7D vs 5H 3H vs 7H KS vs 9C 2S vs 10S 6D vs AD 4H vs 2H JH vs 3S 4D vs 6H 8C vs JD 3D vs QH JS vs AC 4C vs 3C QC vs 7C 2D vs 5D 8H vs 10H 5S vs 8D JC vs KD 5C vs 8S KC vs AS 6C vs 9D AH vs 9H 10C vs 9S QD vs QS WAR!! 6S vs 10D 5H vs 2C 7D vs 4S 9C vs 3H KS vs 7H 2H vs 2S WAR!! 4H vs 10S JH vs AD 3C vs 4D 4C vs 6H 7C vs 8C QC vs JD 9H vs 3D AH vs QH 9S vs JS 10C vs AC 2C vs 2D WAR!! 5H vs 5D WAR!! 7D vs 10H 9C vs 8D 7H vs JC KS vs KD WAR!! JD vs 5C 3D vs KC 9H vs AS QH vs 6C AH vs 9D 8D vs 6S 9C vs 7S JD vs 10D QC vs KH 5C vs QD 8S vs QS KD vs 4H KS vs 3S 6C vs 10S QH vs 6D 9D vs 2H AH vs 2S 6S vs JH 8D vs AD 7S vs 3C 9C vs 4D 10D vs 4C JD vs 6H 4H vs 7C KD vs 8C 3S vs 9S KS vs JS 6D vs 10C QH vs AC 2H vs 7D 9D vs 3H 2S vs 10H AH vs 5S 3C vs 5H 7S vs 4S 4D vs 5D 9C vs 8H 4C vs 2C 10D vs 2D 6H vs 7H JD vs JC WAR!! 8C vs 3D JS vs 9H KS vs AS 3H vs QC 9D vs KH 5S vs 5C WAR!! AH vs QD 7S vs QS 8H vs 6C 9C vs 10S 2C vs 6S 4C vs JH 2D vs 8D 10D vs AD 8C vs 4H KD vs 7C 3D vs 3S WAR!! KC vs 9S JD vs 10C 9H vs QH JS vs AC AH vs 2H 4S vs 7D QD vs 2S 8S vs 10H 5C vs 3C 5S vs 5H WAR!! 6C vs 4D 4H vs 6H 8C vs 7H 7C vs KS KD vs AS KC vs 3H JC vs QC 9S vs 9D WAR!! 6D vs KH 3D vs QS 10C vs 9C JD vs 10S 2H vs 2C WAR!! AH vs 6S QD vs JH 3C vs 2D 5C vs 8D 6C vs 10D 8H vs AD 4D vs 9H 5D vs QH 5H vs JS 5S vs AC 7H vs 4S 8C vs 7D 3H vs 8S KC vs 10H 9C vs 4H 10C vs 6H 10S vs 7C JD vs KS AH vs KD 2S vs AS 6S vs JC 4C vs QC 2C vs 6D 2H vs 3S JH vs KH QD vs 7S 2D vs 9S 3C vs 9D 4S vs 3D 7H vs QS 7D vs 5C 8C vs 8D WAR!! 10H vs 6C 4H vs 8H 9C vs AD 6H vs 4D 10C vs 9H 7C vs 5D 10S vs QH KD vs 5H AH vs JS 7S vs 5S QD vs AC 3D vs 3H WAR!! 4S vs 8S 7D vs KS 10H vs 2S KC vs AS 6C vs 6S WAR!! 10D vs JC 8C vs QC 4D vs 2C 6H vs 6D WAR!! 9H vs 2H 5D vs JH 7C vs KH 5H vs 2D KD vs 9S JS vs 3C AH vs 9D 5S vs 7H 7S vs QS 2S vs 4H 10H vs 8H 2C vs 9C 4D vs AD 9H vs 10S 10C vs QH 2H vs QD 3S vs AC 6D vs 4S 6H vs 5C 2D vs 8S 5H vs JD 9S vs 3D KD vs 3H 3C vs 7D JS vs KS 9D vs KC AH vs AS WAR!! 8H vs 10D 4S vs JC 6D vs 4C 5C vs 6C 6H vs 6S WAR!! 3D vs 8C 3H vs 5D KD vs JH 4C vs 7C 6D vs KH JH vs 5S KD vs 7H 5S vs 7S JH vs QS 7H vs 2S KD vs 4H 2S vs 2C WAR!! 7H vs 9C KD vs AD Player 2 Wins! 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications 27th International Conference Dasfaa 2022 Virtual Event April 11 14 2022 Proceedings Part 2 Lncs 13246

Authors: Arnab Bhattacharya ,Janice Lee Mong Li ,Divyakant Agrawal ,P. Krishna Reddy ,Mukesh Mohania ,Anirban Mondal ,Vikram Goyal ,Rage Uday Kiran

1st Edition

3031001257, 978-3031001253

More Books

Students also viewed these Databases questions

Question

Guidelines for Informative Speeches?

Answered: 1 week ago