我正在尝试使用python编写一个脚本,将国际象棋位置输入到stockfish中,并获得评估结果。
我的问题就是基于这个,How to Communicate with a Chess engine in Python?
问题出在subprocess.pipe上。
import subprocess, time
import os
os.chdir('C:\Users\Michael\Downloads\stockfish-6-win\stockfish-6-win\Windows'
engine = subprocess.Popen('stockfish', universal_newlines=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
def put(command):
print('\nyou:\n\t'+command)
engine.stdin.write(command+'\n')
def get():
# using the 'isready' command (eng has to answer 'readyok')
# to indicate current last line of stdout
engine.stdin.write('isready\n')
print('\nengine:')
while True:
text = engine.stdout.readline().strip()
if text == 'readyok':
break
if text !='':
print('\t'+text)
put('go depth 15')
get()
put('eval')
get()
put('position fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1')
get()我在stdin=subprocess.PIPE后的逗号上收到无效语法错误
任何帮助解决这个问题或尝试其他方法的人都将不胜感激。
发布于 2015-04-16 07:57:27
这条线
os.chdir('C:\Users\Michael\Downloads\stockfish-6-win\stockfish-6-win\Windows'缺少一个右括号。你可能想要
stockfish_cmd = 'C:\\Users\\Michael\\Downloads\\stockfish-6-win\\stockfish-6-win\\Windows\\stockfish'
engine = subprocess.Popen(
stockfish_cmd, universal_newlines=True,
stdin=subprocess.PIPE, stdout=subprocess.PIPE)注意反斜杠的加倍,尽管我相信在这种情况下它只是碰巧是无害的。
发布于 2015-04-16 07:55:42
您的第三行缺少一个):
os.chdir('C:\Users\Michael\Downloads\stockfish-6-win\stockfish-6-win\Windows'
应为os.chdir('C:\Users\Michael\Downloads\stockfish-6-win\stockfish-6-win\Windows')
https://stackoverflow.com/questions/29662984
复制相似问题