This is my search function for python chess engine:
def search(pos: position.Position, depth: int, alpha: int, beta: int, side_to_move: chess.Color, root: bool = False):
global nodes, best_score, best_move, max_score, min_score, killers
bestMove = None
"""Search the position to a given depth."""
if depth == 0 or pos.board.chess_board().is_game_over():
nodes += 1
return evaluate.evaluate(pos, side_to_move), best_move
for move in pos.board.chess_board().legal_moves:
if stop_search.search_has_stopped():
return best_score, best_move
if prune(pos, move, alpha, beta, side_to_move, depth):
continue
pos.board.chess_board().push(move)
score, _ = search(pos, depth - 1, -beta, -alpha, not side_to_move, root=False)
score = -score
pos.board.chess_board().pop()
if score > best_score:
best_score = score
best_move = move
if score > max_score:
max_score = score
if score < min_score:
min_score = score
if score > alpha:
alpha = score
bestMove = move
if score >= beta:
best_move = bestMove
return beta, bestMove
else:
killers.append(move)
return alpha, bestMove
When I run it, the results are fine until depth 4, where I get this strange error:
File "path/to/search.py", line 66, in search
pos.board.chess_board().pop()
File "path/to/chess/__init__.py", line 2267, in pop
move = self.move_stack.pop()
^^^^^^^^^^^^^^^^^^^^^
IndexError: pop from empty list
I assume this means that pushing the move to the position was not successful.
I do not know why this occurs, because I am certain that the generated move
is legal (it uses the default legal move generator).
Any help on this would be much appreciated.