A Complete Design Doc for Nim
Overview
Nim is a two-player game. There are multiple (how many?) piles of matchsticks (how high?). Each player takes a turn removing some number of matchsticks from one of the piles. The player who removes the last matchstick wins the game.
State
- Piles of matchsticks; represented as an
int[]
, with each element being the number of matchsticks in that pile - The total number of piles; represented as an
int
- The maximum and minimum number of matchsticks in each pile; each represented by an
int
- The current player; represented by an
int
- We’ll store all of this in a
Nim
class
Functions
Nim
constructor: To set up a new game boarddisplayBoard
: To display the current boardisGameOver
: To determine whether the game is doneisValidMove
: To determine whether a player’s move is validtakeATurn
: Some way of prompting user to take a turn
Procedure
The game operates as follows.
- Set up the game.
- Initialize the board randomly.
- Choose the starting player.
- Start the game.
displayBoard
[A]- Ask the current player to
takeATurn
[B]:- Prompt player to enter which pile and amount to remove
- Determine if the entered information
isValidMove
- If valid, update board using the appropriate
set
method. If not, go to [B] - Flip the player using the appropriate
set
method.
isGameOver
? If not, go to [A]
- Flip the player one more time
- Tell the current player that they won