forked from Purvajakale/AI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminmax
27 lines (20 loc) · 803 Bytes
/
minmax
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
import math
def minimax (curDepth, nodeIndex, maxTurn, scores,targetDepth):
if (curDepth == targetDepth):
return scores[nodeIndex]
if (maxTurn):
t1=minimax(curDepth + 1, nodeIndex * 2,
False, scores, targetDepth)
t2=minimax(curDepth + 1, nodeIndex * 2 + 1,
False, scores, targetDepth)
return max(t1,t2)
else:
t1=minimax(curDepth + 1, nodeIndex * 2,
True, scores, targetDepth)
t2=minimax(curDepth + 1, nodeIndex * 2 + 1,
True, scores, targetDepth)
return min(t1,t2)
scores = [3, 5, 2, 9, 12, 5, 15, 12]
treeDepth = math.log(len(scores), 2)
print("The optimal value is : ", end = "")
print(minimax(0, 0, True, scores, treeDepth))