chessai.agents.cheating

 1import chessai.chess.piece
 2import chessai.core.action
 3import chessai.core.agent
 4import chessai.core.coordinate
 5import chessai.core.gamestate
 6
 7class CheatingAgent(chessai.core.agent.Agent):
 8    """
 9    This agent cheats!
10    On its turn, this agent will try to modify the game state to declare itself the winner.
11    Agent isolation should prevent this agent from cheating.
12    """
13
14    def get_action(self, state: chessai.core.gamestate.GameState) -> chessai.core.action.Action:
15        enemy_king_coordinate = None
16
17        # Remove all opposing pieces, except the king.
18        for piece_position in state.get_coordinate_map(state.turn.opposite()):
19            piece = state.get(piece_position)
20            if isinstance(piece, chessai.chess.piece.King):
21                enemy_king_coordinate = piece_position
22                continue
23
24            state.board.remove(piece_position)
25
26        # Place queens all around the enemy king.
27        if (enemy_king_coordinate is not None):
28            for file_delta in range(-1, 2):
29                for rank_delta in range(-1, 2):
30                    # Don't replace the enemy king.
31                    if (file_delta == 0) and (rank_delta == 0):
32                        continue
33
34                    file = enemy_king_coordinate.file - file_delta
35                    rank = enemy_king_coordinate.rank - rank_delta
36                    if not state.board.is_within_bounds(file, rank):
37                        continue
38
39                    state.board.set(chessai.chess.piece.Queen(state.turn),
40                                    chessai.core.coordinate.Coordinate(file, rank))
41
42        # Prioritize movement actions.
43        legal_actions = state.get_legal_move_actions()
44
45        # Fall back to all legal actions if there are no legal movement actions.
46        if (len(legal_actions) == 0):
47            legal_actions = state.get_legal_actions()
48
49        # Try to move one of the queens without capturing the king.
50        if (enemy_king_coordinate is not None):
51            for action in legal_actions:
52                if not isinstance(action, chessai.core.action.MoveAction):
53                    continue
54
55                if (action.end_coordinate == enemy_king_coordinate):
56                    continue
57
58                # All of our queens must be near the enemy king.
59                file_delta = abs(action.start_coordinate.file - enemy_king_coordinate.file)
60                if file_delta > 1:
61                    continue
62
63                rank_delta = abs(action.start_coordinate.rank - enemy_king_coordinate.rank)
64                if rank_delta > 1:
65                    continue
66
67                return action
68
69        # Take any remaining random action.
70        return self.rng.choice(legal_actions)
class CheatingAgent(chessai.core.agent.Agent):
 8class CheatingAgent(chessai.core.agent.Agent):
 9    """
10    This agent cheats!
11    On its turn, this agent will try to modify the game state to declare itself the winner.
12    Agent isolation should prevent this agent from cheating.
13    """
14
15    def get_action(self, state: chessai.core.gamestate.GameState) -> chessai.core.action.Action:
16        enemy_king_coordinate = None
17
18        # Remove all opposing pieces, except the king.
19        for piece_position in state.get_coordinate_map(state.turn.opposite()):
20            piece = state.get(piece_position)
21            if isinstance(piece, chessai.chess.piece.King):
22                enemy_king_coordinate = piece_position
23                continue
24
25            state.board.remove(piece_position)
26
27        # Place queens all around the enemy king.
28        if (enemy_king_coordinate is not None):
29            for file_delta in range(-1, 2):
30                for rank_delta in range(-1, 2):
31                    # Don't replace the enemy king.
32                    if (file_delta == 0) and (rank_delta == 0):
33                        continue
34
35                    file = enemy_king_coordinate.file - file_delta
36                    rank = enemy_king_coordinate.rank - rank_delta
37                    if not state.board.is_within_bounds(file, rank):
38                        continue
39
40                    state.board.set(chessai.chess.piece.Queen(state.turn),
41                                    chessai.core.coordinate.Coordinate(file, rank))
42
43        # Prioritize movement actions.
44        legal_actions = state.get_legal_move_actions()
45
46        # Fall back to all legal actions if there are no legal movement actions.
47        if (len(legal_actions) == 0):
48            legal_actions = state.get_legal_actions()
49
50        # Try to move one of the queens without capturing the king.
51        if (enemy_king_coordinate is not None):
52            for action in legal_actions:
53                if not isinstance(action, chessai.core.action.MoveAction):
54                    continue
55
56                if (action.end_coordinate == enemy_king_coordinate):
57                    continue
58
59                # All of our queens must be near the enemy king.
60                file_delta = abs(action.start_coordinate.file - enemy_king_coordinate.file)
61                if file_delta > 1:
62                    continue
63
64                rank_delta = abs(action.start_coordinate.rank - enemy_king_coordinate.rank)
65                if rank_delta > 1:
66                    continue
67
68                return action
69
70        # Take any remaining random action.
71        return self.rng.choice(legal_actions)

This agent cheats! On its turn, this agent will try to modify the game state to declare itself the winner. Agent isolation should prevent this agent from cheating.

def get_action( self, state: chessai.core.gamestate.GameState) -> chessai.core.action.Action:
15    def get_action(self, state: chessai.core.gamestate.GameState) -> chessai.core.action.Action:
16        enemy_king_coordinate = None
17
18        # Remove all opposing pieces, except the king.
19        for piece_position in state.get_coordinate_map(state.turn.opposite()):
20            piece = state.get(piece_position)
21            if isinstance(piece, chessai.chess.piece.King):
22                enemy_king_coordinate = piece_position
23                continue
24
25            state.board.remove(piece_position)
26
27        # Place queens all around the enemy king.
28        if (enemy_king_coordinate is not None):
29            for file_delta in range(-1, 2):
30                for rank_delta in range(-1, 2):
31                    # Don't replace the enemy king.
32                    if (file_delta == 0) and (rank_delta == 0):
33                        continue
34
35                    file = enemy_king_coordinate.file - file_delta
36                    rank = enemy_king_coordinate.rank - rank_delta
37                    if not state.board.is_within_bounds(file, rank):
38                        continue
39
40                    state.board.set(chessai.chess.piece.Queen(state.turn),
41                                    chessai.core.coordinate.Coordinate(file, rank))
42
43        # Prioritize movement actions.
44        legal_actions = state.get_legal_move_actions()
45
46        # Fall back to all legal actions if there are no legal movement actions.
47        if (len(legal_actions) == 0):
48            legal_actions = state.get_legal_actions()
49
50        # Try to move one of the queens without capturing the king.
51        if (enemy_king_coordinate is not None):
52            for action in legal_actions:
53                if not isinstance(action, chessai.core.action.MoveAction):
54                    continue
55
56                if (action.end_coordinate == enemy_king_coordinate):
57                    continue
58
59                # All of our queens must be near the enemy king.
60                file_delta = abs(action.start_coordinate.file - enemy_king_coordinate.file)
61                if file_delta > 1:
62                    continue
63
64                rank_delta = abs(action.start_coordinate.rank - enemy_king_coordinate.rank)
65                if rank_delta > 1:
66                    continue
67
68                return action
69
70        # Take any remaining random action.
71        return self.rng.choice(legal_actions)

Get an action for this agent given the current state of the game. This is simplified version of get_action_full(), see that method for full details.