2

I try to access the Stockfish engine via Python, but only get bestmove = a2a3. If I enter the same commands into the MAC-terminal, the engine calculates the correct move. Can anyone help me, please ?

My Python code is:

p = subprocess.Popen('/Applications/TigerJython/LuChess/Stockfish/stockfish-11-mac/Mac/stockfish-11-64', stdin=subprocess.PIPE,
                     stdout=subprocess.PIPE,
                     stderr=subprocess.STDOUT)

p.stdin.write("uci\n")
p.stdin.write("ucinewgame\n")
p.stdin.write("position startpos moves e2e4 e7e5\n")
p.stdin.write("go infinite\n")
p.stdin.flush()
print(p.communicate())

The result is shown below:

('Stockfish 11 64 by T. Romstad, M. Costalba, J. Kiiski, G. Linscott
id name Stockfish 11 64\nid author T. Romstad, M. Costalba, J. Kiiski, G. Linscott

option name Debug Log File type string default
option name Contempt type spin default 24 min -100 max 100
...
uciok
bestmove a2a3
None
4
  • 1
    The best and simplest option would be to use the python-chess library. For engine communication see: python-chess.readthedocs.io/en/latest/engine.html
    – Ellie
    Mar 4, 2020 at 21:32
  • Thanks, Phonon, for your hint. python-chess unfortunately only supports version 3.6+ and my application relies on Tiger-Jython, which runs under version 2.7. I could solve the problem in the meantime and have posted the code accordingly above.
    – Michael
    Mar 6, 2020 at 7:59
  • You're welcome and great that you solved it on your own! But generally, it might be a good idea to already consider transitioning to Python 3 anyhow as Python 2 has become deprecated. Please feel free to post your solution as an answer (instead of an edit to the question) as it will be useful for future readers and gives the post closure. SE even encourages answering own questions.
    – Ellie
    Mar 6, 2020 at 11:30
  • 1
    Thanks again, Phonon, for your comments. I am so glad to have found this forum, where you can find an incredible amount of information.
    – Michael
    Mar 8, 2020 at 13:30

1 Answer 1

2

The Problem was p.communicate(), which kills the subprocess. The interaction with the engine works well with the following code:

def put(command, inf_list, tmp_time):

    engine.stdin.write(command+'\n')
    engine.stdin.flush()
    time.sleep(tmp_time)

    if command <> "quit":

        engine.stdin.write('isready\n')     
        engine.stdin.flush()

        while True:
            text = engine.stdout.readline().strip()

            if text == 'readyok':
                break

            elif text !='':
                inf_list.append(text)


inf_list = []
put('uci', inf_list, 0)
put('setoption name Hash value 8192', inf_list, 0)
put('ucinewgame', inf_list, 0)
put('position fen rq3r2/2pbppk1/3p1np1/1N5p/1n1PP2P/2N2P2/PP1Q2P1/R3KB1R w KQ - 3 15', inf_list, 0)
put('go movetime=3000', inf_list, 4)
put('stop', inf_list, 0)
put('quit', inf_list, 0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.