chessai.tour.game
1import argparse 2import random 3import typing 4 5import chessai.chess.game 6import chessai.core.agentinfo 7import chessai.core.board 8import chessai.core.game 9import chessai.core.gamestate 10import chessai.tour.gamestate 11import chessai.tour.piece 12 13class Game(chessai.chess.game.Game): 14 """ 15 A tour game where the objective is to navigate a piece to the designated coordinates. 16 """ 17 18 def __init__(self, 19 game_info: chessai.core.game.GameInfo, 20 save_path: str | None = None, 21 is_replay: bool = False, 22 search_targets: list[chessai.core.coordinate.Coordinate] | dict[str, typing.Any] | None = None, 23 search_agent: chessai.core.types.Color = chessai.core.types.Color.WHITE) -> None: 24 super().__init__(game_info, save_path, is_replay) 25 26 if (search_targets is None): 27 search_targets = [] 28 29 # Convert the string case into the dict case. 30 if (isinstance(search_targets, str)): 31 search_targets = { 32 chessai.core.coordinate.COORDINATES_KEY: search_targets 33 } 34 35 if (isinstance(search_targets, dict)): 36 search_targets = chessai.core.coordinate.coordinates_from_dict(search_targets) 37 38 self.search_targets: list[chessai.core.coordinate.Coordinate] = search_targets 39 """ The search targets of this game. """ 40 41 self.search_agent: chessai.core.types.Color = search_agent 42 """ The agent completing the tour search, who is always the agent with the first move. """ 43 44 def process_args(self, args: argparse.Namespace) -> None: 45 if (args.search_targets is not None): 46 search_targets = { 47 chessai.core.coordinate.COORDINATES_KEY: args.search_targets 48 } 49 50 self.search_targets = chessai.core.coordinate.coordinates_from_dict(search_targets) 51 52 def get_initial_state(self, 53 rng: random.Random, 54 fen: str | None = None) -> chessai.core.gamestate.GameState: 55 if (len(self.search_targets) == 0): 56 # Let the gamestate parse the FEN so we can look for search targets from a file. 57 initial_state = chessai.tour.gamestate.GameState.from_fen(fen = fen) 58 self.search_targets = initial_state.search_targets 59 else: 60 initial_state = chessai.tour.gamestate.GameState.from_fen(fen = fen, search_targets = self.search_targets) 61 62 # The search agent is always the agent with the first move. 63 self.search_agent = initial_state.turn 64 65 for search_target in self.search_targets: 66 initial_state.board.set(chessai.tour.piece.Target(initial_state.turn.opposite()), search_target) 67 68 return initial_state
14class Game(chessai.chess.game.Game): 15 """ 16 A tour game where the objective is to navigate a piece to the designated coordinates. 17 """ 18 19 def __init__(self, 20 game_info: chessai.core.game.GameInfo, 21 save_path: str | None = None, 22 is_replay: bool = False, 23 search_targets: list[chessai.core.coordinate.Coordinate] | dict[str, typing.Any] | None = None, 24 search_agent: chessai.core.types.Color = chessai.core.types.Color.WHITE) -> None: 25 super().__init__(game_info, save_path, is_replay) 26 27 if (search_targets is None): 28 search_targets = [] 29 30 # Convert the string case into the dict case. 31 if (isinstance(search_targets, str)): 32 search_targets = { 33 chessai.core.coordinate.COORDINATES_KEY: search_targets 34 } 35 36 if (isinstance(search_targets, dict)): 37 search_targets = chessai.core.coordinate.coordinates_from_dict(search_targets) 38 39 self.search_targets: list[chessai.core.coordinate.Coordinate] = search_targets 40 """ The search targets of this game. """ 41 42 self.search_agent: chessai.core.types.Color = search_agent 43 """ The agent completing the tour search, who is always the agent with the first move. """ 44 45 def process_args(self, args: argparse.Namespace) -> None: 46 if (args.search_targets is not None): 47 search_targets = { 48 chessai.core.coordinate.COORDINATES_KEY: args.search_targets 49 } 50 51 self.search_targets = chessai.core.coordinate.coordinates_from_dict(search_targets) 52 53 def get_initial_state(self, 54 rng: random.Random, 55 fen: str | None = None) -> chessai.core.gamestate.GameState: 56 if (len(self.search_targets) == 0): 57 # Let the gamestate parse the FEN so we can look for search targets from a file. 58 initial_state = chessai.tour.gamestate.GameState.from_fen(fen = fen) 59 self.search_targets = initial_state.search_targets 60 else: 61 initial_state = chessai.tour.gamestate.GameState.from_fen(fen = fen, search_targets = self.search_targets) 62 63 # The search agent is always the agent with the first move. 64 self.search_agent = initial_state.turn 65 66 for search_target in self.search_targets: 67 initial_state.board.set(chessai.tour.piece.Target(initial_state.turn.opposite()), search_target) 68 69 return initial_state
A tour game where the objective is to navigate a piece to the designated coordinates.
Game( game_info: chessai.core.game.GameInfo, save_path: str | None = None, is_replay: bool = False, search_targets: list[chessai.core.coordinate.Coordinate] | dict[str, typing.Any] | None = None, search_agent: chessai.core.types.Color = Color.WHITE)
19 def __init__(self, 20 game_info: chessai.core.game.GameInfo, 21 save_path: str | None = None, 22 is_replay: bool = False, 23 search_targets: list[chessai.core.coordinate.Coordinate] | dict[str, typing.Any] | None = None, 24 search_agent: chessai.core.types.Color = chessai.core.types.Color.WHITE) -> None: 25 super().__init__(game_info, save_path, is_replay) 26 27 if (search_targets is None): 28 search_targets = [] 29 30 # Convert the string case into the dict case. 31 if (isinstance(search_targets, str)): 32 search_targets = { 33 chessai.core.coordinate.COORDINATES_KEY: search_targets 34 } 35 36 if (isinstance(search_targets, dict)): 37 search_targets = chessai.core.coordinate.coordinates_from_dict(search_targets) 38 39 self.search_targets: list[chessai.core.coordinate.Coordinate] = search_targets 40 """ The search targets of this game. """ 41 42 self.search_agent: chessai.core.types.Color = search_agent 43 """ The agent completing the tour search, who is always the agent with the first move. """
search_agent: chessai.core.types.Color
The agent completing the tour search, who is always the agent with the first move.
def
process_args(self, args: argparse.Namespace) -> None:
45 def process_args(self, args: argparse.Namespace) -> None: 46 if (args.search_targets is not None): 47 search_targets = { 48 chessai.core.coordinate.COORDINATES_KEY: args.search_targets 49 } 50 51 self.search_targets = chessai.core.coordinate.coordinates_from_dict(search_targets)
Process any special arguments from the command-line.
def
get_initial_state( self, rng: random.Random, fen: str | None = None) -> chessai.core.gamestate.GameState:
53 def get_initial_state(self, 54 rng: random.Random, 55 fen: str | None = None) -> chessai.core.gamestate.GameState: 56 if (len(self.search_targets) == 0): 57 # Let the gamestate parse the FEN so we can look for search targets from a file. 58 initial_state = chessai.tour.gamestate.GameState.from_fen(fen = fen) 59 self.search_targets = initial_state.search_targets 60 else: 61 initial_state = chessai.tour.gamestate.GameState.from_fen(fen = fen, search_targets = self.search_targets) 62 63 # The search agent is always the agent with the first move. 64 self.search_agent = initial_state.turn 65 66 for search_target in self.search_targets: 67 initial_state.board.set(chessai.tour.piece.Target(initial_state.turn.opposite()), search_target) 68 69 return initial_state
Create the initial state for this game.