chipiron.games.game package

Submodules

chipiron.games.game.final_game_result module

Module for the FinalGameResult enum and the GameReport dataclass.

class chipiron.games.game.final_game_result.FinalGameResult(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: str, Enum

Enum representing the final result of a game.

DRAW = 'draw'
WIN_FOR_BLACK = 'win_for_black'
WIN_FOR_WHITE = 'win_for_white'
class chipiron.games.game.final_game_result.GameReport(final_game_result: FinalGameResult, move_history: list[str], fen_history: list[str])[source]

Bases: object

Dataclass representing a game report.

fen_history: list[str]
final_game_result: FinalGameResult
move_history: list[str]

chipiron.games.game.game module

Module for the Game class.

class chipiron.games.game.game.Game(board: IBoard, playing_status: GamePlayingStatus, seed_: int = 0)[source]

Bases: object

Class representing a game of chess. Note that a Game and a Board can look similar classes as a Board can (but not necessarily) include a record of previous moves. Board should be more lightweight that Board. Boards are more specific to a special position

(but can include history) while Game are more related to the entire Game and the process generating it.

Game needs the original fen, all the moves, the seed to generate and maybe more.

property board: IBoard

Gets the chess board.

Returns:

The chess board.

Return type:

BoardChi

property fen_history: list[str]

Gets the history of fen.

Returns:

The history of fen.

Return type:

list[fen]

is_paused() bool[source]

Checks if the game is paused.

Returns:

True if the game is paused, False otherwise.

Return type:

bool

is_play() bool[source]

Checks if the game is being played.

Returns:

True if the game is being played, False otherwise.

Return type:

bool

property move_history: list[str]

Gets the history of move.

Returns:

The history of move.

Return type:

list[chess.Move]

play_move(move: int) None[source]

Plays a move on the chess board.

Parameters:

move (chess.Move) – The move to be played.

Raises:

AssertionError – If the move is not valid or the game status is not play.

property playing_status: GamePlayingStatus

Gets or sets the playing status of the game.

Returns:

The playing status of the game.

Return type:

GamePlayingStatus

rewind_one_move() None[source]

Rewinds the last move on the chess board.

Raises:

AssertionError – If the game status is not paused.

set_pause_status() None[source]

Pauses the game.

set_play_status() None[source]

Starts playing the game.

class chipiron.games.game.game.ObservableGame(game: Game)[source]

Bases: object

Represents an observable version of the Game object.

property board: IBoard

Gets the chess board.

Returns:

The chess board.

Return type:

BoardChi

property fen_history: list[str]

Gets the history of fen.

Returns:

The history of fen.

Return type:

list[fen]

game: Game
is_paused() bool[source]

Checks if the game is paused.

Returns:

True if the game is paused, False otherwise.

Return type:

bool

is_play() bool[source]

Checks if the game is being played.

Returns:

True if the game is being played, False otherwise.

Return type:

bool

mailboxes_display: list[queue.Queue[chipiron.utils.dataclass.IsDataclass]]
move_functions: list[chipiron.players.factory_higher_level.MoveFunction]
property move_history: list[str]

Gets the history of move.

Returns:

The history of move.

Return type:

list[chess.Move]

notify_display() None[source]

Notifies the display mailboxes with the updated board.

notify_status() None[source]

Notifies the status mailboxes with the updated game status.

play_move(move: int) None[source]

Plays a move on the chess board.

Parameters:

move (chess.Move) – The move to be played.

property playing_status: GamePlayingStatus

Gets the playing status of the game.

Returns:

The playing status of the game.

Return type:

GamePlayingStatus

query_move_from_players() None[source]

Notifies the players to ask for a move.

register_display(mailbox: Queue[IsDataclass]) None[source]

Registers a mailbox for displaying the board.

Parameters:

mailbox (queue.Queue[IsDataclass]) – The mailbox for board to be displayed.

register_player(move_function: MoveFunction) None[source]

Registers a player to compute a move.

Parameters:

move_function (MoveFunction) – The function to be called to compute a move.

rewind_one_move() None[source]

Rewinds the last move on the chess board.

set_pause_status() None[source]

Pauses the game.

set_play_status() None[source]

Starts playing the game.

chipiron.games.game.game_args module

Module that contains the GameArgs class.

class chipiron.games.game.game_args.GameArgs(starting_position: FenStartingPositionArgs | FileStartingPositionArgs, max_half_moves: int | None = None, each_player_has_its_own_thread: bool = False)[source]

Bases: object

Represents the arguments for a game.

starting_position

The starting position of the game.

Type:

AllStartingPositionArgs

max_half_moves

The maximum number of half moves allowed in the game. Defaults to None.

Type:

int | None, optional

each_player_has_its_own_thread

Whether each player has its own thread. Defaults to False.

Type:

bool, optional

each_player_has_its_own_thread: bool = False
max_half_moves: int | None = None
starting_position: FenStartingPositionArgs | FileStartingPositionArgs

chipiron.games.game.game_args_factory module

Module for the GameArgsFactory class

This module defines the GameArgsFactory class, which is responsible for creating game arguments and managing game settings.

class chipiron.games.game.game_args_factory.GameArgsFactory(args_match: match.MatchSettingsArgs, args_player_one: PlayerArgs, args_player_two: PlayerArgs, seed_: int | None, args_game: GameArgs)[source]

Bases: object

The GameArgsFactory creates the players and decides the rules. So far quite simple This class is supposed to be dependent on Match-related classes (contrarily to the GameArgsFactory)

args_game: GameArgs
args_match: match.MatchSettingsArgs
args_player_one: PlayerArgs
args_player_two: PlayerArgs
game_number: int
generate_game_args(game_number: int) tuple[dict[bool, chipiron.players.player_args.PlayerFactoryArgs], chipiron.games.game.game_args.GameArgs, Optional[int]][source]

Generate game arguments for a specific game number.

Parameters:

game_number (int) – The number of the game.

Returns:

A tuple containing the player color to factory arguments mapping, game arguments, and the merged seed.

Return type:

tuple[dict[chess.Color, players.PlayerFactoryArgs], GameArgs, seed | None]

is_match_finished() bool[source]

Check if the match is finished.

Returns:

True if the match is finished, False otherwise.

Return type:

bool

seed_: int | None

chipiron.games.game.game_manager module

Module in charge of managing the game. It is the main class that will be used to play a game.

class chipiron.games.game.game_manager.GameManager(game: ObservableGame, syzygy: SyzygyTable[Any] | None, display_board_evaluator: IGameBoardEvaluator, output_folder_path: str | PathLike[str] | None, args: GameArgs, player_color_to_id: dict[bool, str], main_thread_mailbox: Queue[IsDataclass], players: list[chipiron.players.player_thread.PlayerProcess | chipiron.players.game_player.GamePlayer], move_factory: MoveFactory, progress_collector: PlayerProgressCollectorP)[source]

Bases: object

Object in charge of playing one game

args: GameArgs
display_board_evaluator: IGameBoardEvaluator
external_eval() tuple[float | None, float][source]

Evaluates the game board using the display board evaluator.

Returns:

A tuple containing the evaluation scores.

Return type:

tuple[float, float]

game: ObservableGame
game_continue_conditions() bool[source]

Checks the conditions for continuing the game.

Returns:

True if the game should continue, False otherwise.

Return type:

bool

main_thread_mailbox: Queue[IsDataclass]
move_factory: MoveFactory
output_folder_path: str | PathLike[str] | None
play_one_game() GameReport[source]

Play one game.

Returns:

The report of the game.

Return type:

GameReport

play_one_move(move: int) None[source]

Play one move in the game.

Parameters:

move (chess.Move) – The move to be played.

player_color_to_id: dict[bool, str]
players: list[chipiron.players.player_thread.PlayerProcess | chipiron.players.game_player.GamePlayer]
print_to_file(game_report: GameReport, idx: int = 0) None[source]

Print the moves of the game to a yaml file and a more human-readable text file.

Parameters:
  • game_report (GameReport) – a game report to be printed

  • idx (int) – The index to include in the file name (default is 0).

Returns:

None

processing_mail(message: IsDataclass) None[source]

Process the incoming mail message.

Parameters:

message (IsDataclass) – The incoming mail message.

Returns:

None

progress_collector: PlayerProgressCollectorP
rewind_one_move() None[source]

Rewinds the game by one move.

This method rewinds the game by one move, undoing the last move made. If the game has a Syzygy tablebase loaded and the current board position is in the tablebase, it prints the theoretically finished value for white.

Returns:

None

simple_results() FinalGameResult[source]

Determines the final result of the game based on the current state of the board.

Returns:

The final result of the game.

Return type:

FinalGameResult

syzygy: SyzygyTable[Any] | None
tell_results() None[source]

Prints the results of the game based on the current state of the board.

The method checks various conditions on the board and prints the corresponding result. It also checks for specific conditions like syzygy, fivefold repetition, seventy-five moves, insufficient material, stalemate, and checkmate.

Returns:

None

terminate_processes() None[source]

Terminates all player processes and stops the associated threads.

This method iterates over the list of players and terminates any player processes found. If a player process is found, it is terminated and the associated thread is stopped.

Note: - This method assumes that the players attribute is a list of PlayerProcess or GamePlayer objects.

Returns: None

chipiron.games.game.game_manager_factory module

Module for the GameManagerFactory class.

class chipiron.games.game.game_manager_factory.GameManagerFactory(syzygy_table: ~chipiron.players.boardevaluators.table_base.syzygy_table.SyzygyTable[~typing.Any] | None, output_folder_path: str | ~os.PathLike[str] | None, main_thread_mailbox: ~queue.Queue[~chipiron.utils.dataclass.IsDataclass], game_manager_board_evaluator: ~chipiron.players.boardevaluators.board_evaluator.IGameBoardEvaluator, board_factory: ~chipiron.environments.chess_env.board.factory.BoardFactory, move_factory: ~chipiron.environments.chess_env.move_factory.MoveFactory, implementation_args: ~chipiron.scripts.chipiron_args.ImplementationArgs, universal_behavior: bool, subscribers: list[queue.Queue[chipiron.utils.dataclass.IsDataclass]] = <factory>)[source]

Bases: object

The GameManagerFactory creates GameManager once the players and rules have been decided. Calling create ask for the creation of a GameManager depending on args and players. This class is supposed to be independent of Match-related classes (contrarily to the GameArgsFactory)

Args: syzygy_table (SyzygyTable | None): The syzygy table used for endgame tablebase lookups. game_manager_board_evaluator (IGameBoardEvaluator): The game board evaluator used for evaluating game positions. output_folder_path (path | None): The path to the output folder where game data will be saved. main_thread_mailbox (queue.Queue[IsDataclass]): The mailbox used for communication between processes.

board_factory: BoardFactory
create(args_game_manager: GameArgs, player_color_to_factory_args: dict[bool, chipiron.players.player_args.PlayerFactoryArgs], game_seed: int) GameManager[source]

Create a GameManager with the given arguments

Parameters:
  • args_game_manager (GameArgs) – the arguments of the game manager

  • player_color_to_factory_args (dict[chess.Color, PlayerFactoryArgs]) – the arguments of the players

  • game_seed (int) – the seed of the game

Returns:

the created GameManager

game_manager_board_evaluator: IGameBoardEvaluator
implementation_args: ImplementationArgs
main_thread_mailbox: Queue[IsDataclass]
move_factory: MoveFactory
output_folder_path: str | PathLike[str] | None
subscribe(subscriber: Queue[IsDataclass]) None[source]

Subscribe to the GameManagerFactory to get the PlayersColorToPlayerMessage As well as subscribing to the game_manager_board_evaluator to get the EvaluationMessage

Parameters:

subscriber – the subscriber queue

subscribers: list[queue.Queue[chipiron.utils.dataclass.IsDataclass]]
syzygy_table: SyzygyTable[Any] | None
universal_behavior: bool

chipiron.games.game.game_playing_status module

Module containing the class GamePlaying

class chipiron.games.game.game_playing_status.GamePlayingStatus(_status: PlayingStatus = PlayingStatus.PLAY)[source]

Bases: object

Object containing the playing status of a game and the board.

is_paused() bool[source]

Check if the game is currently paused.

Returns:

True if the game is paused, False otherwise.

Return type:

bool

is_play() bool[source]

Check if the game is currently playing.

Returns:

True if the game is playing, False otherwise.

Return type:

bool

pause() None[source]

Set the playing status to ‘PAUSE’.

play() None[source]

Set the playing status to ‘PLAY’.

property status: PlayingStatus

Get the current playing status of the game.

Returns:

The current playing status.

Return type:

PlayingStatus

class chipiron.games.game.game_playing_status.PlayingStatus(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: Enum

Class containing the possible playing status of a game.

PAUSE = 0
PLAY = 1

chipiron.games.game.observable_game_playing_status module

Module that defines an observable version of GamePlayingStatus

class chipiron.games.game.observable_game_playing_status.ObservableGamePlayingStatus(game_playing_status: GamePlayingStatus)[source]

Bases: object

An observable version of GamePlayingStatus that notifies subscribers. Players and GUI can then decide what to do with this information.

is_paused() bool[source]

Checks if the game is currently paused.

Returns:

True if the game is paused, False otherwise.

Return type:

bool

is_play() bool[source]

Checks if the game is currently being played.

Returns:

True if the game is being played, False otherwise.

Return type:

bool

notify() None[source]

Notifies the subscribers with the current game status.

pause() None[source]

Pauses the game and notifies the subscribers.

play() None[source]

Plays the game and notifies the subscribers.

property status: PlayingStatus

Gets the current playing status.

Returns:

The current playing status.

Return type:

PlayingStatus

subscribe(mailboxes: list[queue.Queue[chipiron.utils.communication.gui_messages.game_status_message.GameStatusMessage]]) None[source]

Subscribes the given mailboxes to receive game status updates.

Parameters:

mailboxes (list[queue.Queue[GameStatusMessage]]) – The mailboxes to subscribe.

chipiron.games.game.progress_collector module

Module collecting the progress of computing moves by each player

class chipiron.games.game.progress_collector.PlayerProgressCollector(progress_white_: int | None = None, progress_black_: int | None = None)[source]

Bases: object

Object in charge of collecting the progress of computing moves by each player

property progress_black: int | None
progress_black_: int | None = None
property progress_white: int | None
progress_white_: int | None = None
class chipiron.games.game.progress_collector.PlayerProgressCollectorObservable(progress_collector: ~chipiron.games.game.progress_collector.PlayerProgressCollector = <factory>, subscribers: list[queue.Queue[chipiron.utils.dataclass.IsDataclass]] = <factory>)[source]

Bases: object

Object in charge of collecting the progress of computing moves by each player

notify(color: bool, value: int | None) None[source]
progress_black(value: int | None) None[source]
progress_collector: PlayerProgressCollector
progress_white(value: int | None) None[source]
subscribers: list[queue.Queue[chipiron.utils.dataclass.IsDataclass]]
class chipiron.games.game.progress_collector.PlayerProgressCollectorP(*args, **kwargs)[source]

Bases: Protocol

Object defining the protocol for setting the progress values

progress_black(value: int | None) None[source]
progress_white(value: int | None) None[source]

chipiron.games.game.test_game module

Module contents

This module contains the game logic and the game arguments.

The game module provides the following classes: - ObservableGame: A class that represents an observable game. - Game: A class that represents a game. - GameArgs: A class that represents the game arguments. - GameArgsFactory: A class that provides a factory for creating game arguments.

The module also defines the __all__ list, which specifies the names that should be imported when using the from game import * syntax.

Example usage:

from game import ObservableGame, Game, GameArgs, GameArgsFactory

class chipiron.games.game.Game(board: IBoard, playing_status: GamePlayingStatus, seed_: int = 0)[source]

Bases: object

Class representing a game of chess. Note that a Game and a Board can look similar classes as a Board can (but not necessarily) include a record of previous moves. Board should be more lightweight that Board. Boards are more specific to a special position

(but can include history) while Game are more related to the entire Game and the process generating it.

Game needs the original fen, all the moves, the seed to generate and maybe more.

property board: IBoard

Gets the chess board.

Returns:

The chess board.

Return type:

BoardChi

property fen_history: list[str]

Gets the history of fen.

Returns:

The history of fen.

Return type:

list[fen]

is_paused() bool[source]

Checks if the game is paused.

Returns:

True if the game is paused, False otherwise.

Return type:

bool

is_play() bool[source]

Checks if the game is being played.

Returns:

True if the game is being played, False otherwise.

Return type:

bool

property move_history: list[str]

Gets the history of move.

Returns:

The history of move.

Return type:

list[chess.Move]

play_move(move: int) None[source]

Plays a move on the chess board.

Parameters:

move (chess.Move) – The move to be played.

Raises:

AssertionError – If the move is not valid or the game status is not play.

property playing_status: GamePlayingStatus

Gets or sets the playing status of the game.

Returns:

The playing status of the game.

Return type:

GamePlayingStatus

rewind_one_move() None[source]

Rewinds the last move on the chess board.

Raises:

AssertionError – If the game status is not paused.

set_pause_status() None[source]

Pauses the game.

set_play_status() None[source]

Starts playing the game.

class chipiron.games.game.GameArgs(starting_position: FenStartingPositionArgs | FileStartingPositionArgs, max_half_moves: int | None = None, each_player_has_its_own_thread: bool = False)[source]

Bases: object

Represents the arguments for a game.

starting_position

The starting position of the game.

Type:

AllStartingPositionArgs

max_half_moves

The maximum number of half moves allowed in the game. Defaults to None.

Type:

int | None, optional

each_player_has_its_own_thread

Whether each player has its own thread. Defaults to False.

Type:

bool, optional

each_player_has_its_own_thread: bool = False
max_half_moves: int | None = None
starting_position: FenStartingPositionArgs | FileStartingPositionArgs
class chipiron.games.game.GameArgsFactory(args_match: match.MatchSettingsArgs, args_player_one: PlayerArgs, args_player_two: PlayerArgs, seed_: int | None, args_game: GameArgs)[source]

Bases: object

The GameArgsFactory creates the players and decides the rules. So far quite simple This class is supposed to be dependent on Match-related classes (contrarily to the GameArgsFactory)

args_game: GameArgs
args_match: match.MatchSettingsArgs
args_player_one: PlayerArgs
args_player_two: PlayerArgs
game_number: int
generate_game_args(game_number: int) tuple[dict[bool, chipiron.players.player_args.PlayerFactoryArgs], chipiron.games.game.game_args.GameArgs, Optional[int]][source]

Generate game arguments for a specific game number.

Parameters:

game_number (int) – The number of the game.

Returns:

A tuple containing the player color to factory arguments mapping, game arguments, and the merged seed.

Return type:

tuple[dict[chess.Color, players.PlayerFactoryArgs], GameArgs, seed | None]

is_match_finished() bool[source]

Check if the match is finished.

Returns:

True if the match is finished, False otherwise.

Return type:

bool

seed_: int | None
class chipiron.games.game.ObservableGame(game: Game)[source]

Bases: object

Represents an observable version of the Game object.

property board: IBoard

Gets the chess board.

Returns:

The chess board.

Return type:

BoardChi

property fen_history: list[str]

Gets the history of fen.

Returns:

The history of fen.

Return type:

list[fen]

game: Game
is_paused() bool[source]

Checks if the game is paused.

Returns:

True if the game is paused, False otherwise.

Return type:

bool

is_play() bool[source]

Checks if the game is being played.

Returns:

True if the game is being played, False otherwise.

Return type:

bool

mailboxes_display: list[queue.Queue[chipiron.utils.dataclass.IsDataclass]]
move_functions: list[chipiron.players.factory_higher_level.MoveFunction]
property move_history: list[str]

Gets the history of move.

Returns:

The history of move.

Return type:

list[chess.Move]

notify_display() None[source]

Notifies the display mailboxes with the updated board.

notify_status() None[source]

Notifies the status mailboxes with the updated game status.

play_move(move: int) None[source]

Plays a move on the chess board.

Parameters:

move (chess.Move) – The move to be played.

property playing_status: GamePlayingStatus

Gets the playing status of the game.

Returns:

The playing status of the game.

Return type:

GamePlayingStatus

query_move_from_players() None[source]

Notifies the players to ask for a move.

register_display(mailbox: Queue[IsDataclass]) None[source]

Registers a mailbox for displaying the board.

Parameters:

mailbox (queue.Queue[IsDataclass]) – The mailbox for board to be displayed.

register_player(move_function: MoveFunction) None[source]

Registers a player to compute a move.

Parameters:

move_function (MoveFunction) – The function to be called to compute a move.

rewind_one_move() None[source]

Rewinds the last move on the chess board.

set_pause_status() None[source]

Pauses the game.

set_play_status() None[source]

Starts playing the game.