why does this minimax algorithm output the same moves every time no matter what?

83 views Asked by At

I'm trying to make a simple chess AI in python but when I try to play a game using this algorithm to find the best move it outputs the same thing every time (Nh6, Rg8, Rh8, Rg8...).

# Minimax algorithm with alpha-beta pruning
def minimax(board, depth, alpha, beta, maximizing_player):
    if depth == 0 or board.is_game_over():
        return evaluate_board(board)

    legal_moves = list(board.legal_moves)
    if maximizing_player:
        max_eval = float("-inf")
        for move in legal_moves:
            board.push(move)
            eval = minimax(board, depth - 1, alpha, beta, False)
            board.pop()
            max_eval = max(max_eval, eval)
            alpha = max(alpha, eval)  # Update alpha here
            if beta <= alpha:
                break
        return max_eval
    else:
        min_eval = float("inf")
        for move in legal_moves:
            board.push(move)
            eval = minimax(board, depth - 1, alpha, beta, True)
            board.pop()
            min_eval = min(min_eval, eval)
            beta = min(beta, eval)  # Update beta here
            if beta <= alpha:
                break
        return min_eval
# Get the best move using minimax algorithm
def get_best_move(board, depth):
    best_move = None
    best_score = float("-inf")
    for move in board.legal_moves:
        board.push(move)
        eval = minimax(board, depth - 1, float("-inf"), float("inf"), False)
        board.pop()
        if eval > best_score:
            best_score = eval
            best_move = move
    return best_move

I've looked over it a bajillion times and I don't know what's wrong. I want it to actually think and make logical moves but it returns the same thing every freaking time

0

There are 0 answers