Skip to content

Commit 143b434

Browse files
committed
Add Minimax algorithm implementation in game_theory folder
feat(game_theory): Implement Minimax algorithm in min_max.py - Add min_max.py file with Minimax algorithm implementation - Implement MinimaxGame class with recursive Minimax function - Include driver code for demonstration - Enhance decision-making capabilities for two-player zero-sum games - Added type hints for function parameters and return values. - Integrated doctests to validate functionality.
1 parent e9e7c96 commit 143b434

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

game_theory/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Game Theory Algorithms
2+
3+
This repository contains implementations of various algorithms related to game theory.
4+
5+
## Algorithms
6+
7+
- **Minimax Algorithm**
8+
- [Minimax (Wikipedia)](https://en.wikipedia.org/wiki/Minimax)
9+
10+

game_theory/__init__.py

Whitespace-only changes.

game_theory/min_max.py

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import math
2+
3+
class MinMax:
4+
"""
5+
A class to represent a game using the Minimax algorithm.
6+
7+
Attributes:
8+
----------
9+
scores : list[int]
10+
List of terminal node scores.
11+
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
26+
"""
27+
28+
def __init__(self, scores: list[int]) -> None:
29+
"""
30+
Initialize the MinMax game with a list of scores.
31+
32+
Parameters:
33+
----------
34+
scores : list[int]
35+
List of terminal node scores.
36+
"""
37+
self.scores = scores
38+
self.tree_depth = int(math.log2(len(scores)))
39+
40+
def minimax(self, current_depth: int = 0, node_index: int = 0, is_max_turn: bool = True) -> int:
41+
"""
42+
Recursive implementation of the minimax algorithm.
43+
44+
Parameters:
45+
----------
46+
current_depth : int
47+
Current depth in the game tree.
48+
node_index : int
49+
Index of the current node in the tree.
50+
is_max_turn : bool
51+
Boolean indicating if it's the maximizing player's turn.
52+
53+
Returns:
54+
-------
55+
int
56+
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
63+
"""
64+
65+
if current_depth == self.tree_depth:
66+
return self.scores[node_index]
67+
68+
if is_max_turn:
69+
return max(
70+
self.minimax(current_depth + 1, node_index * 2, False),
71+
self.minimax(current_depth + 1, node_index * 2 + 1, False)
72+
)
73+
else:
74+
return min(
75+
self.minimax(current_depth + 1, node_index * 2, True),
76+
self.minimax(current_depth + 1, node_index * 2 + 1, True)
77+
)
78+
79+
def find_optimal_value(self) -> int:
80+
"""
81+
Find and return the optimal value for the maximizing player.
82+
83+
Returns:
84+
-------
85+
int
86+
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
93+
"""
94+
return self.minimax()
95+
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)