CPSC 120 Assignment #3: Reverse Nim

Due by Friday, November 19, 2010 at 4:00 pm

In this assignment you will write a program to play the game of Nim (actually, reverse Nim). The game has two players and a "board" that consists of three rows of sticks. In his or her turn, a player removes any number of sticks from one row of the board. The player who clears the board of sticks after a move loses the game. (NOTE: In some versions the player who removes the last stick wins but we are doing the reverse.)

Your program should prompt for and read in the names of each player. It should then prompt the user for the number of sticks in each row to set up the Nim board (just one number - each row will start with the same number of sticks). The player whose name was entered first will take the first turn. The player will need to enter two integers for a turn - the first will be the number of the row they want to remove sticks from and the second will be the number of sticks to remove from that row. After each player makes a move the board should be displayed in a format similar to the following:


Row 1: | | |
Row 2: | |
Row 3: | | | | | |

The players continue taking turns until no sticks remain. The program should keep track of the number of rounds the game took (that is the number of times the players took a turn) and the total number of sticks removed by each player. At the end of a game it should print the name of the winner along with the number of rounds and the number of sticks removed for each of the players.

After each game the program should ask if the user wants to play again. If so, ask again about the number of sticks in each row (but assume the players will be the same) to set up a new board. The program should keep track of overall statistics - how many times each player won, the average number of rounds needed to complete the game, and both the minimum number and the maximum number of rounds. These statistics should be printed at the end of the program.

Your program must use a class to represent the Nim board. Since the important aspect of a Nim board is how many sticks are in each row, we can represent the board by 3 integers. Hence the there should be three instance variables of type int. The class must provide at least the following methods (you may include additional ones if you wish).

Implementation: Do your work incrementally, testing as you go. First get the class correct and a program to play one game. After that is correct enclose the code to play one game in a loop to let the user continue playing. Add the code to collect statistics (average number of rounds, max, and min) and to print the results.

Requirements: As always, your program should:

Grading: As usual your program will be graded on both style and correctness with very little credit for a program that does not compile or run.

Academic Integrity Reminder: Programming assignments are to be your own work. You may get help on the specifics of the assignment from no one except the instructor. You may not show your program to anyone or look at anyone's program or share ideas with anyone about how to write the program.

Submission: Tar your directory (your work should be in a subdirectory of your assignment directory), naming the tar file assn3yourname.tgz and copy it to ~ingram/CPSC120/yourname.