python - 鳕鱼和 python

标签 python chess

我正在尝试使用 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 之后的逗号上遇到无效的语法错误

如果您能帮助修复该问题或尝试其他方法,我们将不胜感激。

最佳答案

线

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)

还要注意双反斜杠,尽管我相信在这种情况下它恰好是无害的。

关于python - 鳕鱼和 python ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29662984/

相关文章:

android - 不用ndk写一个android gui象棋界面

c++ - 函数不是全局命名空间的成员

python - 具有整数序列的 Keras 示例字级模型给出 `expected ndim=3, found ndim=4`

python - 在python中,有没有办法在满足条件的情况下进入try/except block ,否则就直接执行try block 中的代码?

python - 将生成器表达式传递给 any() 和 all()

haskell - 在 Haskell 中表示棋盘

c - 在现有的 C 国际象棋引擎中实现多线程

python - 如果使用python在postgres中主键或id相同,如何附加值

python - 是否可以使 matplotlib 图形轴等比例缩放?

algorithm - 要求解决八皇后难题的算法