【发布时间】:2015-04-15 23:53:31
【问题描述】:
我正在尝试使用 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 之后的逗号上出现无效语法错误
感谢任何帮助解决该问题或尝试其他方法。
【问题讨论】:
-
这听起来很奇怪。也许尝试删除空格并重写它:它可以是一个薄的空间,显示为固定宽度字体的空间;例如,如果您不小心输入了 Alt+Space。 (假设在 python 告诉你的地方实际上存在错误;我相信在重新加载答案之前我没有看到的缺失括号应该在
def关键字处开始语法错误)