-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathracing_kings_actions.py
59 lines (42 loc) · 2 KB
/
racing_kings_actions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""
Created on February 26 2021
@author: Andreas Spanopoulos
Implements a class used as an interface between the actual moves of a Racing Kings chess game, and
how they are perceived as actions by a chess agent. It inherits the MoveTranslator() class.
"""
from src.environment.actions.action_representations import MoveTranslator
class RacingKingsActions(MoveTranslator):
""" interface between actual Racing Kings Chess moves and their corresponding IDs """
def __init__(self):
""" constructor """
MoveTranslator.__init__(self,
files=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
ranks=['1', '2', '3', '4', '5', '6', '7', '8'])
def _build_id_and_move_lookups(self):
"""
:return: Dictionaries that map moves to their respective IDs and vice-versa.
:rtype: tuple(dict, dict)
The dictionaries look like this: move_to_id['a1a2'] = 0,
coordinate_to_square[1] = 'a1a3'
"""
move_id = 0
move_to_id = {}
id_to_move = {}
for coordinate, square in self._coordinate_to_square.items():
squares_reachable_by_queen_move = self._squares_queens_move_away(coordinate)
squares_reachable_by_knight_move = self._squares_knights_move_away(coordinate)
reachable_squares = squares_reachable_by_queen_move + squares_reachable_by_knight_move
for reachable_square in reachable_squares:
move = square + reachable_square
move_to_id[move] = move_id
id_to_move[move_id] = move
move_id += 1
return move_to_id, id_to_move
@property
def legal_moves_upper_bound(self):
"""
:return: The max number of legal moves that a Racing Kings player may have at any state.
:rtype: int
"""
# 14 legal moves per rook, 14 per bishop, 8 per knight, 28 for the queen and 6 for the king
return 106