CPE 103 Extra Credit
Card game simulation: War

This is an individual assignment. You may not discuss any aspect of this problem with anyone except the instructor.

War is a card game for two players that is played with a standard 52 card deck. Cards rank as usual from high to low: K Q J T 9 8 7 6 5 4 3 2 A. Suits are ignored in this game. The object of the game is to win all the cards.

The deck is shuffled and all the cards are dealt so that each player has 26. Players do not look at their cards, but keep them in a packet face down. 

Both players now turn their top card face up and put them on the table. Whoever turned the higher card takes both cards and adds them (face down) to the bottom of their packet. Then both players turn up their next card and so on.

If the turned up cards are equal there is a war. The tied cards stay on the table and both players play the next card of their pile face down and then another card face-up. Whoever has the higher of the new face-up cards wins the war and adds all six cards face-down to the bottom of their packet. If the new face-up cards are equal as well, the war continues: each player puts another card face-down and one face-up. The war goes on like this as long as the face-up cards continue to be equal. As soon as they are different the player of the higher card wins all the cards in the war.

The game continues until one player has all the cards and wins. This can take a long time.

If a player runs out of cards during a war they lose. If neither player has enough cards, the one who runs out first loses. If both run out simultaneously, it's a draw. Example: Players A and B both play sevens, so there is a war. Each player plays a card face down, but this is player B's last card. Player A wins, since player B does not have enough cards to fight the war.

This game requires no strategy by the players and the outcome is predetermined once the cards have been dealt.

Write a Java program to simulate a game of War between two players.  The output may be a simple console display.  At each turn display each player's face up cards and announce the winner.  Announce when a war occurs.  Show the number of cards in each player's hand after each turn.  When the game is over, announce the winner and how many turns the game lasted.

Your design should have classes that represent the different elements of the problem, for example:  a Card, a Deck of cards, a player's Hand, the Table (or Game), and a Console (or Display).   Your solution should make effective use of Stacks and/or Queues. (You may use Java's version of these ADT's.) The classes representing the game logic must do no input or output;  all the I/O must appear in the Console class.   To facilitate testing, it is permitted to have some mechanism to provide the game with a pre-ordered deck.

You must have JUnit tests of your classes.  You must have system tests for the three game over conditions:  Player A wins, Player B wins, and a Draw game.

Deadline May 5.

FAQ


Q: When we add face-down cards to the bottom of our packet, must these cards be in order? If so, in what order?
A: Yes. When a player wins, they take all the opponents discards (face-up) and place them on top of their own discards,
then turn the resulting stack face-down and place it on the bottom of their hand.

Q: Because a game can take a long time, my system test output is many pages long.  Do I have to print the entire thing?
A: No, for each system test case that is longer than one page, just print the first and last page of output.