Project 9 - EE 312 - The Game of War

In this project, you will get practice with:

You may not acquire from any source (e.g., another student or an internet site) a partial or complete solution to a problem or project that has been assigned. You may NOT show other students your solution to an assignment. You may not have another person walk you through how to solve an assignment, or walk another student through the solution. You may get help from the instructional staff. You may discuss general ideas and approaches. Review the class policy on collaboration from the syllabus. I will run plagiarism detection software on project submissions.

Make sure that you follow the EE 312 style guide. You will lose points if you do not. You must include the honor statement below in your .cpp files. You will lose points if you do not. 

Include the following at the top of each file: 

// EE 312 Project 9 -- The Game of War

/* Student information for project:
 *
 * Replace <NAME> with your name.  <--- REMOVE THIS LINE FROM YOUR HEADER
 *
 * On my honor, <NAME>, this programming project is my own work
 * and I have not provided this code to any other student.
 *
 * Name:
 * email address:
 * UTEID:
 * Section 5 digit ID:
 * Number of slip days used on this assignment.
 */

You must also include a description of the project in a comment at the top of your file. Include comments that describe each function, as well as comments that describe code chunks inside your functions, following the class style guide. As always, style and design are important and affect your project grade.

Provided files: Card.h, Card.cpp, Deck.h, Deck.cpp, Hand.h, Hand.cpp, playWar.cpp

I've described the classes you must write below.

1. The Card class

This class represents a single playing card in a standard deck of cards. A playing card has two properties: a suit and a value. We encode each of these as an integer:

Suit
Spades - 3
Hearts - 2
Diamonds - 1
Clubs - 0

Value
Ace - 1
2 - 10 represented by actual value
Jack - 11
Queen - 12
King - 13

You will use constants for the suits and values, e.g.,
const int SPADES = 3;
You must write two constructors, and an accessor method for each property. One constructor creates a random, valid card. The second constructor will accept two parameters, the suit and value of the card.

Write a toString() method which returns a string representation of the card. The returned string is of the form "Ace of Hearts" or "3 of Diamonds".

Write methods that return the suit and value as a string:
string getSuitAsString(): Returns a string representing the card's suit, e.g., "Spades", "Hearts", "Diamonds" or "Clubs"
string getValueAsString(): Returns a string representing the card's value, e.g., "Ace", "2", "Jack", "Queen".

Write methods that test to see if another card is of the same suit or value as this card.
bool sameSuit(Card& other): Return true if this card and other have the same suit
bool sameValue(Card& other): Return true if this card and other have the same value

Though not part of the class, define the operator == and the operator << for Cards.
bool operator ==(Card& c1, Card& c2);
ostream& operator <<(ostream& output, const Card& c);

2. The Deck class

The Deck class represents a standard deck of 52 playing cards. It has three instance variables:
A vector of Cards, myDeck
An integer index called top. If top is 4, that means that Cards at indexes 0 to 3 have been dealt. The next card is at index 4.
The number of Cards currently in the deck, deckSize.

Your Deck class will contain the following methods:

A constructor that takes no arguments, and initializes a deck in order by suit and rank. The cards should be in this order:
Ace of Clubs, 2 of Clubs, ..., King of Clubs, Ace of Diamonds, 2 of Diamonds, ..., King of Diamonds, Ace of Hearts, etc.

void shuffle(): this method shuffles the deck. Remember to ignore cards which have been dealt when shuffling. To shuffle the deck, repeatedly (e.g., 500 times) pick two cards in the deck at random and swap their positions. You will need to use rand() and srand(). Use the constant SHUFFLE provided in Deck.h for the number of swaps to perform.

Card dealCard(): Deal the card on top of the deck and return it

Accessor methods for instance variables top and deckSize.

string toString(): returns a string representation of the deck, with the cards in order. Use the string representation of each card, separating two such strings with a newline.

3. The Hand class

This class represents a player's hand of cards. The instance variables in this class are:

myHand, a vector of type Card
size, the number of Cards currently in the Hand
top, the index of the card at the top of the Hand

The methods in this class are:
int getSize(): accessor for size instance variable
int getTop(): accessor for top instance variable
bool isEmpty(): returns true if the hand is empty, false otherwise
void addCard(Card& c): adds c to the bottom of the hand
Card getNext(): Removes and returns the top card from the Hand. This method throws a string (a char array) if the Hand is empty.

Playing the Game of War

The card game War has the following rules:

Shuffle a deck of cards, and deal them out into two hands of 26 cards each.

Play rounds until one player is out of cards. A round consists of the following:
If a player runs out of cards at any time, that player loses the game. If both players run out of cards at the same time, the game is a draw.

In the Game of War, Aces win against any other card.

In your implementation, the program plays War until one player wins or until 5000 rounds have been played. If you stop after 5000 rounds, your program will print that the game was a draw and display the number of wars, and if the game ends because a player won, your program should indicates who lost and the number of wars.

Note that the getNext() method in the Hand class throws an exception if the hand is empty, and use that to detect when a player has lost.

To deal the cards into the two players' hands: alternate between dealing a card to player 1 and then player 2. The players add the dealt card to the bottom of their hand.

I suggest that during a war, you use Hand objects to hold each player's cards that are on the table (the cards they put down as part of the war.) Note that the order in which the cards are added to the winner's hand matters.

The main function in your program will be contained in the file playWar.cpp. You will include prototypes of helper functions that you use to play the game at the top of this file.

EXAMPLE OUTPUT
You must match the output exactly, except of course for the exact cards and their order in the Player's hands, and of course which player loses will vary.
Example Run:
Hand of Player 1:
9 of Spades
Jack of Hearts
Queen of Spades
Ace of Hearts
2 of Diamonds
4 of Clubs
5 of Clubs
King of Hearts
Ace of Clubs
4 of Diamonds
10 of Diamonds
10 of Spades
4 of Hearts
Queen of Clubs
5 of Hearts
3 of Hearts
9 of Diamonds
8 of Hearts
6 of Clubs
2 of Spades
8 of Diamonds
8 of Clubs
2 of Clubs
3 of Clubs
6 of Hearts
Queen of Diamonds

Hand of Player 2:
5 of Spades
2 of Hearts
7 of Clubs
6 of Diamonds
Jack of Spades
Jack of Diamonds
4 of Spades
Queen of Hearts
King of Diamonds
5 of Diamonds
6 of Spades
10 of Hearts
8 of Spades
10 of Clubs
King of Clubs
Jack of Clubs
7 of Diamonds
7 of Hearts
3 of Diamonds
9 of Clubs
3 of Spades
Ace of Diamonds
Ace of Spades
7 of Spades
9 of Hearts
King of Spades

Player 1 lost after 174 rounds.
There were 11 wars.


If neither player wins and the game stops because 5000 rounds have been played, the last two lines of output would be replaced with:

The game was a draw after 5000 rounds.
There were 52 wars.

For this project, use a text editor like emacs or vi, and compile and test your program on the LRC machines. Do not use CLion. 

Create a zip file a10.zip with no internal structure. Zip together Card.cpp, Deck.cpp, Hand.cpp and playWar.cpp. You must use these exact file names to receive credit. Do not modify the provided .h files, or the #includes in the .cpp files. Do not add globals to the .cpp files.

You are not allowed to use slip days on this project.