minimax helps to achieve maximum score in a game by checking all possible moves depth is current depth in game tree nodeindex is index of current node in scores if move is of maximizer return true else false leaves of game tree is stored in scores height is maximum height of game tree this function implements the minimax algorithm which helps achieve the optimal score for a player in a twoplayer game by checking all possible moves if the player is the maximizer then the score is maximized if the player is the minimizer then the score is minimized parameters depth current depth in the game tree nodeindex index of the current node in the scores list ismax a boolean indicating whether the current move is for the maximizer true or minimizer false scores a list containing the scores of the leaves of the game tree height the maximum height of the game tree returns an integer representing the optimal score for the current player import math scores 90 23 6 33 21 65 123 34423 height math loglenscores 2 minimax0 0 true scores height 65 minimax1 0 true scores height traceback most recent call last valueerror depth cannot be less than 0 minimax0 0 true 2 traceback most recent call last valueerror scores cannot be empty scores 3 5 2 9 12 5 23 23 height math loglenscores 2 minimax0 0 true scores height 12 base case if the current depth equals the height of the tree return the score of the current node if it s the maximizer s turn choose the maximum score between the two possible moves if it s the minimizer s turn choose the minimum score between the two possible moves sample scores and height calculation calculate and print the optimal value using the minimax algorithm this function implements the minimax algorithm which helps achieve the optimal score for a player in a two player game by checking all possible moves if the player is the maximizer then the score is maximized if the player is the minimizer then the score is minimized parameters depth current depth in the game tree node_index index of the current node in the scores list is_max a boolean indicating whether the current move is for the maximizer true or minimizer false scores a list containing the scores of the leaves of the game tree height the maximum height of the game tree returns an integer representing the optimal score for the current player import math scores 90 23 6 33 21 65 123 34423 height math log len scores 2 minimax 0 0 true scores height 65 minimax 1 0 true scores height traceback most recent call last valueerror depth cannot be less than 0 minimax 0 0 true 2 traceback most recent call last valueerror scores cannot be empty scores 3 5 2 9 12 5 23 23 height math log len scores 2 minimax 0 0 true scores height 12 base case if the current depth equals the height of the tree return the score of the current node if it s the maximizer s turn choose the maximum score between the two possible moves if it s the minimizer s turn choose the minimum score between the two possible moves sample scores and height calculation calculate and print the optimal value using the minimax algorithm
from __future__ import annotations

import math


def minimax(
    depth: int, node_index: int, is_max: bool, scores: list[int], height: float
) -> int:
    

    if depth < 0:
        raise ValueError("Depth cannot be less than 0")
    if len(scores) == 0:
        raise ValueError("Scores cannot be empty")

    
    
    if depth == height:
        return scores[node_index]

    
    
    if is_max:
        return max(
            minimax(depth + 1, node_index * 2, False, scores, height),
            minimax(depth + 1, node_index * 2 + 1, False, scores, height),
        )

    
    
    return min(
        minimax(depth + 1, node_index * 2, True, scores, height),
        minimax(depth + 1, node_index * 2 + 1, True, scores, height),
    )


def main() -> None:
    
    scores = [90, 23, 6, 33, 21, 65, 123, 34423]
    height = math.log(len(scores), 2)

    
    print("Optimal value : ", end="")
    print(minimax(0, 0, True, scores, height))


if __name__ == "__main__":
    import doctest

    doctest.testmod()
    main()