Exemplo n.º 1
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.º 2
0
def evaluate(board: chess.Board, evaluator: Callable[[float, float, float],
                                                     float]):
    # for every piece that is still alive award player 100 points
    result = board.legal_moves.count() * 100

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

    if board.has_insufficient_material(chess.WHITE):
        result -= 1000

    # add material advantage that white has
    result += diff_pieces(board)

    # if on white then return as normal since white Maximizes
    if board.turn is chess.WHITE:
        return result
    # if black then negate for minimization
    else:
        return -result