0

I hope you're having a good day :)

Recently I have been making a chess program.

I'm now at the point of making the AI and I'm using Stockfish for testing purposes.

Since I need the computer to have time to evaluate without pausing the pygame game loop, I'm using the threading library.

I'm also using python-chess as my main library for handling the game state and making moves, as well as accessing Stockfish.

Here is my code for threading:

def engine_play():
    global player_color
    global board
    while board.result() == "*":
        if board.turn is not player_color:
            result = engine.play(board, chess.engine.Limit(time=3.0))
            board.push(result.move)
            print(result.move)
    print(board.result())

engine_thread = threading.Thread(target=engine_play)
engine_thread.setDaemon(True)
engine_thread.start()

For some reason, the while loop in engine_play() stops executing.

It doesn't stop consistently, it just stops at random.

When it prints board.result after the while loop the value = "*".

How is this while loop stopping when the condition (board.result() == "*") is still met?

Is it actually a threading problem?

Also, the pygame game loop just updates the graphics and implements things like drag 'n drop functionality.

There are no errors displayed and I only have this one thread.

KNN
  • 35
  • 5
  • Please define what "stops abruptly" means. Produces an error, if so what error? The program exits? etc. – Kingsley Sep 25 '20 at 01:25
  • Which loop is handling the PyGame event processing? – Kingsley Sep 25 '20 at 01:25
  • The loop showed handles the engine and automatically play the move, by stop abruptly I mean the whole loop showed stops executing which means the engine doesn't play any moves. There is a loop after that which is the game loop. – KNN Sep 25 '20 at 02:10
  • No error is shown – KNN Sep 25 '20 at 02:14
  • Is `board.result()` shared between threads? If so, are you using a kind of lock? I could be a race condition somewhere – geckos Sep 25 '20 at 12:45
  • I only have one thread, the game loop executes after that thread is started. – KNN Sep 25 '20 at 12:52
  • board.result() is also part of the python-chess library – KNN Sep 25 '20 at 12:53

1 Answers1

0

I'm not entirely sure why the loop stops, but I did figure out a way to fix my problem. Instead of:

while board.result() == "*":
    if board.turn is not player_color:
        result = engine.play(board, chess.engine.Limit(time=3.0))
        board.push(result.move)
        print(result.move)
print(board.result())

I put an infinite loop and checked for board.result() every time.

while True:
    if board.result() == "*":
        if board.turn is not player_color:
            result = engine.play(board, chess.engine.Limit(time=3.0))
            board.push(result.move)
            print(result.move)
print(board.result())

It also seems pretty important to set Daemon to True, otherwise the infinite loop will prevent the program from stopping.

KNN
  • 35
  • 5