Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Communicate with a Chess engine in Python?
    text
    copied!<p>on win 7 i can communicate with a chess engine via commandline. Small Example Session with <a href="http://www.stockfishchess.com/">Stockfish</a> on Win 7:</p> <pre><code>C:\run\Stockfish&gt;stockfish-x64.exe Stockfish 2.2.2 JA SSE42 by Tord Romstad, Marco Costalba and Joona Kiiski quit C:\run\Stockfish&gt; </code></pre> <p>The first line was output by the engine and the 'quit' was what i typed to quit the engine (There are <a href="http://wbec-ridderkerk.nl/html/UCIProtocol.html">other things i can do</a>, but thats clear to me).</p> <p>Now i want to communicate with that engine from 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>and i get</p> <pre><code>C:\run\Stockfish&gt;communicate.py b'Stockfish 2.2.2 JA SSE42 by Tord Romstad, Marco Costalba and Joona Kiiski' </code></pre> <p>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.</p> <p>In other words i can read from stdout but when i write to stdin nothing happens.</p> <p>What am i doing wrong and how to do it right?</p> <hr> <p>Edit: ok, thanks to larsmans´ help i solved it:</p> <p>Example Python script:</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>Output:</p> <pre><code>C:\run\Stockfish&gt;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&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload