@@ -26,8 +26,12 @@ def __init__(self, scores: list[int]):
26
26
self .tree_depth = int (math .log2 (len (scores )))
27
27
28
28
def minimax (
29
+ << << << < HEAD
29
30
self , current_depth : int = 0 ,
30
31
node_index : int = 0 , is_max_turn : bool = True
32
+ == == == =
33
+ self , current_depth : int = 0 , node_index : int = 0 , is_max_turn : bool = True
34
+ >> >> >> > 8497 aa531c8636d5735e3df3d7583d9c05a323b5
31
35
) -> int :
32
36
"""
33
37
Recursive implementation of the minimax algorithm.
@@ -50,16 +54,19 @@ def minimax(
50
54
if current_depth == self .tree_depth :
51
55
return self .scores [node_index ]
52
56
57
+ < << << << HEAD
53
58
# Recursive case
59
+ == == == =
60
+ >> >> >> > 8497 aa531c8636d5735e3df3d7583d9c05a323b5
54
61
if is_max_turn :
55
62
return max (
56
63
self .minimax (current_depth + 1 , node_index * 2 , False ),
57
- self .minimax (current_depth + 1 , node_index * 2 + 1 , False )
64
+ self .minimax (current_depth + 1 , node_index * 2 + 1 , False ),
58
65
)
59
66
else :
60
67
return min (
61
68
self .minimax (current_depth + 1 , node_index * 2 , True ),
62
- self .minimax (current_depth + 1 , node_index * 2 + 1 , True )
69
+ self .minimax (current_depth + 1 , node_index * 2 + 1 , True ),
63
70
)
64
71
65
72
def find_optimal_value (self ) -> int :
@@ -73,3 +80,16 @@ def find_optimal_value(self) -> int:
73
80
"""
74
81
return self .minimax ()
75
82
83
+ < << << << HEAD
84
+ == == == =
85
+
86
+ if __name__ == "__main__" :
87
+ import doctest
88
+
89
+ doctest .testmod ()
90
+
91
+ scores = [3 , 5 , 2 , 9 , 12 , 5 , 23 , 23 ]
92
+ game = MinMax (scores )
93
+ optimal_value = game .find_optimal_value ()
94
+ print (f"The optimal value is: { optimal_value } " )
95
+ > >> >> >> 8497 aa531c8636d5735e3df3d7583d9c05a323b5
0 commit comments