def legal_drop_squares_mask(self) -> chess.Bitboard: king = self.king(self.turn) if king is None: return ~self.occupied king_attackers = self.attackers_mask(not self.turn, king) if not king_attackers: return ~self.occupied elif chess.popcount(king_attackers) == 1: return chess.between(king, chess.msb(king_attackers)) & ~self.occupied else: return chess.BB_EMPTY
def slide_move(board: chess.Board, move: chess.Move) -> Optional[chess.Move]: # We iterate longest to shortest so the revised move is the longest pseudo-legal move. # If the to-square < from-square then we want our list in sorted order. # Otherwise we want it in reverse order. # In either case, we need to add the to-square to the front of our list manually. squares = chess.SquareSet(chess.between(move.from_square, move.to_square)) if move.to_square > move.from_square: squares = reversed(squares) for slide_square in [move.to_square] + list(squares): revised = chess.Move(move.from_square, slide_square, move.promotion) if board.is_pseudo_legal(revised): return revised return None
def slide_move(board: chess.Board, move: chess.Move) -> Optional[chess.Move]: psuedo_legal_moves = list(board.generate_pseudo_legal_moves()) squares = list( chess.SquareSet(chess.between(move.from_square, move.to_square))) + [move.to_square] squares = sorted(squares, key=lambda s: chess.square_distance(s, move.from_square), reverse=True) for slide_square in squares: revised = chess.Move(move.from_square, slide_square, move.promotion) if revised in psuedo_legal_moves: return revised return None
def is_illegal_castle(board: chess.Board, move: chess.Move) -> bool: if not board.is_castling(move): return False # illegal without kingside rights if board.is_kingside_castling(move) and not board.has_kingside_castling_rights(board.turn): return True # illegal without queenside rights if board.is_queenside_castling(move) and not board.has_queenside_castling_rights(board.turn): return True # illegal if any pieces are between king & rook rook_square = chess.square(7 if board.is_kingside_castling(move) else 0, chess.square_rank(move.from_square)) between_squares = chess.SquareSet(chess.between(move.from_square, rook_square)) if any(map(lambda s: board.piece_at(s), between_squares)): return True # its legal return False