How to print second best move in python-chess, along with first bes move

53 Views Asked by At

I'm currently trying to create a program using Python-Chess that will analyse games for me, and give a list of best, and second best moves. Even though lichess.org and chess.com have good UI for this, I want to mass analyse games and then output the data so I can graph or smth.

Right now, I've been able to open and store lists of games, and analyse the best move, but how do I get it to output the second best move? Should be noted I'm a beginner and am struggling big time understand what all the classes and functions do.

Code:

import chess
import asyncio
import chess.engine
import chess.pgn

engine = chess.engine.SimpleEngine.popen_uci("filelocation")
board = chess.Board()
pgnBase = open("filelocation")
gameList = []

#Accesses the files and stores each game in a list of objects
temp = chess.pgn.read_game(pgnBase)
while str(type(temp)) != "<class 'NoneType'>":
    gameList.append(temp.mainline_moves())
    temp = chess.pgn.read_game(pgnBase)

temp = gameList[0]
analysis = []
for moves in temp:
    result = engine.play(board, chess.engine.Limit(time=0.3))
    board.push(moves)
    analysis.append(str(result.move))
print(analysis)
engine.quit()
pgnBase.close()

Basically, I want it to output the second best move, along with the first.

0

There are 0 best solutions below