Game

data class Game(val target: Boolean = false, val currGameName: String? = null, val lastModified: Long? = null, val gameState: GameState? = null, val countPass: Int = 0, val myPiece: PieceType? = null, val config: CoreConfig = loadCoreConfig(), val service: GameServiceImpl)

Represents a Reversi game, managing the game state, player turns, and interactions with storage. The game has two modes: local and not local.

Local Game

In a local game, if currGameName is null.

Not Local Game

In a not local game, currGameName is not null. The game state is saved and loaded from storage.

Constructors

Link copied to clipboard
constructor(target: Boolean = false, currGameName: String? = null, lastModified: Long? = null, gameState: GameState? = null, countPass: Int = 0, myPiece: PieceType? = null, config: CoreConfig = loadCoreConfig(), service: GameServiceImpl)

Properties

Link copied to clipboard

The core configuration for the game.

Link copied to clipboard

The number of consecutive passes made by players.

Link copied to clipboard

The name of the current game for storage purposes.

Link copied to clipboard

The current state of the game, including the board and players.

Link copied to clipboard

The timestamp of the last modification to the game state in storage.

Link copied to clipboard

The piece type representing the current player.

Link copied to clipboard
Link copied to clipboard

Indicates if the game is in target mode.

Functions

Link copied to clipboard

Changes the player's piece type.

Link copied to clipboard

Checks if it's the player's turn in a not local game.

Link copied to clipboard
suspend fun closeStorage()

Closes the storage connection.

Link copied to clipboard
suspend fun delete()
Link copied to clipboard
internal fun gameEnded()

Validates whether the game already ended and throws if a winner is set.

Link copied to clipboard

Gets the available plays for the current player. If it is not a local game, and it is not the player's turn, returns an empty list.

Link copied to clipboard
suspend fun hasAllPlayers(): Boolean

Ensures both players are available either locally or in persisted storage.

Link copied to clipboard
Link copied to clipboard
suspend fun pass(): Game

Passes the turn to the next player. Saves the game state if the game is not local. Only check player turn if it is a not local game.

Link copied to clipboard
suspend fun play(coordinate: Coordinate): Game

Plays a move at the specified coordinate. Saves the piece to data access if the game is not local. Only check player turn if it is a not local game. And resets the pass count to 0.

Link copied to clipboard
suspend fun refresh(): Game

Refreshes the game state from storage (board and last player). Updates players to reflect the current board state. Increments the pass count if the board is unchanged but the last player has changed.

Link copied to clipboard

Reloads the core configuration.

Link copied to clipboard

Ensures that the game has started by checking if the game state and players are initialized.

Link copied to clipboard
suspend fun saveEndGame()

Saves the current game state to storage. Saves the player in storage if not already present (makes available this player for future loads). It is recommended to use this method only to save the game at the end. Only applicable for not local games (players size must be 1).

Link copied to clipboard
suspend fun saveOnlyBoard(gameState: GameState?)

Saves only the play-related state (board and last player) to storage. Keeps the existing players in storage unchanged. It is recommended to use this method during gameplay to save progress. Only applicable for not local games (players size must be 1).

Link copied to clipboard
fun setTargetMode(target: Boolean): Game

Sets the target mode for the game.

Link copied to clipboard

Converts the game board to a string representation with row/column labels and target markers. If target mode is enabled, available plays are marked with the target character.