Class Game

java.lang.Object
org.xxdc.oss.example.Game
All Implemented Interfaces:
Serializable, AutoCloseable

public class Game extends Object implements Serializable, AutoCloseable
Represents a game of Tic-Tac-Toe, including the game board, players, and game state. The game can be serialized and persisted to a file, and loaded from a file. The game can be played by alternating moves between human and bot players.
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new Game instance with a 3x3 game board, persistence enabled, and a human player as player 'X' and a bot player as player 'O'.
    Game(int size, boolean persistenceEnabled, PlayerNode... players)
    Constructs a new Game instance with the specified game board size, persistence enabled state, and player nodes.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    static Game
    from(File gameFile)
    Loads a Game instance from the specified file.
    Deprecated, for removal: This API element is subject to removal in a future version.
    use id() instead
    Returns the history of the game, including all moves made.
    id()
    Returns the unique identifier for this game instance.
    int
     
    int
    Returns the number of players in the game.
    static Game
    Constructs a new Game instance with a 3x3 game board, persistence disabled, and a bot player as player 'X' and a bot player as player 'O'.
    void
    Plays the game, rendering the board, applying player moves, and persisting the game state if enabled.
    void
    playWithAction(Consumer<Game> postMoveAction)
    Plays the game, rendering the board, applying player moves, and persisting the game state if enabled.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Game

      public Game()
      Constructs a new Game instance with a 3x3 game board, persistence enabled, and a human player as player 'X' and a bot player as player 'O'.
    • Game

      public Game(int size, boolean persistenceEnabled, PlayerNode... players)
      Constructs a new Game instance with the specified game board size, persistence enabled state, and player nodes.
      Parameters:
      size - The size of the game board, e.g. 3 for a 3x3 board.
      persistenceEnabled - Whether game state should be persisted to a file.
      players - The player nodes for the game, which include the player marker and player implementation.
  • Method Details

    • from

      public static Game from(File gameFile) throws IOException, ClassNotFoundException
      Loads a Game instance from the specified file.
      Parameters:
      gameFile - The file containing the serialized Game instance.
      Returns:
      The loaded Game instance.
      Throws:
      IOException - If an I/O error occurs while reading the file.
      ClassNotFoundException - If the serialized class cannot be found.
    • ofBots

      public static Game ofBots()
      Constructs a new Game instance with a 3x3 game board, persistence disabled, and a bot player as player 'X' and a bot player as player 'O'.
    • play

      public void play()
      Plays the game, rendering the board, applying player moves, and persisting the game state if enabled. The game continues until a winning player is found or there are no more moves available, at which point the winner or tie is logged.
    • playWithAction

      public void playWithAction(Consumer<Game> postMoveAction)
      Plays the game, rendering the board, applying player moves, and persisting the game state if enabled. The game continues until a winning player is found or there are no more moves available, at which point the winner or tie is logged.
      Parameters:
      postMoveAction - The action to perform after a move is made (if any)
    • getGameId

      @Deprecated(since="1.5.0", forRemoval=true) public UUID getGameId()
      Deprecated, for removal: This API element is subject to removal in a future version.
      use id() instead
      Returns the unique identifier for this game instance (Deprecated).
      Returns:
      the game ID
    • id

      public UUID id()
      Returns the unique identifier for this game instance.
      Returns:
      the game ID
    • close

      public void close() throws Exception
      Specified by:
      close in interface AutoCloseable
      Throws:
      Exception
    • numberOfPlayers

      public int numberOfPlayers()
      Returns the number of players in the game.
      Returns:
      the number of players
    • history

      public SequencedCollection<GameState> history()
      Returns the history of the game, including all moves made.
    • moveNumber

      public int moveNumber()