Package a5.logic
Class MNKGame
java.lang.Object
a5.logic.MNKGame
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 -
Method Summary
Modifier and TypeMethodDescriptionprotected void
Effect: advance the game turn by one.board()
Returns: the board state.protected void
Effect: change the current player to be the next.int
colSize()
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
gameType()
Returns: the type of this game.boolean
hasEnded()
Checks if there are enough consecutive stones in a row so that the game ends.int
hashCode()
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()
, andp
is empty on board.result()
Returns: The result of the game.int
rowSize()
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.toString()
-
Constructor Details
-
MNKGame
protected MNKGame(int m, int n, int k) -
MNKGame
Create a game copy.
-
-
Method Details
-
makeMove
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()
, andp
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
Returns: The result of the game. Requires: the game has ended and the result has been determined. -
setResult
Set the result of the game. Requires: game has ended. -
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
Returns: the type of this game. -
currentTurn
public int currentTurn()Returns: number of the current turn. -
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
Returns: a set of legal moves. -
countToWin
public int countToWin()Returns: the number of consecutive stones (k) to win. -
toString
-
equals
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. -
stateEqual
Returns: true if the two games have equivalent state for the purpose of deciding future moves. -
hashCode
public int hashCode()
-