A3 War!

Annoucements and Clarifications

None

Brief Description

War is a two player card game. It is played with a standard 52 card deck (see our last assignment for a description). The rules of War are listed below. Everyone who has played War knows that it is a particularly boring game, namely because there is no aspect of player strategy (there are no choices to make). Instead of playing the game interactively, the computer will play both players and display the course of the game and the outcome. Your program will also accumulate statistics about games over multiple runs.

Your design should be object-oriented using natural objects in the problem space: Queue, Stack, Card, ... You should not use any Java collection classes in your implementation (if you are unsure whether you have done this or not please ask).

Goals and Requirements

Rules:

  1. Each player is dealt 26 cards from the shuffled deck face down in front of them, this is each player's hand.
  2. Each turn:
    1. Each player turns over the top card from their hand.
    2. If the ranks of the cards are different, the player with the higher rank card takes both cards and places them on the bottom of their hand. Aces are the lowest rank card and Kings are the highest.
    3. If the ranks of the cards are the same a war is declared. Each player places three more cards from the top of their hands face down on the table. Repeat step two until until the ranks of the cards flipped over are different. At that point the player with the higher rank card takes all the cards placed on the table since the beginning of the war and places them on the bottom of their hand.
  3. Repeat step 2 until one player runs out of cards.
  4. If at any point a player runs out of cards they lose.

There is one point for variability between programs. The choice is in the order of the cards are placed on the bottom of each player's hand. That choice is up to you; you will explore modifications to this in the Questions section.

We consider infinite games (games that cycle forever) to be games that last more than 15000 turns. There may be games that end in more that 15000 turns but are not infinite, however they are only represent a small minority of the total amount of games. There are ways to check whether the game is cycling though it is beyond the scope of this project (students looking for a challenge can attempt it).

Description

Your program will have two modes of operation. In the first mode it will run a game of War displaying the turn-by-turn action. In the second mode the program will run a number of games and computing several basic statistics about those games. Your program will determine which mode to run in by examining the single command line argument: if the argument is an integer (as parseInt determines) use the second mode, otherwise the argument is considered a filename for mode 1.

Mode 1: Turn-by-turn

wolf(1)% java War output1.txt
wolf(2)% java War 1output.txt

In this mode the program will list the cards shown face up each turn. The program should not output anything to the console, instead all the output should be directed to a file specified from the commmandline. A sample output is located here. The number of |'s represent the first player's cards and the number of .'s represent the second player's cards. Try to mimic the output style shown in the sample file. If the file to be used cannot be opened or written to the program should exit and indicate the error.

Mode 2: Simulation

wolf(3)% java War 356

In this mode the program takes an integer from the user on the commandline and runs the game that many times calculating several statistics:

The only output of the program should be a list of the above statistics to the console. Note that depending on your choices for how the cards get placed on the bottom the statistics may vary. No sample run is provided to maintain the integrity of your results.

Questions to Answer

Include the answers to the following questions at the end of your README.txt file.

  1. Algorithm Analysis:
    1. For each of the following ADT methods give the asymtotic running time for the method (as you implemented it), and whether it can be improved upon.
      1. Stack - pop()
      2. Queue - enqueue()
      3. Queue - dequeue()
    2. Suppose starting from an empty queue you enqueue n items, then you dequeue n items, what is the running time of this procedure? Why? What is the average running time for each item in this procedure (this is also called amortized cost)?

  2. Simulation Analysis:
    1. For each of the statistics what value do you expect your program to return? Why?
    2. Run the simulation version of your program several times with a large number of trials each time (at least 1000). Copy the output into README.txt. Are your results consistent with each other, how do the results vary as you do more or less trials?
    3. Describe how you implemented placing cards on the bottom of each player's hand.
    4. Describe two alternate ways to have implemented that feature.
    5. Implement both of those ways and repeat your experiment from Question 2.b.
    6. Are these results consistent with each other? Do the games play fundamentally the same?

  3. In Linux there is a command called time. The time command is used to measure the time it takes for another command to run.
    wolf(20)% time java War output1.txt
    0.358u 0.396s 0:00.88 84.0%     0+0k 0+0io 1pf+0w
    

    The third number, shown in italics, is the amount of clock time it took for the War program to run, in this case it took 0.88 seconds. If you want to know more about the time command use the command man 1 time. In general to find out information about a command use man 1 command_name.

    Use the Linux time command to measure the actual running time of your simulation. You should take at least 10 measurements in the range from 1-1000 trials and plot a graph of those points. Submit an electronic copy of the graph with the rest of your program. Based on the graph what is the running time of your program as a function of number of trials?

  4. Suppose we required you to implement the check for infinite games properly, give a brief description in psuedo code of the algorithm you might use (no program code).

Commenting and Style

Handin

Please hand all necessary files into your handin directory in a subdirectory named War. Your application class should be called War.java. There should be exactly one class declared within each .java file. If your program does anything strange (bugs), awesome(extra features) or has a non-intuitive interface please include a file called README.txt which explain them. If there are bugs in your program but you do not describe them in your README you will lose more credit than if you had described them.

Hints