Record Class GameState
java.lang.Object
java.lang.Record
org.xxdc.oss.example.GameState
- Record Components:
gameId- The unique game idboard- The game board.playerMarkers- The list of player markers.currentPlayerIndex- The index of the current player in theplayerMarkerslist.lastMove- The index of the last move made on the game board.
- All Implemented Interfaces:
Serializable,JsonSerializable
public record GameState(UUID gameId, GameBoard board, List<String> playerMarkers, int currentPlayerIndex, int lastMove)
extends Record
implements JsonSerializable, Serializable
Represents the current state of a game, including the game board, player markers, and the index
of the current player. Provides methods to interact with the game state, such as checking for
available moves, checking for chains, and advancing the game to the next player's turn.
Implements the JsonSerializable and Serializable interfaces, allowing the game state to be
serialized and deserialized.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionafterPlayerMoves(int move) Creates a newGameStateinstance with the current player's move applied to the game board.Returns the JSON representation of the object.Returns a list of available moves on the game board.board()Returns the value of theboardrecord component.Returns the current player's marker.intReturns the value of thecurrentPlayerIndexrecord component.final booleanIndicates whether some other object is "equal to" this one.gameId()Returns the value of thegameIdrecord component.booleanChecks if the specified player has a chain on the game board.final inthashCode()Returns a hash code value for this object.booleanChecks if there are any available moves on the game board.booleanChecks if the game is in a terminal state, where either there are no more available moves or a player has a chain.intlastMove()Returns the value of thelastMoverecord component.Returns the last player's marker from the list of player markers.booleanChecks if the last player to move has a chain on the game board.intReturns the index of the last player to move in the list of player markers.Returns the value of theplayerMarkersrecord component.final StringtoString()Returns a string representation of this record class.
-
Constructor Details
-
GameState
Constructs a newGameStateinstance with the provided game board, player markers, and the index of the current player. The last move index is set to -1 to indicate that no move has been made yet.- Parameters:
gameId- The unique game idboard- The game board.playerMarkers- The list of player markers.currentPlayerIndex- The index of the current player in theplayerMarkerslist.
-
GameState
Constructs a newGameStateinstance by copying the state from the providedGameStateobject. This constructor creates a deep copy of the game board, player markers, and current player index, allowing the newGameStateinstance to be modified independently from the original.- Parameters:
state- TheGameStateobject to copy.
-
GameState
public GameState(UUID gameId, GameBoard board, List<String> playerMarkers, int currentPlayerIndex, int lastMove) Creates an instance of aGameStaterecord class.- Parameters:
gameId- the value for thegameIdrecord componentboard- the value for theboardrecord componentplayerMarkers- the value for theplayerMarkersrecord componentcurrentPlayerIndex- the value for thecurrentPlayerIndexrecord componentlastMove- the value for thelastMoverecord component
-
-
Method Details
-
currentPlayer
Returns the current player's marker.- Returns:
- The current player's marker.
-
hasMovesAvailable
public boolean hasMovesAvailable()Checks if there are any available moves on the game board.- Returns:
trueif there are available moves,falseotherwise.
-
hasChain
Checks if the specified player has a chain on the game board.- Parameters:
player- The player marker to check for a chain.- Returns:
trueif the player has a chain,falseotherwise.
-
availableMoves
-
isTerminal
public boolean isTerminal()Checks if the game is in a terminal state, where either there are no more available moves or a player has a chain.- Returns:
trueif the game is in a terminal state,falseotherwise.
-
asJsonString
Description copied from interface:JsonSerializableReturns the JSON representation of the object.- Specified by:
asJsonStringin interfaceJsonSerializable- Returns:
- the JSON string representation of the object.
-
afterPlayerMoves
-
lastPlayerHasChain
public boolean lastPlayerHasChain()Checks if the last player to move has a chain on the game board.- Returns:
trueif the last player has a chain,falseotherwise.
-
lastPlayer
Returns the last player's marker from the list of player markers.- Returns:
- the last player's marker
- Throws:
GameServiceException- if there is no last move or the board is empty
-
lastPlayerIndex
public int lastPlayerIndex()Returns the index of the last player to move in the list of player markers.- Returns:
- the index of the last player to move in the list of player markers
-
toString
-
hashCode
-
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared withObjects::equals(Object,Object); primitive components are compared with thecomparemethod from their corresponding wrapper classes. -
gameId
-
board
-
playerMarkers
Returns the value of theplayerMarkersrecord component.- Returns:
- the value of the
playerMarkersrecord component
-
currentPlayerIndex
public int currentPlayerIndex()Returns the value of thecurrentPlayerIndexrecord component.- Returns:
- the value of the
currentPlayerIndexrecord component
-
lastMove
-