Game

Class for manipulating games. It allows starting a game from a configurable initial position and with various configuration options, such as the game type (match or analysis), and how to apply the three-fold repetition and 50-move rules.

This class implements Iterable, allowing easy traversal over the nodes of the main line. The toString function returns the game in PGN format.

Since

1.0.0-beta.1

Author

lunalobos

Types

Link copied to clipboard

Represents the mode in which the 50-move rule is handled.

Link copied to clipboard

Enum to choose the modality of the instance. In a MATCH, the idea is that there is no going back or possibility of modification once the game is over. In an ANALYSIS game, immutability is not necessary.

Link copied to clipboard

This type of exception is thrown if an instance is misused according to the established GameMode.

Link copied to clipboard
interface Node

Each game can be interpreted as a series of interconnected nodes in a tree. Each node is move in the game and holds information about the board state after that move. Additionally, each node can have regular or end-of-line comments, suffix annotations, as well as more than one child (from the second child onwards, they are variations), and always only one parent node.

Link copied to clipboard

Enum that represents the result of a game.

Link copied to clipboard

Represents the mode in which the three-fold repetition rule is handled.

Link copied to clipboard

Thrown when an internal value of a Game instance is inconsistent (indicates a library bug). If it is thrown, please create an ISSUE with the context where the exception occurs.

Properties

Link copied to clipboard

Stores the current ECO (Encyclopedia of Chess Openings) information for the main line. Setting this property will be ignored if the game is immutable (i.e., GameMode.MATCH and result is set).

Link copied to clipboard

Indicates whether the 50-move half-move limit has been reached (non-terminal).

Link copied to clipboard

Determines how the fifty-move rule is enforced for this game instance. Setting this property will be ignored if the game is immutable.

Link copied to clipboard

An optional comment placed immediately after the game's result tag in PGN. This comment is enclosed in braces {}. Setting this property will be ignored if the game is immutable.

Link copied to clipboard

An optional end-of-line comment placed immediately after the game's result tag in PGN. This comment follows a semicolon ; and extends until the end of the line. Setting this property will be ignored if the game is immutable.

Link copied to clipboard

Indicates whether a position has been repeated five times, leading to an automatic draw according to FIDE rules (terminal).

Link copied to clipboard
val id: Any?

A developer-provided unique identifier for serialization or tracking purposes (e.g., UUID or String).

Link copied to clipboard

The final result of the game. Setting this property will update the "Result" tag in tags and set the game as immutable if GameMode.MATCH is used.

Link copied to clipboard

The root node of the game tree. The setter is protected if the game is immutable.

Link copied to clipboard

Indicates whether the 75-move half-move limit has been reached, leading to an automatic draw (terminal).

Link copied to clipboard

Standard PGN tags (e.g., Event, Site, Date, Round, White, Black, Result).

Link copied to clipboard

Indicates whether a position has been repeated three times (non-terminal).

Link copied to clipboard

Determines how the three-fold repetition rule is enforced for this game instance. Setting this property will be ignored if the game is immutable.

Link copied to clipboard

Indicates that a three-fold repetition draw can be claimed, as the repetition is impending (e.g., the current move will complete the third repetition).

Functions

Link copied to clipboard

Deletes all moves that preceded the provided node in the main line. The node (and its position) becomes the new effective start of the game, creating a new RootNode.

Link copied to clipboard

Deletes all moves (the main line continuation and any variations) that follow the provided node. The node provided remains in the game.

Link copied to clipboard

Deletes the provided node and all subsequent moves/variations in its subtree. The move represented by the node is effectively removed from the game.

Link copied to clipboard
open operator override fun iterator(): Iterator<Game.Node>

Returns an iterator that traverses the main line nodes of the game.

Link copied to clipboard
fun toAnalisis(idSupplier: () -> Any? = { null }): Game

Creates a deep copy of this game, converting its mode to GameMode.ANALYSIS and setting both repetition rules to ThreeRepetitionsMode.AWARE and FiftyMovesRuleMode.AWARE. This makes the new instance fully mutable for analysis.

Link copied to clipboard
open override fun toString(): String

Returns the PGN representation of the game, including tags, moves, and result.

Link copied to clipboard

Removes the last node in the main line, effectively undoing the last move, and returns the parent node. This function only operates on the primary variation (main line).

Link copied to clipboard
fun updateEco()

Updates the instance's ECO ranking based on the last move of the main line. In match mode this is automatic, but in analysis this function must be called, otherwise the game will not be ranked.