Class TicTacToe
java.lang.Object
net.flectone.pulse.module.command.tictactoe.model.TicTacToe
A game of tic-tac-toe, including the moves played and who is to move next.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe board value marking the first player's move.static final intApplied to a board value to mark the move that hard mode is about to drop.static final intThe board value marking the second player's move.static final intAdded to a board value to mark a square that is part of the winning line. -
Constructor Summary
ConstructorsConstructorDescriptionTicTacToe(int id, int firstPlayer, int secondPlayer, boolean hard) Creates a game. -
Method Summary
Modifier and TypeMethodDescriptionvoidLooks for a completed line and records it, so it can be highlighted on the board.booleanWhether a player is in this game.int[][]getField()intintgetId()intintint[]booleanbooleanisDraw()Whether the board is full with no line completed.booleanisEnded()booleanisHard()booleanisWin()Whether the last move completed a line.booleanPlays a square, and in hard mode drops that player's oldest move.voidsetEnded(boolean ended) voidPasses the turn to the other player.voidsetNextPlayer(int nextPlayer) toString()
-
Field Details
-
FIRST_VALUE
public static final int FIRST_VALUEThe board value marking the first player's move.- See Also:
-
SECOND_VALUE
public static final int SECOND_VALUEThe board value marking the second player's move.- See Also:
-
REMOVE_MULTIPLIER
public static final int REMOVE_MULTIPLIERApplied to a board value to mark the move that hard mode is about to drop.- See Also:
-
WIN_OFFSET
public static final int WIN_OFFSETAdded to a board value to mark a square that is part of the winning line.- See Also:
-
-
Constructor Details
-
TicTacToe
public TicTacToe(int id, int firstPlayer, int secondPlayer, boolean hard) Creates a game.- Parameters:
id- the game idfirstPlayer- the player who moves firstsecondPlayer- the other playerhard- whether the older move is removed once a player has three on the board
-
-
Method Details
-
toString
-
isWin
public boolean isWin()Whether the last move completed a line.- Returns:
- true if the game is won
-
checkWinningTrio
public void checkWinningTrio()Looks for a completed line and records it, so it can be highlighted on the board. -
isDraw
public boolean isDraw()Whether the board is full with no line completed.- Returns:
- true if the game is drawn
-
contains
Whether a player is in this game.- Parameters:
fPlayer- the player- Returns:
- true if they are one of the two
-
setNextPlayer
public void setNextPlayer()Passes the turn to the other player. -
move
-
getMovesMap
-
getId
public int getId() -
isHard
public boolean isHard() -
getField
public int[][] getField() -
getFirstPlayer
public int getFirstPlayer() -
getSecondPlayer
public int getSecondPlayer() -
getNextPlayer
public int getNextPlayer() -
getWinningTrio
public int[] getWinningTrio() -
isEnded
public boolean isEnded() -
isCreated
public boolean isCreated() -
setNextPlayer
public void setNextPlayer(int nextPlayer) -
setEnded
public void setEnded(boolean ended)
-