用python与Stockfish交流

问题描述

我用pygame制作了一个国际象棋游戏GUI,现在我想添加一个在这种情况下将是鳕鱼的计算机播放器。 github仓库:https://github.com/Dark-Leader/Chess

抱歉,我无法在此处显示代码。它是多个文件。 目前,该游戏非常适合玩家对玩家。 看看main.py文件

import pygame
from chess.constants import WIDTH,HEIGHT,FPS,BOARD_EDGE
from chess.game import Game
from chess.engine import Engine
pygame.init()
window = pygame.display.set_mode((WIDTH + BOARD_EDGE * 2,HEIGHT + BOARD_EDGE * 2))
pygame.display.set_caption("Chess")
clock = pygame.time.Clock()
game = Game(window)
# engine = Engine()
# engine.get_response()
# engine.put_command("uci")
# engine.get_response()
# engine.put_command('quit')

running = True
while running:
    clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.MOUSEBUTTONDOWN:
            pos = pygame.mouse.get_pos()
            game.select(pos)

    game.update()
    result = game.get_winner()
    if result:
        print(result)
        running = False
    pygame.display.flip()

pygame.quit()

这是engine.py:

import subprocess


class Engine:

    def __init__(self):
        self.engine = subprocess.Popen("chess/stockfish.exe",stdin=subprocess.PIPE,stdout=subprocess.PIPE,universal_newlines=True)

    def put_command(self,command):
        self.engine.stdin.write(command + "\n")

    def get_response(self):
        for line in self.engine.stdout:
            print(line.strip())

看看注释行。 我试图创建引擎实例并与其进行通信。 但是引擎只是冻结程序,因此GUI被冻结。 我也看过这些帖子,无法解决Stockfish and Python

How to Communicate with a Chess engine in Python?

基本上我想要实现的是,当我向引擎发出命令时,我想立即获得响应并立即将其关闭,以使GUI可以继续(或者是否可以通过线程/来同时运行两者)多处理) 谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)