Skip to main content
 首页 » 编程设计

python之鳕鱼和 python

2024年10月25日11myhome

我正在尝试使用 python 编写一个脚本,将国际象棋位置输入鳕鱼并进行评估。

我的问题是基于此, How to Communicate with a Chess engine in Python?

问题出在 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() 

我在 stdin=subprocess.PIPE 之后的逗号上遇到无效的语法错误

如果您能帮助修复该问题或尝试其他方法,我们将不胜感激。

请您参考如下方法:

线

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

缺少右括号。你可能想要

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) 

还要注意双反斜杠,尽管我相信在这种情况下它恰好是无害的。