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
Functions
printPiles
: To display the current boardsetupGame
: To set up a new game boarddone
: To determine whether the game is donevalidMove
: To determine whether a player’s move is validpromptMovePile
: To ask the player which pile they want to remove matchsticks frompromptMoveNum
: To ask the player how many they want to remove from a pile
Procedure
The game operates as follows.
- Set up the game.
- Choose the starting player.
- Start the game.
- Ask the current player `promptMovePile`
- Ask the current player `promptMoveNum`
- Determine if this is a `validMove`
- If yes, update board
- If no, go back to a.
- Flip the player.
- Are we `done`? If not, go to a.
- Flip the player again
- Tell the current player that they won