Context
I am writing an AI for a deterministic two player game in python. I wish to write a function which takes a timeout value as one of its parameters and returns a move shortly after the timeout. The function searches (negamax or similar) until the timeout is up and then returns the best move it can find.
Specification
- The function should return a valid move no matter how or when it is caused to return.
- The function may return a little after the timeout, so long as this is not noticeable to the user (~100ms).
- The function should return if a custom AI_INTERRUPT event is placed on the pygame event queue. (This is so that impatient users can force the computer to play).
Proposed Implementation
I think I have an idea of how to implement this, but I have found a lot of conflicting advice online (and mostly for problems not quite the same as this one). I am also concerned that I am over-engineering things. I am therefore asking whether or not this implementation suggestion is a sensible one, or whether you recommend something else.
I am considering writing my AI algorithm as a generator that yields successively better moves. Time delays between yields could be long, but the first yield would be almost immediate.
I would then call this generator in a subprocess and have it feed yield values into a pipe.
The main process would then run in a loop of:
- Poll the pipe. If a new value has been yielded it is stored.
- Check the time. If the timeout has been exceeded, return the latest value.
- Check for an AI_INTERRUPT event, returning the latest value if one is found.
- Handle other pygame events as necessary
I am using Python 3 with pygame.