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.


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

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

Functional Requirements

  • Nim constructor: To set up a new game board
  • displayBoard: To display the current board
  • isGameOver: To determine whether the game is done
  • isValidMove: To determine whether a player’s move is valid
  • takeATurn: Some way of prompting user to take a turn
  • CSCI 136, Spring 2023

CS136 webpage, spring 2023

Powered by Bootstrap 4 Github Pages