Python:使用 python-chess 库中的 stockfish 评估棋盘位置

Python: Evaluating a board position using stockfish from the python-chess library

我正在尝试创建一个引擎,但我的评估函数很糟糕,所以我决定使用 stockfish 来为我评估它。

import chess
import chess.engine

def stockfish_evaluation(board, time_limit = 0.01):
    engine = chess.engine.SimpleEngine.popen_uci("stockfish/stockfish_10_x64")
    result = engine.play(board, chess.engine.Limit(time=time_limit))
    return result.info["score"]

board = chess.Board("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
result = stockfish_evaluation(board)
print(result)

但它打印出来:

KeyError: 'score'

我认为你用错了函数,应该是engine.analyse而不是engine.play

import chess
import chess.engine

def stockfish_evaluation(board, time_limit = 0.01):
    engine = chess.engine.SimpleEngine.popen_uci("stockfish_10_x64")
    result = engine.analyse(board, chess.engine.Limit(time=time_limit))
    return result['score']

board = chess.Board("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
result = stockfish_evaluation(board)
print(result)

#+58