-
Notifications
You must be signed in to change notification settings - Fork 0
/
alphabetapruning.py
39 lines (35 loc) · 1.61 KB
/
alphabetapruning.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import math
def minimax(depth, nodeIndex, isMaximizingPlayer, values, maxDepth, alpha, beta):
if depth == maxDepth:
print(f"Leaf node reached at depth {depth}, returning value: {values[nodeIndex]}")
return values[nodeIndex]
if isMaximizingPlayer:
best = -math.inf
print(f"Maximizer at depth {depth}")
for i in range(2):
value = minimax(depth + 1, nodeIndex * 2 + i, False, values, maxDepth, alpha, beta)
print(f"Maximizer at depth {depth}, comparing value: {value} with best: {best}")
best = max(best, value)
alpha = max(alpha, best)
if beta <= alpha:
print(f"Maximizer at depth {depth}, pruning with alpha: {alpha} and beta: {beta}")
break
print(f"Maximizer at depth {depth}, selected best: {best}")
return best
else:
best = math.inf
print(f"Minimizer at depth {depth}")
for i in range(2):
value = minimax(depth + 1, nodeIndex * 2 + i, True, values, maxDepth, alpha, beta)
print(f"Minimizer at depth {depth}, comparing value: {value} with best: {best}")
best = min(best, value)
beta = min(beta, best)
if beta <= alpha:
print(f"Minimizer at depth {depth}, pruning with alpha: {alpha} and beta: {beta}")
break
print(f"Minimizer at depth {depth}, selected best: {best}")
return best
maxDepth = 3
values = [-1, 4, 2, 6, -3, -5, 0, 7]
optimalValue = minimax(0, 0, True, values, maxDepth, -math.inf, math.inf)
print("\nThe optimal value is:", optimalValue)