I am trying to write a chess engine in python, i can find the best move given a position, but i am struggling to collect the principal variation from that position, the following is what i have tried so far:
def alphabeta(board, alpha, beta, depth, pvtable):
if depth == 0:
return evaluate.eval(board)
for move in board.legal_moves:
board.push(move)
score = -alphabeta(board, -beta, -alpha, depth - 1, pvtable)
board.pop()
if score >= beta:
return beta
if score > alpha:
alpha = score
pvtable[depth-1] = str(move)
return alpha
i am using pvtable[depth - 1] = str(move) to append moves but in the end i find that pvtable contains random non consistent moves, things like ['g1h3', 'g8h6', 'h3g5', 'd8g5'] for the starting position.
I know that similar questions about that have been asked but i still didn't figure out how i can solve this problem.