How to Communicate with a Chess engine in Python?

后端 未结 2 1898
清酒与你
清酒与你 2020-12-14 04:36

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
Stockfi         


        
相关标签:
2条回答
  • 2020-12-14 04:50

    You might want to use asyncio like python-chess does. See

    engine.py

    and the example from the documentation

    import asyncio
    import chess
    import chess.engine
    
        async def main():
            transport, engine = await chess.engine.popen_uci("/usr/bin/stockfish")
    
            board = chess.Board()
            while not board.is_game_over():
                result = await engine.play(board, chess.engine.Limit(time=0.1))
                board.push(result.move)
    
            await engine.quit()
    
        asyncio.set_event_loop_policy(chess.engine.EventLoopPolicy())
        asyncio.run(main())
    
    0 讨论(0)
  • 2020-12-14 05:11

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

    This loop only stops when the subprocess closes its stdout.

    0 讨论(0)
提交回复
热议问题