chessai.puzzle.gamestate

 1import copy
 2import typing
 3
 4import edq.util.serial
 5
 6import chessai.chess.piece
 7import chessai.chess.gamestate
 8import chessai.core.action
 9import chessai.core.board
10import chessai.core.gamestate
11import chessai.core.parser
12import chessai.core.types
13import chessai.puzzle.parser
14
15class GameState(chessai.chess.gamestate.GameState):
16    """ A game state specific to a Puzzle game. """
17
18    def __init__(self,
19                 board: chessai.core.board.Board,
20                 turn: chessai.core.types.Color,
21                 castling_rights: chessai.core.castling.CastlingRights,
22                 en_passant_coordinate: chessai.core.coordinate.Coordinate | None = None,
23                 halfmove_clock: int = 0,
24                 fullmove_number: int = 1,
25                 previous_action: chessai.core.action.Action | None = None,
26                 seed: int = -1,
27                 game_over: bool = False,
28                 puzzle_solved: bool = False,
29                 _capture_move_lines: bool = False,
30                 **kwargs: typing.Any) -> None:
31        super().__init__(board, turn, castling_rights, en_passant_coordinate,
32                         halfmove_clock, fullmove_number, previous_action,
33                         seed, game_over, **kwargs)
34
35        self.puzzle_solved: bool = puzzle_solved
36        """ Set to True when the agent has successfully completed one of the move lines. """
37
38        self.dummy_player: chessai.core.types.Color = self.turn.opposite()
39        """ The dummy player for the current game, which is the opponent to the puzzle's starting player. """
40
41        if _capture_move_lines:
42            move_lines = kwargs.get(chessai.puzzle.parser.MOVE_LINES_KEY, [])
43        else:
44            move_lines = []
45
46        self._move_lines: list[list[chessai.core.action.Action]] = move_lines
47        """ The move lines for the puzzle. """
48
49    def game_complete(self) -> tuple[list[chessai.core.types.Color], float]:
50        """
51        Determine the outcome of the puzzle.
52
53        The puzzle agent wins with score 1.0 if they fully completed one of the valid move lines.
54        Otherwise the agent loses with score 0.0.
55        """
56
57        agent = self.dummy_player.opposite()
58
59        if self.puzzle_solved:
60            return ([agent], 1.0)
61
62        return ([self.dummy_player], 0.0)
63
64    def copy(self,
65            context: typing.Union[edq.util.serial.SerializationContext, None] = None,
66            ) -> 'GameState':
67        new_state = super().copy()
68        new_state = typing.cast(GameState, new_state)
69
70        new_state.puzzle_solved = self.puzzle_solved
71        new_state._move_lines = copy.deepcopy(self._move_lines)
72
73        return new_state
74
75    @classmethod
76    def get_gamestate_parser(cls) -> chessai.core.parser.GameStateParser:
77        return chessai.puzzle.parser.parse_puzzle
class GameState(chessai.chess.gamestate.GameState):
16class GameState(chessai.chess.gamestate.GameState):
17    """ A game state specific to a Puzzle game. """
18
19    def __init__(self,
20                 board: chessai.core.board.Board,
21                 turn: chessai.core.types.Color,
22                 castling_rights: chessai.core.castling.CastlingRights,
23                 en_passant_coordinate: chessai.core.coordinate.Coordinate | None = None,
24                 halfmove_clock: int = 0,
25                 fullmove_number: int = 1,
26                 previous_action: chessai.core.action.Action | None = None,
27                 seed: int = -1,
28                 game_over: bool = False,
29                 puzzle_solved: bool = False,
30                 _capture_move_lines: bool = False,
31                 **kwargs: typing.Any) -> None:
32        super().__init__(board, turn, castling_rights, en_passant_coordinate,
33                         halfmove_clock, fullmove_number, previous_action,
34                         seed, game_over, **kwargs)
35
36        self.puzzle_solved: bool = puzzle_solved
37        """ Set to True when the agent has successfully completed one of the move lines. """
38
39        self.dummy_player: chessai.core.types.Color = self.turn.opposite()
40        """ The dummy player for the current game, which is the opponent to the puzzle's starting player. """
41
42        if _capture_move_lines:
43            move_lines = kwargs.get(chessai.puzzle.parser.MOVE_LINES_KEY, [])
44        else:
45            move_lines = []
46
47        self._move_lines: list[list[chessai.core.action.Action]] = move_lines
48        """ The move lines for the puzzle. """
49
50    def game_complete(self) -> tuple[list[chessai.core.types.Color], float]:
51        """
52        Determine the outcome of the puzzle.
53
54        The puzzle agent wins with score 1.0 if they fully completed one of the valid move lines.
55        Otherwise the agent loses with score 0.0.
56        """
57
58        agent = self.dummy_player.opposite()
59
60        if self.puzzle_solved:
61            return ([agent], 1.0)
62
63        return ([self.dummy_player], 0.0)
64
65    def copy(self,
66            context: typing.Union[edq.util.serial.SerializationContext, None] = None,
67            ) -> 'GameState':
68        new_state = super().copy()
69        new_state = typing.cast(GameState, new_state)
70
71        new_state.puzzle_solved = self.puzzle_solved
72        new_state._move_lines = copy.deepcopy(self._move_lines)
73
74        return new_state
75
76    @classmethod
77    def get_gamestate_parser(cls) -> chessai.core.parser.GameStateParser:
78        return chessai.puzzle.parser.parse_puzzle

A game state specific to a Puzzle game.

GameState( board: chessai.core.board.Board, turn: chessai.core.types.Color, castling_rights: chessai.core.castling.CastlingRights, en_passant_coordinate: chessai.core.coordinate.Coordinate | None = None, halfmove_clock: int = 0, fullmove_number: int = 1, previous_action: chessai.core.action.Action | None = None, seed: int = -1, game_over: bool = False, puzzle_solved: bool = False, _capture_move_lines: bool = False, **kwargs: Any)
19    def __init__(self,
20                 board: chessai.core.board.Board,
21                 turn: chessai.core.types.Color,
22                 castling_rights: chessai.core.castling.CastlingRights,
23                 en_passant_coordinate: chessai.core.coordinate.Coordinate | None = None,
24                 halfmove_clock: int = 0,
25                 fullmove_number: int = 1,
26                 previous_action: chessai.core.action.Action | None = None,
27                 seed: int = -1,
28                 game_over: bool = False,
29                 puzzle_solved: bool = False,
30                 _capture_move_lines: bool = False,
31                 **kwargs: typing.Any) -> None:
32        super().__init__(board, turn, castling_rights, en_passant_coordinate,
33                         halfmove_clock, fullmove_number, previous_action,
34                         seed, game_over, **kwargs)
35
36        self.puzzle_solved: bool = puzzle_solved
37        """ Set to True when the agent has successfully completed one of the move lines. """
38
39        self.dummy_player: chessai.core.types.Color = self.turn.opposite()
40        """ The dummy player for the current game, which is the opponent to the puzzle's starting player. """
41
42        if _capture_move_lines:
43            move_lines = kwargs.get(chessai.puzzle.parser.MOVE_LINES_KEY, [])
44        else:
45            move_lines = []
46
47        self._move_lines: list[list[chessai.core.action.Action]] = move_lines
48        """ The move lines for the puzzle. """
puzzle_solved: bool

Set to True when the agent has successfully completed one of the move lines.

dummy_player: chessai.core.types.Color

The dummy player for the current game, which is the opponent to the puzzle's starting player.

def game_complete(self) -> tuple[list[chessai.core.types.Color], float]:
50    def game_complete(self) -> tuple[list[chessai.core.types.Color], float]:
51        """
52        Determine the outcome of the puzzle.
53
54        The puzzle agent wins with score 1.0 if they fully completed one of the valid move lines.
55        Otherwise the agent loses with score 0.0.
56        """
57
58        agent = self.dummy_player.opposite()
59
60        if self.puzzle_solved:
61            return ([agent], 1.0)
62
63        return ([self.dummy_player], 0.0)

Determine the outcome of the puzzle.

The puzzle agent wins with score 1.0 if they fully completed one of the valid move lines. Otherwise the agent loses with score 0.0.

def copy( self, context: Optional[edq.util.common.SerializationContext] = None) -> GameState:
65    def copy(self,
66            context: typing.Union[edq.util.serial.SerializationContext, None] = None,
67            ) -> 'GameState':
68        new_state = super().copy()
69        new_state = typing.cast(GameState, new_state)
70
71        new_state.puzzle_solved = self.puzzle_solved
72        new_state._move_lines = copy.deepcopy(self._move_lines)
73
74        return new_state

Get a deep copy of this state.

Child classes are responsible for making any deep copies they need to.

@classmethod
def get_gamestate_parser(cls) -> chessai.core.parser.GameStateParser:
76    @classmethod
77    def get_gamestate_parser(cls) -> chessai.core.parser.GameStateParser:
78        return chessai.puzzle.parser.parse_puzzle

Get the parser that can convert user inputs into a gamestate.