1
1
import math
2
2
3
-
4
- def minimax_with_pruning (
5
- depth , node_index , is_maximizing_player , scores , height , alpha , beta
6
- ):
3
+ def minimax_with_pruning (depth , node_index , is_maximizing_player , scores ,
4
+ height , alpha , beta ):
7
5
"""
8
- Minimax algorithm with alpha-beta pruning to determine the optimal move with improved efficiency.
9
-
6
+ Minimax algorithm with alpha-beta pruning to determine the optimal
7
+ move with improved efficiency.
8
+
10
9
Parameters:
11
10
- depth (int): Current depth in the game tree, used to track recursion level.
12
- - node_index (int): Index of the current node in the scores array, representing leaf nodes.
13
- - is_maximizing_player (bool): True if the current player is the maximizing player, False otherwise.
14
- - scores (list): A list of integers representing the scores at the leaf nodes of the game tree.
15
- - height (int): The maximum depth of the game tree, based on the number of leaf nodes in a binary structure.
16
- - alpha (int): The best value that the maximizer can guarantee at the current level or above.
17
- - beta (int): The best value that the minimizer can guarantee at the current level or above.
11
+ - node_index (int): Index of the current node in the scores array, representing
12
+ leaf nodes.
13
+ - is_maximizing_player (bool): True if the current player is the maximizing player
14
+ , False otherwise.
15
+ - scores (list): A list of integers representing the scores at the
16
+ leaf nodes of the game tree.
17
+ - height (int): The maximum depth of the game tree, based on the number of
18
+ leaf nodes in a binary structure.
19
+ - alpha (int): The best value that the maximizer can guarantee at the
20
+ current level or above.
21
+ - beta (int): The best value that the minimizer can guarantee at the
22
+ current level or above.
18
23
19
24
Returns:
20
25
- int: The best score that the current player can achieve from this node.
@@ -27,9 +32,8 @@ def minimax_with_pruning(
27
32
if is_maximizing_player :
28
33
best_score = - math .inf # Start with the worst possible score for maximizer
29
34
for i in range (2 ): # Two branches at each level in binary tree
30
- val = minimax_with_pruning (
31
- depth + 1 , node_index * 2 + i , False , scores , height , alpha , beta
32
- )
35
+ val = minimax_with_pruning (depth + 1 , node_index * 2 + i ,
36
+ False , scores , height , alpha , beta )
33
37
best_score = max (best_score , val ) # Maximizer selects the maximum value
34
38
alpha = max (alpha , best_score ) # Update alpha (best option for maximizer)
35
39
if beta <= alpha :
@@ -40,9 +44,8 @@ def minimax_with_pruning(
40
44
else :
41
45
best_score = math .inf # Start with the worst possible score for minimizer
42
46
for i in range (2 ):
43
- val = minimax_with_pruning (
44
- depth + 1 , node_index * 2 + i , True , scores , height , alpha , beta
45
- )
47
+ val = minimax_with_pruning (depth + 1 , node_index * 2 + i , True ,
48
+ scores , height , alpha , beta )
46
49
best_score = min (best_score , val ) # Minimizer selects the minimum value
47
50
beta = min (beta , best_score ) # Update beta (best option for minimizer)
48
51
if beta <= alpha :
@@ -57,11 +60,8 @@ def main():
57
60
height = math .ceil (math .log2 (len (scores )))
58
61
59
62
# Print the optimal outcome for the maximizing player with alpha-beta pruning
60
- print (
61
- "Optimal value for the maximizing player with pruning:" ,
62
- minimax_with_pruning (0 , 0 , True , scores , height , - math .inf , math .inf ),
63
- )
64
-
63
+ print ("Optimal value for the maximizing player with pruning:" ,
64
+ minimax_with_pruning (0 , 0 , True , scores , height , - math .inf , math .inf ))
65
65
66
66
if __name__ == "__main__" :
67
67
main ()
0 commit comments