Search code examples
pythonpython-3.xchesspython-chess

PythonChess - Get engine to predict a list of the best moves in a position


I currently have a board, then on that I move my e2 pawn to e4. I'm looking for a way to get the engine to predict the X amount of best moves for black.

I know how to predict the best move given a position:

result = engine.play(board, chess.engine.Limit(time=0.1))
board.push(result.move)

But that only predicts the number 1 best move. I want a list containing the second, third ... X best moves.

How would I do that?


Solution

  • You don't give any detail about your search function, but typically it should return a sequence of moves (the principal variation, otherwise known as PV) that your engine considers best and therefore expects to be played.

    You can change your search function to return multiple PVs, basically collecting and reporting more than one sequence of next best moves. But note that this normally requires a less efficient root search where subsequent moves are searched with a wider alpha-beta window.

    Alternatively you can just save each "best move" during the search by appending any move that raises alpha. This won't give you multiple PVs, but will at least give you a raw "best move" list.