def is_defended(board: Board, piece: Piece, square: Square) -> bool: if board.attackers(piece.color, square): return True # ray defense https://lichess.org/editor/6k1/3q1pbp/2b1p1p1/1BPp4/rp1PnP2/4PRNP/4Q1P1/4B1K1_w_-_-_0_1 for attacker in board.attackers(not piece.color, square): if board.piece_at(attacker).piece_type in ray_piece_types: bc = board.copy(stack = False) bc.remove_piece_at(attacker) if bc.attackers(piece.color, square): return True return False
def is_in_bad_spot(board: Board, square: Square) -> bool: # hanging or takeable by lower piece piece = board.piece_at(square) assert (piece) return (bool(board.attackers(not piece.color, square)) and (is_hanging(board, piece, square) or can_be_taken_by_lower_piece(board, piece, square)))
def can_be_taken_by_lower_piece(board: Board, piece: Piece, square: Square) -> bool: for attacker_square in board.attackers(not piece.color, square): attacker = board.piece_at(attacker_square) assert attacker if attacker.piece_type != chess.KING and values[ attacker.piece_type] < values[piece.piece_type]: return True return False
def evaluate(board: chess.Board): ENEMY = {chess.WHITE: chess.BLACK, chess.BLACK: chess.WHITE} score = 0 if board.result() == "1-0" or board.result() == "0-1": score += 9999 if board.is_check(): score += 10 for sq in chess.SQUARES: score += len(board.attackers(board.turn, sq)) * VALUE[board.piece_type_at(sq)] score -= len(board.attackers(ENEMY[board.turn], sq)) * VALUE[board.piece_type_at(sq)] return score
def get_attacking_pieces(board: chess.Board, attacking_color: chess.Color, square: chess.Square) -> [chess.PieceType]: piece_types = [] attacking_squares = board.attackers(attacking_color, square) for attacking_square in attacking_squares: if board.is_pinned(attacking_color, attacking_square) == False: piece_type = board.piece_type_at(attacking_square) piece_types.append(piece_type) return piece_types
def attacker_pieces(board: Board, color: Color, square: Square) -> List[Piece]: return [ p for p in [board.piece_at(s) for s in board.attackers(color, square)] if p ]