Exemplo n.º 1
0
def attacked_opponent_squares(board: Board, from_square: Square, pov: Color) -> List[Tuple[Piece, Square]]:
    pieces = []
    for attacked_square in board.attacks(from_square):
        attacked_piece = board.piece_at(attacked_square)
        if attacked_piece and attacked_piece.color != pov:
            pieces.append((attacked_piece, attacked_square))
    return pieces
Exemplo n.º 2
0
    def __init__(self, board: Board, total_moves: int):
        self.__cache = dict()

        self.enemy_king_color: Color = not board.turn
        enemy_king_square: Square = board.king(self.enemy_king_color)

        self.mating_squares: [int] = list(board.attacks(enemy_king_square))
        self.mating_squares.append(enemy_king_square)

        self.total_moves = total_moves
        self.weight = 4.5 / total_moves
Exemplo n.º 3
0
def get_board_defence_total_value(board: chess.Board) -> float:
    total_value = 0

    for square_defend in chess.SQUARES:
        piece_defend = board.piece_at(square_defend)
        if piece_defend is not None and piece_defend.color == board.turn:
            if board.is_attacked_by(not board.turn, square_defend):

                for square_defendant in chess.SQUARES:
                    piece_defendant = board.piece_at(square_defendant)
                    if piece_defendant is not None and piece_defendant.color == board.turn:

                        if square_defend in board.attacks(square_defendant):
                            total_value += Macros.PIECE_VALUES[
                                piece_defend.
                                piece_type] / Macros.BOARD_INTERVAL_FIX

    return total_value
def pawn_capture_moves_on(board: chess.Board) -> List[chess.Move]:
    """Generates all pawn captures on `board`, even if there is no piece to capture. All promotion moves are included."""
    pawn_capture_moves = []

    for pawn_square in board.pieces(chess.PAWN, board.turn):
        for attacked_square in board.attacks(pawn_square):
            # skip this square if one of our own pieces are on the square
            attacked_piece = board.piece_at(attacked_square)
            if attacked_piece and attacked_piece.color == board.turn:
                continue

            pawn_capture_moves.append(chess.Move(pawn_square, attacked_square))

            # add in promotion moves
            if attacked_square in BACK_RANKS:
                for piece_type in chess.PIECE_TYPES[1:-1]:
                    pawn_capture_moves.append(chess.Move(pawn_square, attacked_square, promotion=piece_type))

    return pawn_capture_moves
Exemplo n.º 5
0
def pawn_capture_moves_on(board: chess.Board) -> List[chess.Move]:
    """Generates all pawn captures on `board`, even if there is no piece to capture. All promotion moves are included."""
    pawn_capture_moves = []

    no_opponents_board = without_opponent_pieces(board)

    for pawn_square in board.pieces(chess.PAWN, board.turn):
        for attacked_square in board.attacks(pawn_square):
            # skip this square if one of our own pieces are on the square
            if no_opponents_board.piece_at(attacked_square):
                continue

            pawn_capture_moves.append(chess.Move(pawn_square, attacked_square))

            # add in promotion moves
            if attacked_square in chess.SquareSet(chess.BB_BACKRANKS):
                for piece_type in chess.PIECE_TYPES[1:-1]:
                    pawn_capture_moves.append(
                        chess.Move(pawn_square,
                                   attacked_square,
                                   promotion=piece_type))

    return pawn_capture_moves
Exemplo n.º 6
0
def evaluateFuzzy(board: chess.Board, evaluator: Callable[[float, float],
                                                          float]):
    player = chess.WHITE if board.turn == True else chess.BLACK
    opponent = chess.WHITE if player == chess.BLACK else chess.BLACK

    total = 0
    # being in check usually means we are going the wrong route
    if board.is_check():
        total -= 1000

    if board.has_insufficient_material(player):
        total -= 1000

    if board.has_insufficient_material(opponent):
        total += 1000

    num_moves_by_piece = {}
    for move in board.legal_moves:
        if move.from_square in num_moves_by_piece:
            num_moves_by_piece[move.from_square] += 1
        else:
            num_moves_by_piece[move.from_square] = 1

    for i in range(1, 7):
        piece_type = board.pieces(i, player)
        num_attacking = 0
        for piece in piece_type:
            movable_spaces = num_moves_by_piece[
                piece] if piece in num_moves_by_piece else 0
            for spot in board.attacks(piece):
                attacked_player = board.color_at(spot)
                if attacked_player == opponent:
                    num_attacking += 1
            total += i + evaluator(movable_spaces, num_attacking)

    return total
Exemplo n.º 7
0
 def _check_attack(self, board: chess.Board, move: chess.Move):
     possible_attacks = board.attacks(move.to_square)
     if move.from_square in possible_attacks:
         self._attacked = True
Exemplo n.º 8
0
def __is_attacked(board: chess.Board, square: chess.Square):

    return len(board.attacks(square)) > 1