The game of war is a card game played by children and budding computer scientists.

From Wikipedia: The objective of the game is to win all cards [Source: Wikipedia].

There are different interpretations on how to play The Game of War, so we will specify our SMU rules below:

1) 52 cards shuffled and split evenly amongst two players (26 each)

  • The 26 cards to placed into a "to play" pile

2) Each player draws a card from the top of their "to play" pile and placed in a currently in play pile between the two players.

  • The player who drew the higher card (cards are ranked from 2 to ace-high) gets to keep both cards and the cards will be placed into their "played" pile.
    • In the event of a tie, players continue drawing cards until there is no longer a tie. Once the tie is broken, the winning player gets to keep all the cards from the "currently in play" pile.
  • When a player runs out of cards from their "to play" pile, they will take all the cards from their played pile, shuffle them, and then put those cards into their to play pile.
  • When a player runs out of cards from both their "played" pile and their to play pile, then they lose the game.
    • Note that in the unlikely event that there are 26 draws in a row, then both players lose the game.

Implementation Details

  • You may not access any member variables directly.
    • You must use "get" and set functions
  • You may ONLY use old-school c-style arrays. You may not use the < array> class, < vector>, < stack>, the < algorithm> library, or any other built-in c++ data structures.
  • All card objects must be dynamically allocated on the heap.
    • It is ok for arrays of pointers to cards to exist on the stack.
    • All dynamically allocated memory must be appropriately deleted/destroyed (in a destructor perhaps).
  • Consider using enum for suit and rank
  • You must implement your own shuffle algorithm
    • You won't be graded on algorithm performance (big-O). Instead, it should just be a 'meaningful shuffle.
  • You should use at least three classes.
    • In the reference implementation, classes were used for "GameBoard", Card, Pile, and Player, but you can implement your own classes as you see fit.
    • Your class definitions should each be in their own .h file and the implementations should be in their own .cpp file.

Hints

The following Pile class was created in the reference implementation. Feel free to take advantage of it.

You may notice that it implements a version of a stack

class Pile{
public:
Pile(){
numCards = 0;
}
void AddCardToPile(Card * card){
// adds a card to the end of the array
cards[numCards] = card;
numCards++;
}
Card * RemoveTopCard(){
// removes a card from the end of the array
Card * cardToReturn = cards[numCards - 1];
numCards--;
return cardToReturn;
}
int GetNumCards(){
return numCards;
}
void Shuffle(){
// TODO
}
void PrintCards(){
// TODO
}
private:
Card * cards[MAXCARDS]; // an array of card pointers
int numCards;
};

Game of War Part 2

In program 4, you created the Game of War using a custom stack implementation.

For program 6, your assignment is to refactor your code such that you use the ADT instead of a custom implementation.

Implementation Details

  • Your cards must still be allocated on the heap
  • The stack object may exist on the call stack or the heap
  • Any memory you create must be appropriately destroyed
  • A reference implementation of program 4 has been provided. You may either refactor that program or your original submission.

The reference implementation can be found here: https://github.com/egabrielsen/CSE1342- Spring2019/tree/master/Program6Reference

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.