5

I made a chess game GUI with pygame and now I want to add a computer player that will be stockfish in this case. github repo: https://github.com/Dark-Leader/Chess

sorry I can't show the code here.. it's multiple files. at the moment the game works perfectly for player vs player. have a look at main.py file:

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

and this is 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())

have a look at the commented lines. I tried to create the engine instance and communicate with it. but the engine just freezes the program and thus the GUI is frozen. I have looked at these posts too and couldn't solve it: Stockfish and Python

How to Communicate with a Chess engine in Python?

Basically what I want to achieve is, when I give a command to the engine I want to get the response and close it immidiately so the GUI can continue (or if there's a way to run both at the same time with threading / multiprocessing) thanks

DarkLeader
  • 589
  • 7
  • 17
  • If you already have the stockfish executable available locally, the python [stockfish module](https://pypi.org/project/stockfish/) lets you interact with the engine -- no need to write your own wrapper. – Pranav Hosangadi Oct 07 '20 at 21:30
  • @PranavHosangadi well there's already a python-chess module as well.. so why even create my chess game? It's a personal project for me to improve, I want to make everything on my own but thank you for the link – DarkLeader Oct 07 '20 at 21:35
  • @PranavHosangadi well I just finished it, everything works but I ended up using the module you posted, any idea how to start a custom wrapper? – DarkLeader Oct 08 '20 at 02:38
  • 1
    Take a look at the module's source code from its github page if you want pointers on how to communicate with the stockfish executable – Pranav Hosangadi Oct 08 '20 at 13:15
  • Does [this](https://stackoverflow.com/questions/375427/a-non-blocking-read-on-a-subprocess-pipe-in-python) helps? – Mathieu Pagé Feb 22 '21 at 16:09
  • For me your code works perfectly. On what computer do you work ? – LittleCoder Aug 08 '21 at 11:17

0 Answers0