Say that I currently have a board, and then on that board 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))

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?

  • 1
    What you are asking for is known as multiPV. In the documentation for engine.analyse(), read about the multipv optional parameter. I would have posted this as an answer -- to the closers, note that asking an engine for the X best moves is very different from all moves, they are not even accomplished with the same technique, so please don't close so hastily questions that only seem similar. – Remellion Mar 4 '20 at 5:32

In the world of chess engines, what you're looking for is called multi-PV (multiple principal variations): telling the engine to look for and return up to n best moves and the associated principal variations for each. ("Up to" because sometimes there's only 1 legal move in a position.)

In python-chess, this is part of the engine protocols, as one of the optional parameters when calling engine.analyse(). From the documentation:

class chess.engine.EngineProtocol

coroutine analyse( [...] )

  • multipv – Optional. Analyse multiple root moves. Will return a list of at most multipv dictionaries rather than just a single info dictionary.

The .analyse() method tells the engine to analyse the position, returning a dictionary of information, which includes among other things the moves of the principal variation. Specifying for instance .analyse(multipv = 3) will tell the engine to return a list of (up to) 3 such dictionaries -- the first moves in the pv value of each dictionary should be what you are looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.