Stockfish and Python

这一生的挚爱 提交于 2021-02-07 20:24:31

问题


I'm trying to write a script using python to feed chess positions into stockfish and get evaluations.

My question is based on this, How to Communicate with a Chess engine in Python?

The issue is with subprocess.pipe.

import subprocess, time
import os

os.chdir('C:\Users\Michael\Downloads\stockfish-6-win\stockfish-6-win\Windows'


engine = subprocess.Popen('stockfish', 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 (eng 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)

put('go depth 15') 
get()
put('eval')
get() 
put('position fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1') 
get()

I'm getting an invalid syntax error on the comma after stdin=subprocess.PIPE

Any help fixing that or trying another method is appreciated.


回答1:


The line

os.chdir('C:\Users\Michael\Downloads\stockfish-6-win\stockfish-6-win\Windows'

is missing a closing parenthesis. You probably wanted

stockfish_cmd = 'C:\\Users\\Michael\\Downloads\\stockfish-6-win\\stockfish-6-win\\Windows\\stockfish'
engine = subprocess.Popen(
    stockfish_cmd, universal_newlines=True,
    stdin=subprocess.PIPE, stdout=subprocess.PIPE)

Note the doubling of backslashes as well, although I believe it just happens to be harmless in this case.




回答2:


You have a missing ) in your third line:

os.chdir('C:\Users\Michael\Downloads\stockfish-6-win\stockfish-6-win\Windows'

should be os.chdir('C:\Users\Michael\Downloads\stockfish-6-win\stockfish-6-win\Windows')




回答3:


I put a script for returning the Stockfish position evaluation on github. Also a python wrapper here.



来源:https://stackoverflow.com/questions/29662984/stockfish-and-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!