CIT 591 Assignment 5: War (Card Game)
Fall 2010, David Matuszek

Purposes of this assignment

General idea of the assignment

Play a game of War. The play will be computer vs. computer; all the human does is start the game.

Besides, no human above the age of about 6 will enjoy this game. At least, not if they're sober.

The rules:

A deck of cards consists of 52 cards--13 each in four different suits. The four suits are called Spades, Hearts, Diamonds, and Clubs. Each card in a suit has a number from 1 to 13. Hence we have cards such as "the 4 of Spades" or "the 13 of Diamonds." (We do not have Ace, Jack, Queen, King cards, just numbered cards.)

There are two players, whom we will call Dick and Jane.

Here's how it goes:

  1. The deck is shuffled. This randomizes the order of the cards, but there are still 52 different cards--no card ever occurs twice.
  2. The cards are dealt. Each player gets 26 cards.
  3. Play then proceeds as follows:
    1. Each player takes the top card from his or her deck, and plays it.
    2. If one card is numerically larger than the other, the player who played the higher-numbered card takes both cards and puts them on the bottom of their deck.
    3. If the two cards are numerically equal, this is a "war." To fight a war, each player plays four more cards from the top of their deck.
      1. It doesn't matter what the first three cards are; only the last (fourth) cards matter.
      2. The fourth cards are compared, and if one is numerically larger than the other, the person playing the higher-numbered card collects all the cards and puts them on the bottom of their deck.
      3. If the two (fourth) cards are numerically equal, the "war" continues--each player plays four more cards, and the last cards played are compared.
  4. Play continues until one player has all the cards, and wins.

Your assignment:

Use Test Driven Design. You will find that almost all of these methods are extremely simple. That's okay--the idea is to get you used to writing tests first.

Write and test the following classes and methods.

How to begin

One way to begin is to create your file with some or all of the methods in it, but each method is a stub: that is, it doesn't actually do anything. Then write a test for some method, run it and make sure it fails, then alternate between working on the method and working on its test, until you are satisfied with both. Then go on to the next method.

If a method depends on other methods (or its test depends on other methods), then you should write and test those other methods first. The more independent of one another your methods are, the easier this is. Highly interdependent methods, in addition to being bad style, are much harder to test.

Just to save you a bunch of typing, I have created "skeleton" files and that you can download.


Methods that return random results, such as the shuffle method, are harder to test, but not impossible.

For shuffle, you can test:

Due date

Thursday, October 14, before midnight. Hand in one project with both your names on it; zip up the entire project directory and hand it in via Blackboard.