Skip to content

Commit 4978277

Browse files
committed
Fix linting issues in min_max.py
1 parent 143b434 commit 4978277

File tree

1 file changed

+13
-41
lines changed

1 file changed

+13
-41
lines changed

game_theory/min_max.py

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import math
22

3+
34
class MinMax:
45
"""
56
A class to represent a game using the Minimax algorithm.
@@ -9,26 +10,13 @@ class MinMax:
910
scores : list[int]
1011
List of terminal node scores.
1112
tree_depth : int
12-
Depth of the game tree.
13-
14-
Methods:
15-
-------
16-
minimax(current_depth: int = 0, node_index: int = 0, is_max_turn: bool = True) -> int:
17-
Recursive implementation of the minimax algorithm.
18-
find_optimal_value() -> int:
19-
Find and return the optimal value for the maximizing player.
20-
21-
Examples:
22-
---------
23-
>>> game = MinMax([3, 5, 2, 9, 12, 5, 23, 23])
24-
>>> game.find_optimal_value()
25-
12
13+
Depth of the minimax tree.
2614
"""
2715

28-
def __init__(self, scores: list[int]) -> None:
16+
def __init__(self, scores: list[int]):
2917
"""
3018
Initialize the MinMax game with a list of scores.
31-
19+
3220
Parameters:
3321
----------
3422
scores : list[int]
@@ -37,10 +25,13 @@ def __init__(self, scores: list[int]) -> None:
3725
self.scores = scores
3826
self.tree_depth = int(math.log2(len(scores)))
3927

40-
def minimax(self, current_depth: int = 0, node_index: int = 0, is_max_turn: bool = True) -> int:
28+
def minimax(
29+
self, current_depth: int = 0,
30+
node_index: int = 0, is_max_turn: bool = True
31+
) -> int:
4132
"""
4233
Recursive implementation of the minimax algorithm.
43-
34+
4435
Parameters:
4536
----------
4637
current_depth : int
@@ -54,17 +45,12 @@ def minimax(self, current_depth: int = 0, node_index: int = 0, is_max_turn: bool
5445
-------
5546
int
5647
The optimal value for the current player.
57-
58-
Examples:
59-
---------
60-
>>> game = MinMax([3, 5, 2, 9, 12, 5, 23, 23])
61-
>>> game.minimax(0, 0, True)
62-
12
6348
"""
64-
49+
# Base case: we've reached a terminal node
6550
if current_depth == self.tree_depth:
6651
return self.scores[node_index]
67-
52+
53+
# Recursive case
6854
if is_max_turn:
6955
return max(
7056
self.minimax(current_depth + 1, node_index * 2, False),
@@ -79,25 +65,11 @@ def minimax(self, current_depth: int = 0, node_index: int = 0, is_max_turn: bool
7965
def find_optimal_value(self) -> int:
8066
"""
8167
Find and return the optimal value for the maximizing player.
82-
68+
8369
Returns:
8470
-------
8571
int
8672
The optimal value.
87-
88-
Examples:
89-
---------
90-
>>> game = MinMax([3, 5, 2, 9, 12, 5, 23, 23])
91-
>>> game.find_optimal_value()
92-
12
9373
"""
9474
return self.minimax()
9575

96-
if __name__ == "__main__":
97-
import doctest
98-
doctest.testmod()
99-
100-
scores = [3, 5, 2, 9, 12, 5, 23, 23]
101-
game = MinMax(scores)
102-
optimal_value = game.find_optimal_value()
103-
print(f"The optimal value is: {optimal_value}")

0 commit comments

Comments
 (0)