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) and return true . Otherwise it should return false,
leaving the board unchanged.
- A method named toString that converts the current board
as a string 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.
- 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 from this NimBoard. The method must 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. If an invalid
move is attempted (by either player), the method should allow the player to retry until a valid
move is made. This method also needs to update the total number of sticks removed
by the player.
- 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.
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 Tuesday then
the completed program due next Friday.
Phase I Due Tuesday, November 15 by 3:00pm - emailed tar file
Note: 3:00pm is so that you don't interfere with the other section's lab
For Phase I, have the following completed:
- The NimBoard class
- A main program that tests this class. It should read in the
board size and instantiate a NimBoard.
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. (it doesn't distinguish
who is making the moves)
- A skeleton of the Player class that contains:
- The constructor and accessor methods
- The method headers for takeTurns, incrementWins and reset.
- Comments (in your own words) describing how each of these methods will be used.
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 (type = "I"). Then, add code for the computer making a
choice using some intelligent algorithm. This should not alter
computer (type = "C") player. Hint - for intelligent strategies, search for "NIM"
on the web.