Record Class GameBoardLocalImpl

java.lang.Object
java.lang.Record
org.xxdc.oss.example.GameBoardLocalImpl
Record Components:
dimension - the dimension of the board
content - the current state of the board, represented as a 1D array of player marker strings
All Implemented Interfaces:
Serializable, GameBoard, JsonSerializable

public record GameBoardLocalImpl(int dimension, String[] content) extends Record implements Serializable, GameBoard
Represents a game board for a game, such as tic-tac-toe. The board has a specified dimension, and stores the current state of the game in a 1D array. Provides methods to check the validity of moves, place player markers, check for a winner, and get a string representation of the board.
See Also:
  • Field Summary

    Fields inherited from interface GameBoard

    log, useNative
  • Constructor Summary

    Constructors
    Constructor
    Description
    GameBoardLocalImpl(int dimension)
    Constructs a new GameBoardLocalImpl instance with the specified dimension and initializes the content array with null values.
    GameBoardLocalImpl(int dimension, String[] content)
    Creates an instance of a GameBoardLocalImpl record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    Converts the game board to a JSON string representation for serialization.
     
    Returns the value of the content record component.
    int
    Returns the value of the dimension record component.
    final boolean
    Indicates whether some other object is "equal to" this one.
    boolean
    hasChain(String playerMarker)
    Checks if the given player has a winning chain of connected game pieces on the game board.
    final int
    Returns a hash code value for this object.
    boolean
    Checks if there are any available moves on the game board.
    boolean
    hasPlayer(String playerMarker, int location)
    Checks if the given player is present at the given location on the game board.
    boolean
    isValidMove(int location)
    Checks if the given location on the game board is a valid move (i.e.
    Returns a string representation of this record class.
    withMove(String playerMarker, int location)
    Creates a new GameBoard instance with the given player's move made at the specified location within the game board dimension x dimension

    Methods inherited from class Object

    finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface GameBoard

    availableMoves, isEmpty
  • Constructor Details

    • GameBoardLocalImpl

      public GameBoardLocalImpl(int dimension)
      Constructs a new GameBoardLocalImpl instance with the specified dimension and initializes the content array with null values.
      Parameters:
      dimension - the dimension of the game board
    • GameBoardLocalImpl

      public GameBoardLocalImpl(int dimension, String[] content)
      Creates an instance of a GameBoardLocalImpl record class.
      Parameters:
      dimension - the value for the dimension record component
      content - the value for the content record component
  • Method Details

    • toString

      public 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 interface GameBoard
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • isValidMove

      public boolean isValidMove(int location)
      Description copied from interface: GameBoard
      Checks if the given location on the game board is a valid move (i.e. an available location).
      Specified by:
      isValidMove in interface GameBoard
      Parameters:
      location - the location on the game board to check
      Returns:
      true if the location is a valid move, false otherwise
    • hasChain

      public boolean hasChain(String playerMarker)
      Description copied from interface: GameBoard
      Checks if the given player has a winning chain of connected game pieces on the game board.
      Specified by:
      hasChain in interface GameBoard
      Parameters:
      playerMarker - the marker representing the player to check for a chain
      Returns:
      true if the player has a chain of connected game pieces, false otherwise
    • hasMovesAvailable

      public boolean hasMovesAvailable()
      Description copied from interface: GameBoard
      Checks if there are any available moves on the game board.
      Specified by:
      hasMovesAvailable in interface GameBoard
      Returns:
      true if there are any available moves, false otherwise
    • withMove

      public GameBoardLocalImpl withMove(String playerMarker, int location)
      Description copied from interface: GameBoard
      Creates a new GameBoard instance with the given player's move made at the specified location within the game board dimension x dimension
      Specified by:
      withMove in interface GameBoard
      Parameters:
      playerMarker - the marker representing the player making the move
      location - the location on the game board where the player is making the move
      Returns:
      a ne GameBoard instance with the player's move applied
    • asJsonString

      public String asJsonString()
      Description copied from interface: GameBoard

      Converts the game board to a JSON string representation for serialization. Format corresponds to the following JSON schema with content as a 1D array of strings of size dimension x dimension.

      { "dimension": int, "content": [ string, string, ..., string ] } }
      
      Specified by:
      asJsonString in interface GameBoard
      Specified by:
      asJsonString in interface JsonSerializable
      Returns:
      the game board as a JSON string
      See Also:
    • clone

      public GameBoard clone()
      Overrides:
      clone in class Object
    • hasPlayer

      public boolean hasPlayer(String playerMarker, int location)
      Description copied from interface: GameBoard
      Checks if the given player is present at the given location on the game board.
      Specified by:
      hasPlayer in interface GameBoard
      Parameters:
      playerMarker - the marker representing the player to check
      location - the location on the game board to check
      Returns:
      true if the player is present at the given location, false otherwise
    • 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.
    • dimension

      public int dimension()
      Returns the value of the dimension record component.
      Specified by:
      dimension in interface GameBoard
      Returns:
      the value of the dimension record component
    • content

      public String[] content()
      Returns the value of the content record component.
      Specified by:
      content in interface GameBoard
      Returns:
      the value of the content record component