0% found this document useful (0 votes)
7 views1 page

# Program To Implement The Minimax Algorithm

The document contains a Python implementation of the Minimax algorithm with Alpha-Beta pruning. It defines a recursive function that evaluates game tree nodes to determine the optimal move for a maximizing player. The driver code demonstrates the algorithm using a predefined set of leaf node values.

Uploaded by

sainathfugare
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views1 page

# Program To Implement The Minimax Algorithm

The document contains a Python implementation of the Minimax algorithm with Alpha-Beta pruning. It defines a recursive function that evaluates game tree nodes to determine the optimal move for a maximizing player. The driver code demonstrates the algorithm using a predefined set of leaf node values.

Uploaded by

sainathfugare
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

# Program to implement the Minimax algorithm

def minimax(depth, node_index, is_maximizing_player, values, alpha, beta):


# Base case: leaf node is reached
if depth == 3:
return values[node_index]

if is_maximizing_player:
best = float('-inf')

# Recur for left and right children


for i in range(2):
val = minimax(depth + 1, node_index * 2 + i, False, values, alpha, beta)
best = max(best, val)
alpha = max(alpha, best)

# Alpha Beta Pruning


if beta <= alpha:
break
return best

else: # Minimizing player


best = float('inf')

for i in range(2):
val = minimax(depth + 1, node_index * 2 + i, True, values, alpha, beta)
best = min(best, val)
beta = min(beta, best)

# Alpha Beta Pruning


if beta <= alpha:
break
return best

# Driver code
if __name__ == "__main__":
# Example game tree (leaf node values)
values = [3, 5, 6, 9, 1, 2, 0, -1]

print("The optimal value is:", minimax(0, 0, True, values, float('-inf'), float('inf')))

You might also like