def __init__(self): self.actual_game = game.Game() self.rollout_game = game.Game() exemplary_move = self.actual_game.get_moves_observation()[0] self.move_shape = tn.move_to_tensor(exemplary_move).shape exemplary_pos = self.actual_game.board.fen() self.position_shape = tn.fen_to_tensor(exemplary_pos).shape
def idx_move(self, move_idx): ''' make a move in index notation in rollout game :param tuple: tuple with three np.arrays of shape (1,) denoting the index of a move in move tensor notation ''' move_fen = tn.tensor_indices_to_move(move_idx) try: self.rollout_game.make_move(move_fen) except: raise ValueError(f"rollout: move {move_fen} \ not possible in position \ {self.rollout_game.board.fen()}") return tn.fen_to_tensor(self.rollout_game.board.fen())
def actual_idx_move(self, move_idx): ''' commits a move in tensor notation in both actual_game and rollout_game ''' move_fen = tn.tensor_indices_to_move(move_idx) self._actual_move(move_fen)
def get_new_position(self, old_pos, move_idx): """ get new position from an old position and a move selected in old position """ old_fen = tn.tensor_to_fen(old_pos) self.rollout_game.board = chess.variant.RacingKingsBoard(old_fen) move_fen = tn.tensor_indices_to_move(move_idx) try: self.rollout_game.make_move(move_fen) except: raise ValueError(f"move {move_fen} \ not possible in position {old_fen}") new_fen = self.rollout_game.board.fen() return tn.fen_to_tensor(new_fen)
def get_legal_moves_from(self, position): ''' get legal moves for a given position :param str position: position in fen notation ''' fen = tn.tensor_to_fen(position) self.rollout_game.board = chess.variant.RacingKingsBoard(fen) return self.get_legal_moves()
def get_legal_moves(self): ''' get legal moves for current rollout state in tensor notation ''' moves = self.rollout_game.get_moves_observation() legal_move_indices = np.zeros((len(moves), 3), dtype=np.uint16) for i, move in enumerate(moves): legal_move_indices[i] = tn.move_to_tensor_indices(move) return tuple(legal_move_indices.T)
# import pytest from Interface import TensorNotation as tn MOVES = ['h2h3', 'h2g3', 'g2g8', 'g2g7',\ 'g2g6', 'g2g5', 'g2g4', 'g2g3',\ 'f2a7', 'f2b6', 'f2c5', 'f2h4',\ 'f2d4', 'f2g3', 'f2e3', 'e2f4',\ 'e2d4', 'e2g3', 'e1f3', 'e1d3', 'e1c2'] for i in MOVES: tensor = tn.move_to_tensor(i) fen = tn.tensor_to_move(tensor) if fen != i: raise ValueError(f"mismatch: original {i}, back-translated {fen}")
def uci_to_move_idx(self, uci): ''' translate uci move to indices of move in tensor notation ''' return tn.move_to_tensor_indices(uci)
def move_index_to_fen(self, move_idx): ''' translate move index to uci notation :return str: move in uci notation ''' return tn.tensor_indices_to_move(move_idx)
def get_actual_position(self): ''' :return np.array: position of actual game in tensor notation ''' return tn.fen_to_tensor(self.actual_game.board.fen())
def get_position(self): ''' :return np.array: position of rollout game in tensor notation ''' return tn.fen_to_tensor(self.rollout_game.board.fen())