【问题标题】:Stockfish and Python鳕鱼和蟒蛇
【发布时间】: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 关键字处开始语法错误)

标签: python chess


【解决方案1】:

线

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)

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

【讨论】:

    【解决方案2】:

    您的第三行缺少)

    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')

    【讨论】:

    • 是的,我少了一个括号。
    • 别担心,我们最好的人都会遇到这种情况。
    【解决方案3】:

    我在github 上放置了一个用于返回Stockfish 位置评估的脚本。还有一个 python 包装器here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-30
      • 2010-11-03
      • 2011-06-07
      • 1970-01-01
      • 2010-10-27
      • 1970-01-01
      相关资源
      最近更新 更多