我正在尝试使用python编写一个脚本,将国际象棋位置提供给鳕鱼并进行评估。
我的问题是基于此, 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
之后的逗号上收到无效的语法错误任何帮助修复或尝试其他方法的人都表示赞赏。
答案 0 :(得分:2)
该行
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)
注意反斜杠加倍,虽然我认为在这种情况下它恰好是无害的。
答案 1 :(得分:0)
第三行中缺少)
:
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')
答案 2 :(得分:0)