Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions game_theory/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Game Theory Algorithms

This repository contains implementations of various algorithms related to game theory.

## Algorithms

- **Minimax Algorithm**
- [Minimax (Wikipedia)](https://en.wikipedia.org/wiki/Minimax)


Empty file added game_theory/__init__.py
Empty file.
95 changes: 95 additions & 0 deletions game_theory/min_max.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import math

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An error occurred while parsing the file: game_theory/min_max.py

Traceback (most recent call last):
  File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 146, in parse
    reports = lint_file(
              ^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 1:1.
tokenizer error: no matching outer block for dedent

import math
^



class MinMax:
"""
A class to represent a game using the Minimax algorithm.

Attributes:
----------
scores : list[int]
List of terminal node scores.
tree_depth : int
Depth of the minimax tree.
"""

def __init__(self, scores: list[int]):
"""
Initialize the MinMax game with a list of scores.

Parameters:
----------
scores : list[int]
List of terminal node scores.
"""
self.scores = scores
self.tree_depth = int(math.log2(len(scores)))

def minimax(
<<<<<<< HEAD

Check failure on line 29 in game_theory/min_max.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

game_theory/min_max.py:29:1: SyntaxError: Expected a parameter or the end of the parameter list

Check failure on line 29 in game_theory/min_max.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

game_theory/min_max.py:29:3: SyntaxError: Expected a parameter or the end of the parameter list

Check failure on line 29 in game_theory/min_max.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

game_theory/min_max.py:29:5: SyntaxError: Expected a parameter or the end of the parameter list

Check failure on line 29 in game_theory/min_max.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

game_theory/min_max.py:29:7: SyntaxError: Expected a parameter or the end of the parameter list
self, current_depth: int = 0,

Check failure on line 30 in game_theory/min_max.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

game_theory/min_max.py:30:9: SyntaxError: Expected ',', found name
node_index: int = 0, is_max_turn: bool = True
=======

Check failure on line 32 in game_theory/min_max.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

game_theory/min_max.py:32:3: SyntaxError: Expected an expression

Check failure on line 32 in game_theory/min_max.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

game_theory/min_max.py:32:5: SyntaxError: Expected an expression

Check failure on line 32 in game_theory/min_max.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

game_theory/min_max.py:32:7: SyntaxError: Expected an expression
self, current_depth: int = 0, node_index: int = 0, is_max_turn: bool = True

Check failure on line 33 in game_theory/min_max.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

game_theory/min_max.py:33:9: SyntaxError: Parameter without a default cannot follow a parameter with a default

Check failure on line 33 in game_theory/min_max.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

game_theory/min_max.py:33:9: SyntaxError: Duplicate parameter "self"
>>>>>>> 8497aa531c8636d5735e3df3d7583d9c05a323b5
) -> int:
"""
Recursive implementation of the minimax algorithm.

Parameters:
----------
current_depth : int
Current depth in the game tree.
node_index : int
Index of the current node in the tree.
is_max_turn : bool
Boolean indicating if it's the maximizing player's turn.

Returns:
-------
int
The optimal value for the current player.
"""
# Base case: we've reached a terminal node
if current_depth == self.tree_depth:
return self.scores[node_index]

<<<<<<< HEAD
# Recursive case
=======
>>>>>>> 8497aa531c8636d5735e3df3d7583d9c05a323b5
if is_max_turn:
return max(
self.minimax(current_depth + 1, node_index * 2, False),
self.minimax(current_depth + 1, node_index * 2 + 1, False),
)
else:
return min(
self.minimax(current_depth + 1, node_index * 2, True),
self.minimax(current_depth + 1, node_index * 2 + 1, True),
)

def find_optimal_value(self) -> int:
"""
Find and return the optimal value for the maximizing player.

Returns:
-------
int
The optimal value.
"""
return self.minimax()

<<<<<<< HEAD
=======

if __name__ == "__main__":
import doctest

doctest.testmod()

scores = [3, 5, 2, 9, 12, 5, 23, 23]
game = MinMax(scores)
optimal_value = game.find_optimal_value()
print(f"The optimal value is: {optimal_value}")
>>>>>>> 8497aa531c8636d5735e3df3d7583d9c05a323b5
Loading