CS1004 Homework #6
Due on Monday, May 2, 2005 at 11:59:59pm, only ONE late day accepted

There are two parts to this homework: a written component worth 11 points, and programming component worth 14 points.  Submission instructions are available hereMake sure to note the different deadline and late policy for this homework above.  Since there is no class on that day, written homeworks can be submitted to the CS building before it closes at 5pm, to 608 CEPSR before it closes at 5:30pm, or electronically along with your programming assignment.

Written questions

Note that problems assigned from Schneider/Gersting or Lewis/Loftus are the exercise problems at the end of each chapter, not the practice problems or self-review questions.  (The practice/self-review problems are optional, and solutions for them are provided in the book.  For obvious reasons, the solutions for the exercises are not. ;-))

  1. (3 points) Lewis/Loftus exercise 6.11.
  2. (8 points) Game design for the programming assignment (see below).

Design and Programming: The Card Game "War"

As described below, you will submit the design part of this assignment as the written problem #2, and the implementation part of this assignment electronically (including commented source, README, and a typescript).

The Game

The game of War is a classic card game, one that's popular with kids for an important reason: it's deterministic.  That is, there is no thinking or strategy involved, just luck and probabilities.  Here's a summary of the game on Wikipedia, but to make everyone's implementation consistent, the rules we'll follow are outlined here.

Your Design (and Our Requirements)

As we promised earlier, you have the opportunity to design your program to conform to the above rules -- that is, you decide what variables and methods go in each class.  There are a few additional requirements that we will impose upon you, however:

Before you actually implement this design, you must first document it.  This will serve as question #2 in your written section.  The written portion must have two parts:

  1. (4 points) A UML diagram or bullet-list outlining every class and the variables/methods of that class.  Briefly (one sentence each) describe every variable and method.
  2. (4 points) A psuedocode description of the execution of the game itself (i.e., only the part of the main method that runs the game once the deck has been dealt).  You should try to be as precise as possible.  For full credit, make sure to set up the loops for both battles (2 points) and wars (2 points) so that they run as per the rules above.

You may find that, during implementation, your design will change a little bit.  This is normal, but if you do make changes, include a few sentences at the end of the design document pointing out your changes and why you made them.

Your Implementation

Here's a point breakdown of the implementation.

You may find the following hints helpful when designing and implementing the game.

Sample Execution

Here's an execution of our game.  Your game should look similar to this one, but since the design is up to you, it doesn't have to be completely identical.  However, make sure to design and implement the requirements as stated above.

$ java Game
Player A initial hand: 26 cards: [2C|2S|2D|QD|3H|KH|3D|QC|6D|JC|8C|QS|4S|JD|4D|QH|7C|5H|5C|2H|4H|AC|9C|8D|10S|4C]
Player B initial hand: 26 cards: [5S|AH|7S|9D|9H|10D|KS|8H|6H|KC|JH|7D|5D|7H|KD|9S|AD|6C|AS|3C|10C|6S|3S|10H|8S|JS]
*** Playing battle 1
Player A has 2C, player B has 5S
Player B wins
*** Battle results:
Player A has 25 cards: [2S|2D|QD|3H|KH|3D|QC|6D|JC|8C|QS|4S|JD|4D|QH|7C|5H|5C|2H|4H|AC|9C|8D|10S|4C]
Player B has 27 cards: [AH|7S|9D|9H|10D|KS|8H|6H|KC|JH|7D|5D|7H|KD|9S|AD|6C|AS|3C|10C|6S|3S|10H|8S|JS|2C|5S]
Hit enter for next battle, q to quit, or a number of battles:
*** Playing battle 2
Player A has 2S, player B has AH
Player B wins
*** Battle results:
Player A has 24 cards: [2D|QD|3H|KH|3D|QC|6D|JC|8C|QS|4S|JD|4D|QH|7C|5H|5C|2H|4H|AC|9C|8D|10S|4C]
Player B has 28 cards: [7S|9D|9H|10D|KS|8H|6H|KC|JH|7D|5D|7H|KD|9S|AD|6C|AS|3C|10C|6S|3S|10H|8S|JS|2C|5S|2S|AH]
Hit enter for next battle, q to quit, or a number of battles:
*** Playing battle 3
Player A has 2D, player B has 7S
Player B wins
*** Battle results:
Player A has 23 cards: [QD|3H|KH|3D|QC|6D|JC|8C|QS|4S|JD|4D|QH|7C|5H|5C|2H|4H|AC|9C|8D|10S|4C]
Player B has 29 cards: [9D|9H|10D|KS|8H|6H|KC|JH|7D|5D|7H|KD|9S|AD|6C|AS|3C|10C|6S|3S|10H|8S|JS|2C|5S|2S|AH|2D|7S]
Hit enter for next battle, q to quit, or a number of battles:
*** Playing battle 4
Player A has QD, player B has 9D
Player A wins
*** Battle results:
Player A has 24 cards: [3H|KH|3D|QC|6D|JC|8C|QS|4S|JD|4D|QH|7C|5H|5C|2H|4H|AC|9C|8D|10S|4C|QD|9D]
Player B has 28 cards: [9H|10D|KS|8H|6H|KC|JH|7D|5D|7H|KD|9S|AD|6C|AS|3C|10C|6S|3S|10H|8S|JS|2C|5S|2S|AH|2D|7S]
Hit enter for next battle, q to quit, or a number of battles:
*** Playing battle 5
Player A has 3H, player B has 9H
Player B wins
*** Battle results:
Player A has 23 cards: [KH|3D|QC|6D|JC|8C|QS|4S|JD|4D|QH|7C|5H|5C|2H|4H|AC|9C|8D|10S|4C|QD|9D]
Player B has 29 cards: [10D|KS|8H|6H|KC|JH|7D|5D|7H|KD|9S|AD|6C|AS|3C|10C|6S|3S|10H|8S|JS|2C|5S|2S|AH|2D|7S|3H|9H]
Hit enter for next battle, q to quit, or a number of battles:
*** Playing battle 6
Player A has KH, player B has 10D
Player A wins
*** Battle results:
Player A has 24 cards: [3D|QC|6D|JC|8C|QS|4S|JD|4D|QH|7C|5H|5C|2H|4H|AC|9C|8D|10S|4C|QD|9D|KH|10D]
Player B has 28 cards: [KS|8H|6H|KC|JH|7D|5D|7H|KD|9S|AD|6C|AS|3C|10C|6S|3S|10H|8S|JS|2C|5S|2S|AH|2D|7S|3H|9H]
Hit enter for next battle, q to quit, or a number of battles:
*** Playing battle 7
Player A has 3D, player B has KS
Player B wins
*** Battle results:
Player A has 23 cards: [QC|6D|JC|8C|QS|4S|JD|4D|QH|7C|5H|5C|2H|4H|AC|9C|8D|10S|4C|QD|9D|KH|10D]
Player B has 29 cards: [8H|6H|KC|JH|7D|5D|7H|KD|9S|AD|6C|AS|3C|10C|6S|3S|10H|8S|JS|2C|5S|2S|AH|2D|7S|3H|9H|3D|KS]
Hit enter for next battle, q to quit, or a number of battles:
*** Playing battle 8
Player A has QC, player B has 8H
Player A wins
*** Battle results:
Player A has 24 cards: [6D|JC|8C|QS|4S|JD|4D|QH|7C|5H|5C|2H|4H|AC|9C|8D|10S|4C|QD|9D|KH|10D|QC|8H]
Player B has 28 cards: [6H|KC|JH|7D|5D|7H|KD|9S|AD|6C|AS|3C|10C|6S|3S|10H|8S|JS|2C|5S|2S|AH|2D|7S|3H|9H|3D|KS]
Hit enter for next battle, q to quit, or a number of battles:
*** Playing battle 9
Player A has 6D, player B has 6H
WAR!
Player A has 4S, player B has 5D
Player B wins
Giving player B the warchest!
*** Battle results:
Player A has 19 cards: [JD|4D|QH|7C|5H|5C|2H|4H|AC|9C|8D|10S|4C|QD|9D|KH|10D|QC|8H]
Player B has 33 cards: [7H|KD|9S|AD|6C|AS|3C|10C|6S|3S|10H|8S|JS|2C|5S|2S|AH|2D|7S|3H|9H|3D|KS|6D|6H|JC|KC|8C|JH|QS|7D|4S|5D]
Hit enter for next battle, q to quit, or a number of battles:
*** Playing battle 11
Player A has JD, player B has 7H
Player A wins
*** Battle results:
Player A has 20 cards: [4D|QH|7C|5H|5C|2H|4H|AC|9C|8D|10S|4C|QD|9D|KH|10D|QC|8H|JD|7H]
Player B has 32 cards: [KD|9S|AD|6C|AS|3C|10C|6S|3S|10H|8S|JS|2C|5S|2S|AH|2D|7S|3H|9H|3D|KS|6D|6H|JC|KC|8C|JH|QS|7D|4S|5D]
Hit enter for next battle, q to quit, or a number of battles: 1000
*** Playing battle 12
Player A has 4D, player B has KD
Player B wins
*** Playing battle 13
Player A has QH, player B has 9S
Player A wins
*** Playing battle 14
Player A has 7C, player B has AD
Player B wins
*** Playing battle 15
Player A has 5H, player B has 6C
Player B wins

{And so on; removed for brevity}

*** Playing battle 1037
Player A has 9D, player B has 4S
Player A wins
*** Playing battle 1038
Player A has 3S, player B has QC
Player B wins
*** Playing battle 1039
Player A has 10C, player B has 7D
Player A wins
*** Battle results:
Player A has 25 cards: [9C|KH|6C|8C|5S|10S|3C|AD|7C|6S|4D|KC|3D|QS|5H|JS|2C|JC|5D|10H|6D|9D|4S|10C|7D]
Player B has 27 cards: [10D|2D|QH|8D|AH|JH|AC|2S|KS|4H|QD|7H|KD|5C|7S|8H|9S|2H|JD|6H|AS|3H|9H|4C|8S|3S|QC]
Hit enter for next battle, q to quit, or a number of battles: 10000

{Yet more games ensue; removed for brevity}

*** Playing battle 2690
Player A has 3D, player B has AC
Player B wins
*** Playing battle 2691
Player A has KH, player B has 3S
Player A wins
*** Playing battle 2692
Player A has 8D, player B has 8C
WAR!
Player B doesn't have enough cards for war, A wins
Over 2692 battles and 67 wars:
Player A is the winner!
$