def num_our_pieces_attacking_their_king_ring(self): count = 0 for piece in chess.scan_reversed( self.board.occupied_co[self.our_color]): piece_attacks_mask = self.board.attacks_mask(piece) count += bool(self._their_king_ring_and_king_mask & piece_attacks_mask) return count
def hash_board(self, board: chess.BaseBoard) -> int: zobrist_hash = 0 for pivot, squares in enumerate(board.occupied_co): for square in chess.scan_reversed(squares): piece_index = (typing.cast(chess.PieceType, board.piece_type_at(square)) - 1) * 2 + pivot zobrist_hash ^= self.array[64 * piece_index + square] return zobrist_hash
def hash_board(self, board): zobrist_hash = 0 for pivot, squares in enumerate(board.occupied_co): for square in chess.scan_reversed(squares): piece_index = (board.piece_type_at(square) - 1) * 2 + pivot zobrist_hash ^= self.array[64 * piece_index + square] return zobrist_hash
def hash_board(self, board): zobrist_hash = 0 for pivot, squares in enumerate(board.occupied_co): for square in chess.scan_reversed(squares): piece_index = (board.piece_type_at(square) - 1) * 2 + pivot zobrist_hash ^= self.array[64 * piece_index + square] return zobrist_hash
def board_zobrist_hash(board): zobrist_hash = 0 for pivot, squares in enumerate(board.occupied_co): for square in chess.scan_reversed(squares): piece_index = (board.piece_type_at(square) - 1) * 2 + pivot zobrist_hash ^= POLYGLOT_RANDOM_ARRAY[64 * piece_index + square] return zobrist_hash
def _pieces_attacked(self): board = self.aug.copy() board.push(self.move) pieces_attacked_mask = board.attacks_mask(self.move.to_square) pieces_attacked_mask &= board.occupied_co(board.current_color) return [ board.piece_type_at(s) for s in chess.scan_reversed(pieces_attacked_mask) ]
def convert_to_int(board, white_kcastle, black_kcastle, white_qcastle, black_qcastle): white_pos = [None] * 64 black_pos = [None] * 64 for sq in chess.scan_reversed( board.occupied_co[chess.WHITE]): # Check if white white_pos[sq] = board.piece_type_at(sq) for sq in chess.scan_reversed( board.occupied_co[chess.BLACK]): # Check if black black_pos[sq] = board.piece_type_at(sq) white_map = [0 if v is None else v for v in white_pos] black_map = [0 if v is None else v for v in black_pos] matrix = np.zeros((7, 8, 8)) counter = 0 for i in range(len(matrix[0])): for j in range(len(matrix[0][0])): matrix[0][i][j] = white_map[counter] matrix[3][i][j] = black_map[counter] counter += 1 # Turn if board.turn: matrix[6].fill(0) else: matrix[6].fill(1) # Check for castling rights if white_kcastle: matrix[1].fill(1) if white_qcastle: matrix[2].fill(1) if black_kcastle: matrix[4].fill(1) if black_qcastle: matrix[5].fill(1) return matrix
def hash(self, board: chess.Board, moves_left: int): zobrist_hash = 0 zobrist_hash ^= self.__moves[moves_left] for squares in board.occupied_co: for square in chess.scan_reversed(squares): piece: chess.Piece = board.piece_at(square) piece_index = piece.piece_type if piece.color: piece_index *= 2 zobrist_hash ^= self.__table[square][piece_index - 1] return zobrist_hash
def generate_moves(self, from_mask=BB_ALL, to_mask=BB_ALL): our_pieces = self.occupied_co[self.turn] # Generate piece moves. non_pawns = our_pieces & ~self.pawns & from_mask for from_square in scan_reversed(non_pawns): moves = self.attacks_mask(from_square) & ~our_pieces & to_mask for to_square in scan_reversed(moves): yield Move(from_square, to_square) # Generate castling moves. if from_mask & self.kings: yield from self.generate_castling_moves(from_mask, to_mask) # The remaining moves are all pawn moves. pawns = self.pawns & self.occupied_co[self.turn] & from_mask if not pawns: return # Generate pawn captures. capturers = pawns for from_square in scan_reversed(capturers): # All pawn captures are now pseudo-legal targets = (BB_PAWN_ATTACKS[self.turn][from_square] & to_mask) for to_square in scan_reversed(targets): if square_rank(to_square) in [0, 7]: yield Move(from_square, to_square, QUEEN) yield Move(from_square, to_square, ROOK) yield Move(from_square, to_square, BISHOP) yield Move(from_square, to_square, KNIGHT) else: yield Move(from_square, to_square) # Prepare pawn advance generation. if self.turn == WHITE: single_moves = pawns << 8 & ~self.occupied double_moves = single_moves << 8 & ~self.occupied & (BB_RANK_3 | BB_RANK_4) else: single_moves = pawns >> 8 & ~self.occupied double_moves = single_moves >> 8 & ~self.occupied & (BB_RANK_6 | BB_RANK_5) single_moves &= to_mask double_moves &= to_mask # Generate single pawn moves. for to_square in scan_reversed(single_moves): from_square = to_square + (8 if self.turn == BLACK else -8) if square_rank(to_square) in [0, 7]: yield Move(from_square, to_square, QUEEN) yield Move(from_square, to_square, ROOK) yield Move(from_square, to_square, BISHOP) yield Move(from_square, to_square, KNIGHT) else: yield Move(from_square, to_square) # Generate double pawn moves. for to_square in scan_reversed(double_moves): from_square = to_square + (16 if self.turn == BLACK else -16) yield Move(from_square, to_square)
def df(cls, board): attacks = [] for attacking in chess.scan_reversed(board.occupied): for attacked in board.attacks(attacking): attacks.append(Attack(attacking, attacked).__dict__) return pd.DataFrame(attacks)
def df(cls, board): pieces = chess.scan_reversed(board.occupied) df = pd.DataFrame([cls(board, p).__dict__ for p in pieces]) return df.set_index("square")