Record Class GameState

java.lang.Object
java.lang.Record
org.xxdc.oss.example.GameState
Record Components:
board - The game board.
playerMarkers - The list of player markers.
currentPlayerIndex - The index of the current player in the playerMarkers list.
lastMove - The index of the last move made on the game board.
All Implemented Interfaces:
Serializable, JsonSerializable

public record GameState(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 Details

    • GameState

      public GameState(GameBoard board, List<String> playerMarkers, int currentPlayerIndex)
      Constructs a new GameState instance 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:
      board - The game board.
      playerMarkers - The list of player markers.
      currentPlayerIndex - The index of the current player in the playerMarkers list.
    • GameState

      public GameState(GameState state)
      Constructs a new GameState instance by copying the state from the provided GameState object. This constructor creates a deep copy of the game board, player markers, and current player index, allowing the new GameState instance to be modified independently from the original.
      Parameters:
      state - The GameState object to copy.
    • GameState

      public GameState(GameBoard board, List<String> playerMarkers, int currentPlayerIndex, int lastMove)
      Creates an instance of a GameState record class.
      Parameters:
      board - the value for the board record component
      playerMarkers - the value for the playerMarkers record component
      currentPlayerIndex - the value for the currentPlayerIndex record component
      lastMove - the value for the lastMove record component
  • Method Details

    • currentPlayer

      public String 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:
      true if there are available moves, false otherwise.
    • hasChain

      public boolean hasChain(String player)
      Checks if the specified player has a chain on the game board.
      Parameters:
      player - The player marker to check for a chain.
      Returns:
      true if the player has a chain, false otherwise.
    • availableMoves

      public List<Integer> availableMoves()
      Returns a list of available moves on the game board.
      Returns:
      A list of available moves on the game board.
    • 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:
      true if the game is in a terminal state, false otherwise.
    • asJsonString

      public String asJsonString()
      Description copied from interface: JsonSerializable
      Returns the JSON representation of the object.
      Specified by:
      asJsonString in interface JsonSerializable
      Returns:
      the JSON string representation of the object.
    • afterPlayerMoves

      public GameState afterPlayerMoves(int move)
      Creates a new GameState instance with the current player's move applied to the game board.
      Parameters:
      move - The move to apply to the game board.
      Returns:
      A new GameState instance with the updated game board and current player index.
    • lastPlayerHasChain

      public boolean lastPlayerHasChain()
      Checks if the last player to move has a chain on the game board.
      Returns:
      true if the last player has a chain, false otherwise.
    • lastPlayer

      public String 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

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      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 with Objects::equals(Object,Object); primitive components are compared with the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • board

      public GameBoard board()
      Returns the value of the board record component.
      Returns:
      the value of the board record component
    • playerMarkers

      public List<String> playerMarkers()
      Returns the value of the playerMarkers record component.
      Returns:
      the value of the playerMarkers record component
    • currentPlayerIndex

      public int currentPlayerIndex()
      Returns the value of the currentPlayerIndex record component.
      Returns:
      the value of the currentPlayerIndex record component
    • lastMove

      public int lastMove()
      Returns the value of the lastMove record component.
      Returns:
      the value of the lastMove record component