如何在Python中与Chess引擎通信?

Nil*_*ils 8 python chess process python-3.x

在win 7上,我可以通过命令行与国际象棋引擎进行通信.Win 7 上与Stockfish的小例子会话:

C:\run\Stockfish>stockfish-x64.exe
Stockfish 2.2.2 JA SSE42 by Tord Romstad, Marco Costalba and Joona Kiiski
quit

C:\run\Stockfish>
Run Code Online (Sandbox Code Playgroud)

第一行由引擎输出,'quit'是我输入的退出引擎(我还可以做其他事情,但这对我来说很清楚).

现在我想从python与该引擎通信:

import subprocess
engine = subprocess.Popen(
    'stockfish-x64.exe',
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,
)
for line in engine.stdout:
    print(line.strip())
engine.stdin.write('quit\n')
Run Code Online (Sandbox Code Playgroud)

我明白了

C:\run\Stockfish>communicate.py
b'Stockfish 2.2.2 JA SSE42 by Tord Romstad, Marco Costalba and Joona Kiiski'
Run Code Online (Sandbox Code Playgroud)

但它没有退出引擎(没有C:\ run\Stockfish>提示),它一直在等待输入.我必须手动关上窗户.似乎没有把我的退出消息(python脚本的最后一行)写入stdin.

换句话说,我可以从stdout读取,但是当我写入stdin时,没有任何事情发生.

我做错了什么,怎么做对了?


编辑:好的,感谢larsmans的帮助我解决了它:

示例Python脚本:

import subprocess, time

engine = subprocess.Popen(
    'stockfish-x64.exe',
    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 (engine 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)

get()
put('uci')
get()
put('setoption name Hash value 128')
get()
put('ucinewgame')
get()
put('position startpos moves e2e4 e7e5 f2f4')
get()
put('go infinite')
time.sleep(3)
get()
put('stop')
get()
put('quit')
Run Code Online (Sandbox Code Playgroud)

输出:

C:\run\Stockfish>1-communicate.py

engine:
        Stockfish 2.2.2 JA SSE42 by Tord Romstad, Marco Costalba and Joona Kiiski

you:
        uci

engine:
        id name Stockfish 2.2.2 JA SSE42
        id author Tord Romstad, Marco Costalba and Joona Kiiski
        option name Use Search Log type check default false
        option name Search Log Filename type string default SearchLog.txt
        ... etc ...
        uciok

you:
        setoption name Hash value 128

engine:

you:
        ucinewgame

engine:

you:
        position startpos moves e2e4 e7e5 f2f4

engine:

you:
        go infinite

engine:
        info depth 1 seldepth 1 score cp 56 nodes 62 nps 1675 time 37 multipv 1 pv e5f4
        info depth 2 seldepth 2 score cp 48 nodes 804 nps 21157 time 38 multipv 1 pv b8c6 g1h3
        info depth 3 seldepth 3 score cp 64 nodes 1409 nps 37078 time 38 multipv 1 pv b8c6 b1c3 e5f4
        ... etc ...

you:
        stop

engine:
        bestmove e5f4 ponder g1f3

you:
        quit

C:\run\Stockfish>
Run Code Online (Sandbox Code Playgroud)

Fre*_*Foo 3

您遇到了死锁:子进程正在等待输入,而您的程序正在等待它输出更多行

for line in engine.stdout:
    print(line.strip())
Run Code Online (Sandbox Code Playgroud)

该循环仅在子进程关闭其stdout.