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 board
  • setupGame: To set up a new game board
  • done: To determine whether the game is done
  • validMove: To determine whether a player’s move is valid
  • promptMovePile: To ask the player which pile they want to remove matchsticks from
  • promptMoveNum: To ask the player how many they want to remove from a pile

Procedure

The game operates as follows.

  1. Set up the game.
  2. Choose the starting player.
  3. Start the game.
    1. Ask the current player `promptMovePile`
    2. Ask the current player `promptMoveNum`
    3. Determine if this is a `validMove`
    4. If yes, update board
    5. If no, go back to a.
    6. Flip the player.
    7. Are we `done`? If not, go to a.
  4. Flip the player again
  5. Tell the current player that they won
  • CSCI 136, Spring 2022

Website for CSCI 136, Spring 2022 (instructors: Sam McCauley and Dan Barowy)

Powered by Bootstrap 4 Github Pages