我正在使用 python 3.6 以及 Jupyter 和 Anaconda。 该脚本根据 PGN 文件在国际象棋游戏中一步一步地移动,并返回一个分数。效果很好。
当返回的分数是“-M8”时,问题就来了,这意味着黑方可以在 8 步内将死。
但是下面这行:
(handler.info["score"][1].cp)
返回“none”而不是预期的“-M8”。
脚本:
import chess
import chess.uci
import chess.pgn
import sys
with open('E:\PGN Files\Fischer.pgn') as f:
handler = chess.uci.InfoHandler()
engine = chess.uci.popen_engine('C:\Program Files (x86)\Arena\Engines\stockfish_9_x64.exe') #give correct address of your engine here
engine.info_handlers.append(handler)
for number in range(10):
game = chess.pgn.read_game(f)
m=0
while not game.is_end():
m=m+1
node = game.variations[0]
board = game.board()
game = node
engine.position(board)
#Set your evaluation time, in ms:
evaltime = 5000 #so 5 seconds
evaluation = engine.go(movetime=evaltime)
score =(handler.info["score"][1].cp)
print(str(score))
获得分数的方法在某处,但这是一种获得分数和分数和伴侣的方法:
engine = chess.engine.SimpleEngine.popen_uci('C:\Program Files (x86)\Arena\Engines\stockfish_9_x64.exe', timeout = 1000.0)
evaltime = 5000 #so 5 seconds
scoreinfo = engine.analyse(board, chess.engine.Limit(time = evaltime))["score"]
cp = scoreinfo.relative.score(mate_score=1000000)
if scoreinfo.is_mate():
if scoreinfo.turn == chess.WHITE:
mate = 1000000 - cp
elif scoreinfo.turn == chess.BLACK:
mate = 1000000 + cp