如何在Python中与Chess引擎通信? [英] How to Communicate with a Chess engine in Python?

查看:87
本文介绍了如何在Python中与Chess引擎通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在胜利7上,我可以通过命令行与国际象棋引擎通信。在Win 7上使用 Stockfish 的小型示例会话:

on win 7 i can communicate with a chess engine via commandline. Small Example Session with Stockfish on Win 7:

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

C:\run\Stockfish>

第一行是引擎输出的, quit是我键入要退出引擎的内容(还有我可以做的其他事情,但这对我来说很清楚。)

The first line was output by the engine and the 'quit' was what i typed to quit the engine (There are other things i can do, but thats clear to me).

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

Now i want to communicate with that engine from 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')

我得到

C:\run\Stockfish>communicate.py
b'Stockfish 2.2.2 JA SSE42 by Tord Romstad, Marco Costalba and Joona Kiiski'

但是它没有退出引擎(没有C:\run\Stockfish>提示),它一直在等待输入。我必须用手关上窗户。

but it doesnt quit the engine (no C:\run\Stockfish> prompt), it keeps waiting for input. I have to close the window by hand. It seems not to get my quit message (last line of the python script) written to stdin.

换句话说,我可以从stdout读取,但是当我写到stdin时什么也没有

In other words i can read from stdout but when i write to stdin nothing happens.

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

What am i doing wrong and how to do it right?

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

ok, thanks to larsmans´ help i solved it:

Python脚本示例:

Example Python script:

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

输出:

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>


推荐答案

您陷入了僵局:子进程正在等待输入,而您的程序正在等待输出更多行

You've got a deadlock: the subprocess is waiting for input, while your program is waiting for it to output more lines in

for line in engine.stdout:
    print(line.strip())

此循环仅在子进程关闭其<$ c $时停止c> stdout 。

This loop only stops when the subprocess closes its stdout.

这篇关于如何在Python中与Chess引擎通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆