Python中文
首页
教程
问答
标签
搜索
登录
注册
如何与Python中的象棋引擎通信?
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>在win 7上,我可以通过命令行与象棋引擎通信。Win 7上与<a href="http://www.stockfishchess.com/">Stockfish</a>的小示例会话:</p> <pre><code>C:\run\Stockfish>stockfish-x64.exe Stockfish 2.2.2 JA SSE42 by Tord Romstad, Marco Costalba and Joona Kiiski quit C:\run\Stockfish> </code></pre> <p>第一行是引擎输出的,“quit”是我键入的退出引擎的内容(这里有<a href="http://wbec-ridderkerk.nl/html/UCIProtocol.html">other things i can do</a>,但我很清楚)。</p> <p>现在我想和python中的引擎通信:</p> <pre><code>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') </code></pre> <p>我得到了</p> <pre><code>C:\run\Stockfish>communicate.py b'Stockfish 2.2.2 JA SSE42 by Tord Romstad, Marco Costalba and Joona Kiiski' </code></pre> <p>但它没有退出引擎(没有C:\ run\Stockfish>;提示),它一直在等待输入。我得用手关上窗户。似乎没有将我的退出消息(python脚本的最后一行)写入stdin。</p> <p>换言之,我可以从stdout中阅读,但当我给stdin写信时,什么也不会发生。</p> <p>我做错了什么,怎么做才对?</p> <hr/> <p>编辑:好吧,多亏拉尔曼的帮助,我解决了这个问题:</p> <p>示例Python脚本:</p> <pre><code>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') </code></pre> <p>输出:</p> <pre><code>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> </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>您遇到了死锁:子进程正在等待输入,而您的程序正在等待它输出更多的行</p> <pre><code>for line in engine.stdout: print(line.strip()) </code></pre> <p>此循环仅在子进程关闭其<code>stdout</code>时停止。</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何替换子字符串,但前提是它正好出现在两个单词之间
10 回答
如何替换字典中所有出现的指定字符
10 回答
如何替换字典中所有键的第一个字符?
1 回答
如何替换字典所有键中的子字符串
4 回答
如何替换字符串python中的变量值?
9 回答
如何替换字符串Python中的第二次迭代
4 回答
如何替换字符串y Python中不等于字符串x的所有内容?
3 回答
如何替换字符串中出现的第n个单词?
3 回答
如何替换字符串中单词的一部分
1 回答
如何替换字符串中同时出现的2个或更多特殊字符或下划线
4 回答
如何替换字符串中指定位置(索引)的字符?
2 回答
如何替换字符串中某个字符的所有匹配项?
9 回答
如何替换字符串中的
9 回答
如何替换字符串中的一个字符
3 回答
如何替换字符串中的主题(固定位置)
8 回答
如何替换字符串中的分隔逗号?
1 回答
如何替换字符串中的列名(python)?
3 回答
如何替换字符串中的制表符?
7 回答
如何替换字符串中的单个单词而不是用相同的字符替换其他单词
9 回答
如何替换字符串中的单个字符?
8 回答