Class NimState

java.lang.Object
games.nim.NimState
All Implemented Interfaces:
common.State<Integer,NimState>, common.util.Copyable<NimState>, game.State<Integer,NimState>

public class NimState extends Object implements game.State<Integer,NimState>
Represents the states of the Nim variant called the subtraction game. In this game, two players remove objects from a pile of objects in alternating turns. On each turn, at least one object must be removed; however, no more than k objects can be removed (k is a positive integer). The winner of the game is the player who takes the last object.
See Also:
  • Nested Class Summary

    Nested classes/interfaces inherited from interface game.State

    game.State.Player, game.State.Status
  • Constructor Summary

    Constructors
    Constructor
    Description
    NimState(int numberOfObjects, int limit)
    Creates a NimState instance with the specified number of objects.
    Creates a deep copy of the specified NimState object.
  • Method Summary

    Modifier and Type
    Method
    Description
     
     
    game.State.Player
    Returns the player who moves next.
    game.State.Status
    Returns the status of the game.
    boolean
    Returns whether the game is over.
    boolean
    Returns whether it is allowed to remove the specified number of objects.
    void
    Removes the specified number of objects.
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface game.State

    isWinner
  • Constructor Details

    • NimState

      public NimState(int numberOfObjects, int limit)
      Creates a NimState instance with the specified number of objects.
      Parameters:
      numberOfObjects - the initial number of objects, must be positive
      limit - upper limit for the number of objects to be removed, must be positive
    • NimState

      public NimState(NimState other)
      Creates a deep copy of the specified NimState object.
      Parameters:
      other - the object to be copied
  • Method Details

    • isLegalMove

      public boolean isLegalMove(Integer n)
      Returns whether it is allowed to remove the specified number of objects.
      Specified by:
      isLegalMove in interface common.State<Integer,NimState>
      Parameters:
      n - the number of objects to be removed
      Returns:
      whether it is allowed to remove the specified number of objects
    • makeMove

      public void makeMove(Integer n)
      Removes the specified number of objects.
      Specified by:
      makeMove in interface common.State<Integer,NimState>
      Parameters:
      n - the number of objects to be removed
    • getLegalMoves

      public Set<Integer> getLegalMoves()
      Specified by:
      getLegalMoves in interface common.State<Integer,NimState>
    • getNextPlayer

      public game.State.Player getNextPlayer()
      Returns the player who moves next.
      Specified by:
      getNextPlayer in interface game.State<Integer,NimState>
      Returns:
      the player who moves next
    • isGameOver

      public boolean isGameOver()
      Returns whether the game is over.
      Specified by:
      isGameOver in interface game.State<Integer,NimState>
      Returns:
      whether the game is over
    • getStatus

      public game.State.Status getStatus()
      Returns the status of the game.
      Specified by:
      getStatus in interface game.State<Integer,NimState>
      Returns:
      the status of the game
    • copy

      public NimState copy()
      Specified by:
      copy in interface common.util.Copyable<NimState>
      Specified by:
      copy in interface common.State<Integer,NimState>
    • toString

      public String toString()
      Overrides:
      toString in class Object