Class Game
java.lang.Object
org.xxdc.oss.example.Game
- All Implemented Interfaces:
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
ConstructorsConstructorDescriptionGame()Constructs a newGameinstance 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 newGameinstance with the specified game board size, persistence enabled state, and player nodes. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()static GameLoads aGameinstance from the specified file.history()Returns the history of the game, including all moves made.id()Returns the unique identifier for this game instance.intintReturns the number of players in the game.static GameofBots()Constructs a newGameinstance with a 3x3 game board, persistence disabled, and a bot player as player 'X' and a bot player as player 'O'.voidplay()Plays the game, rendering the board, applying player moves, and persisting the game state if enabled.voidplayWithAction(Consumer<Game> postMoveAction) Plays the game, rendering the board, applying player moves, and persisting the game state if enabled.
-
Constructor Details
-
Game
public Game()Constructs a newGameinstance with a 3x3 game board, persistence enabled, and a human player as player 'X' and a bot player as player 'O'. -
Game
Constructs a newGameinstance 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
Loads aGameinstance from the specified file.- Parameters:
gameFile- The file containing the serializedGameinstance.- Returns:
- The loaded
Gameinstance. - Throws:
IOException- If an I/O error occurs while reading the file.ClassNotFoundException- If the serialized class cannot be found.
-
ofBots
-
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
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)
-
id
-
close
- Specified by:
closein interfaceAutoCloseable- Throws:
Exception
-
numberOfPlayers
public int numberOfPlayers()Returns the number of players in the game.- Returns:
- the number of players
-
history
Returns the history of the game, including all moves made. -
moveNumber
public int moveNumber()
-