Package a5.logic

Class MNKGame

java.lang.Object
a5.logic.MNKGame
Direct Known Subclasses:
Pente, TicTacToe

public abstract class MNKGame extends Object
A running (m, n, k) game. An (m, n, k) game is an abstract board game in which two players take turns in placing a stone of their color on an m-by-n board, the winner being the player who first gets k stones of their own color in a row, horizontally, vertically, or diagonally.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    MNKGame(int m, int n, int k)
     
    protected
    Create a game copy.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    Effect: advance the game turn by one.
    Returns: the board state.
    protected void
    Effect: change the current player to be the next.
    int
    Returns: the number of columns of the board.
    int
    Returns: the number of consecutive stones (k) to win.
    Returns: the current player.
    int
    Returns: number of the current turn.
    boolean
    Although MNKGame is a mutable abstraction, equality is defined in terms of the state of the object.
    abstract GameType
    Returns: the type of this game.
    boolean
    Checks if there are enough consecutive stones in a row so that the game ends.
    int
     
    Returns: a set of legal moves.
    abstract boolean
    Effect: place a stone as the current player at p, and advance to the next turn Checks: p is a valid position to place a stone, that is, 0 <= p.rowNo() < rowSize(), 0 <= p.colNo() < colSize(), and p is empty on board.
    Returns: The result of the game.
    int
    Returns: the number of rows of the board.
    void
    Set the result of the game.
    protected boolean
    Returns: true if the two games have equivalent state for the purpose of deciding future moves.
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • MNKGame

      protected MNKGame(int m, int n, int k)
    • MNKGame

      protected MNKGame(MNKGame game)
      Create a game copy.
  • Method Details

    • makeMove

      public abstract boolean makeMove(Position p)
      Effect: place a stone as the current player at p, and advance to the next turn Checks: p is a valid position to place a stone, that is, 0 <= p.rowNo() < rowSize(), 0 <= p.colNo() < colSize(), and p is empty on board. on board.
      Parameters:
      p - the position to place a stone
      Returns:
      true if the action is valid, false otherwise
    • hasEnded

      public boolean hasEnded()
      Checks if there are enough consecutive stones in a row so that the game ends. Returns: whether this game has ended. Effect: if the game has ended, determines the result of the game.
    • result

      public GameResult result()
      Returns: The result of the game. Requires: the game has ended and the result has been determined.
    • setResult

      public void setResult(GameResult r)
      Set the result of the game. Requires: game has ended.
    • board

      public Board board()
      Returns: the board state.
    • rowSize

      public int rowSize()
      Returns: the number of rows of the board.
    • colSize

      public int colSize()
      Returns: the number of columns of the board.
    • gameType

      public abstract GameType gameType()
      Returns: the type of this game.
    • currentTurn

      public int currentTurn()
      Returns: number of the current turn.
    • currentPlayer

      public PlayerRole currentPlayer()
      Returns: the current player.
    • advanceTurn

      protected void advanceTurn()
      Effect: advance the game turn by one.
    • changePlayer

      protected void changePlayer()
      Effect: change the current player to be the next.
    • legalMoves

      public List<Position> legalMoves()
      Returns: a set of legal moves.
    • countToWin

      public int countToWin()
      Returns: the number of consecutive stones (k) to win.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object o)
      Although MNKGame is a mutable abstraction, equality is defined in terms of the state of the object. This goes against our usual advice on how to implement equals(), but is needed to support use in the transposition table.
      Overrides:
      equals in class Object
    • stateEqual

      protected boolean stateEqual(MNKGame g)
      Returns: true if the two games have equivalent state for the purpose of deciding future moves.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object