def get_moves(self, board): moves = [] colorint = color2int(self.color) column, row = self.square.loc for columnint in [-1, 1]: takeloc = (chr(ord(column) + columnint), row + colorint) if (takeloc in board.squares and board.squares[takeloc].has_piece( opposite_color(self.color))): moves.append(takeloc) enpassantloc = (chr(ord(column) + columnint), row) if (enpassantloc in board.squares and board.squares[enpassantloc].has_piece( opposite_color(self.color)) and board.squares[enpassantloc].get_piece().name == 'pawn' and board.squares[enpassantloc].get_piece().double_advance == board.move - 1): moves.append(takeloc) advanceloc = (column, row + colorint) if advanceloc in board.squares and not board.squares[ advanceloc].has_piece(): moves.append(advanceloc) if is_pawn_starting_row(self.color, self.square.loc): advanceloc2 = (column, row + colorint * 2) if not (board.squares[advanceloc2].has_piece() or board.squares[advanceloc].has_piece()): moves.append(advanceloc2) return moves
def in_check(self, color): other_color = opposite_color(color) for piece in self.get_pieces(other_color): if (piece.name != 'king' and # king can't give check self.get_king_loc(color) in piece.get_moves(self)): return True return False
def make_decision(self, board, color): activity_mat = self.pieces2activity_mat( board.pieces[color], board.pieces[opposite_color(color)]) self.propagate(activity_mat) output_activity_mat = self.layer2activity_mat(self.output_layer) piece, move = self.activity_mat2move(output_activity_mat, board, board.pieces[color]) board.make_move(piece, move) self.check_promotion_or_game_end(board)
def score_position(self, color): score = 0 opposite_king_loc = self.get_king_loc(opposite_color(color)) opposite_king_moves = self.get_moves( self.pieces[opposite_color(color)]['king'][0]) for name in self.pieces[color]: score += len(self.pieces[color][name]) * self.score_dict[ name] # Piece score for piece in self.pieces[color][name]: for move in self.get_moves(piece): if move == opposite_king_loc and not opposite_king_moves: # checkmate score += np.inf elif move in opposite_king_moves or move == opposite_king_loc: # king pressure score += 1 for name in self.pieces[opposite_color(color)]: score -= len( self.pieces[opposite_color(color)][name]) * self.score_dict[ name] # Opponent piece negative score return score
def check_check_mate(self): if self.check_repeated_moves(): self.game_over = True return 'Draw by repetition' color = int2color(self.move) # potentially checkmated player's turn for piece in self.get_pieces(color): if len(self.get_moves(piece)) > 0: return False self.game_over = True return 'Check mate %s' % opposite_color(color) if self.in_check( color) else 'Draw by stalemate'
def get_direction_moves(self, board, directions, one_move=False): moves = [] for direction in directions: loc = move_loc(self.square.loc, direction) while (loc in board.squares and not board.squares[loc].has_piece()): moves.append(loc) if one_move: break loc = move_loc(loc, direction) if (loc in board.squares and # capturing case board.squares[loc].has_piece(opposite_color(self.color))): moves.append(loc) return moves
def draw(self, canvas, ss, coords): colorint = color2int(self.color) colorint2 = self.color == 'white' column, row = self.square.loc column, row = loc2int(column, row) coords = [(colorint2 - c) * colorint for c in coords] coords = [ ss * row + ss * c if i % 2 else ss * column + ss * c for i, c in enumerate(coords) ] self.display = canvas.create_polygon(coords, fill=self.color, outline=opposite_color( self.color), tag='piece')
def train_king_hunt(self, n_games=1000): for n in tqdm(range(n_games)): board = Board() while not board.game_over and board.move < MAX_MOVES: color = int2color(board.move) activity_mat = self.pieces2activity_mat( board.pieces[color], board.pieces[opposite_color(color)]) self.propagate(activity_mat) output_activity_mat = self.layer2activity_mat( self.output_layer) piece, move = self.activity_mat2move(output_activity_mat, board, board.pieces[color]) print(piece.name, piece.square.loc, move) board.make_move(piece, move) score = board.score_position(color) output_loc = self.piece2output_layer(piece) self.back_propagate(self.output_layer[output_loc], score, 0)
def get_moves(self, board): moves = [] for first_direction in ['up', 'down', 'left', 'right']: if first_direction in ['up', 'down']: second_directions = ['left', 'right'] elif first_direction in ['left', 'right']: second_directions = ['up', 'down'] else: raise ValueError('Unrecognized direction') for second_direction in second_directions: loc = move_loc(self.square.loc, first_direction) loc = move_loc(loc, second_direction) loc = move_loc(loc, second_direction) if (loc in board.squares and (not board.squares[loc].has_piece() or board.squares[loc].has_piece( opposite_color(self.color)))): moves.append(loc) return moves