CPSC 120 Project #3: Design Specifications
For your Nim project you must write classes to model a Nim board
and a Nim player that meet the following specifications.
NimBoard Class
Instance Data: Three integers representing the number of sticks
in each row of the board.
Methods:
- A constructor that takes three integer parameters representing the
initial number of sticks in each row of the board.
- A method named remove that takes two integer parameters,
the first representing a row number and the second representing
a number of sticks to remove from that row. The method should
remove the given number of sticks from the given row if possible (if
the numbers are valid). Otherwise it should print an error message.
The method returns nothing.
- A method named printBoard that prints the current board
in the format specified in the project description. The method has
no parameters.
- A method named gameOver that returns true if there
are no sticks remaining on the board and false otherwise. The method
takes no parameters.
- A method named validPlay that takes two integer parameters,
the first a row number and the second the number of sticks to remove
from the given row, and returns true if the row number is valid and
there are the given number of sticks in that row. Otherwise the
method returns false. Note that this method just checks the validity
of the move - it does not remove the sticks.
- Accessor methods for each of the instance variables.
Player Class
Instance Data:
- The player's name (a String)
- The player's type - "C" means computer and "H" means human (String)
- The total number of sticks the player has removed in the current
game (an int)
- The total number of games won (int)
Methods:
- A constructor that takes two String parameters. The first is the
name of the player and the second represents the type (either a C/c or
P/p is expected). The constructor should set the type to upper case
and check to make sure it is valid. If it isn't a message should be
printed and the type for the player should be set to "C".
- A method named takeTurn that has one parameter, a NimBoard
object. The method will simulate the player taking a turn removing
sticks. The method must verify that there are sticks left to remove
and then check to see whether the player is a human or a computer. If
the player is a human if will ask the player to enter its move
(row number followed by number of sticks); if it is a computer the
method will generate a valid random move. In either case the method
should be sure the move is valid and then it should remove the
sticks from the NimBoard object that is passed in as a parameter.
It also should update the total number of sticks removed by this
player.
Note that this is where lots is going on so it would be good to
break up the work into some private helper methods.
- A method named incrementWins that adds one to the number
of wins. The method takes no parameters and returns nothing.
- A method reset that sets the total number of sticks
removed back to 0.
- Accessor methods for the instance data.
Nim Game
The Nim game will be played in the main method of a Nim class. Here you
will need to use the NimBoard and Player classes to play the game as
described on the handout. (Note: You could have an additional class
that represents the game. It would have a method to play once (among
other methods). Your main method would have the loop to play multiple
times and keep track of the statistics.)
Working Time Line
You should do the work in small chunks testing each a you go. The
portions due will be in two parts - some basic work due Saturday then
the completed program due next Friday.
Phase I (Due Saturday, November 12 by 5:00 - emailed tar file)
For Phase I have the following completed:
- The NimBoard class
- A skeleton of the Player class that contains the constructor
and accessor methods.
- A main program that tests this part. It should read in the
board size and instantiate a NimBoard. It should read in the name
and type for each player and instantiate two player objects. And,
it should have a loop that continues until the game is over (call
your method) - in the loop just read in row number and number of
sticks and remove the sticks from the board.
Phase II (Completed Project) After getting the basics correct,
you need to add the code to the Player class
for the player taking a turn and you need to write the
program to play the game. A suggested order of doing this:
- Write the takeTurn method for the Player class. (This includes
writing any private helper methods you want to use.)
Note that your
test program is basically a human taking a turn so some of that code
can be re-used in your player class.
- Modify the test program to play the game once now using the
player objects instead (which get the moves for the player) instead
of reading in the moves. Make sure this works correctly.
- Add the outer loop to the main program to
let the user play multiple times and keep track of statistics.
Extra Credit: Add an "intelligent" strategy for the computer.
To do this add a third type of the player - something to represent
an intelligent computer. Then, add code for the computer making a
choice using some intelligent algorithm. Leave the random computer
as is.