mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
added type hints and doctests for minimax algorithm (#2478)
* added type hints and doctests for minimax algorithm * Update backtracking/minimax.py Co-authored-by: Christian Clauss <cclauss@me.com> * last fix Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
daa1b4d70f
commit
53c2a24587
@ -1,3 +1,4 @@
|
|||||||
|
from __future__ import annotations
|
||||||
import math
|
import math
|
||||||
|
|
||||||
""" Minimax helps to achieve maximum score in a game by checking all possible moves
|
""" Minimax helps to achieve maximum score in a game by checking all possible moves
|
||||||
@ -9,26 +10,62 @@ import math
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def minimax(Depth, nodeIndex, isMax, scores, height):
|
def minimax(depth: int, node_index: int, is_max: bool,
|
||||||
|
scores: list[int], height: float) -> int:
|
||||||
|
"""
|
||||||
|
>>> import math
|
||||||
|
>>> scores = [90, 23, 6, 33, 21, 65, 123, 34423]
|
||||||
|
>>> height = math.log(len(scores), 2)
|
||||||
|
>>> minimax(0, 0, True, scores, height)
|
||||||
|
65
|
||||||
|
>>> minimax(-1, 0, True, scores, height)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: Depth cannot be less than 0
|
||||||
|
>>> minimax(0, 0, True, [], 2)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
ValueError: Scores cannot be empty
|
||||||
|
>>> scores = [3, 5, 2, 9, 12, 5, 23, 23]
|
||||||
|
>>> height = math.log(len(scores), 2)
|
||||||
|
>>> minimax(0, 0, True, scores, height)
|
||||||
|
12
|
||||||
|
>>> minimax('1', 2, True, [], 2 )
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
TypeError: '<' not supported between instances of 'str' and 'int'
|
||||||
|
"""
|
||||||
|
|
||||||
if Depth == height:
|
if depth < 0:
|
||||||
return scores[nodeIndex]
|
raise ValueError("Depth cannot be less than 0")
|
||||||
|
|
||||||
if isMax:
|
if len(scores) == 0:
|
||||||
|
raise ValueError("Scores cannot be empty")
|
||||||
|
|
||||||
|
if depth == height:
|
||||||
|
return scores[node_index]
|
||||||
|
|
||||||
|
if is_max:
|
||||||
return max(
|
return max(
|
||||||
minimax(Depth + 1, nodeIndex * 2, False, scores, height),
|
minimax(depth + 1, node_index * 2, False, scores, height),
|
||||||
minimax(Depth + 1, nodeIndex * 2 + 1, False, scores, height),
|
minimax(depth + 1, node_index * 2 + 1, False, scores, height),
|
||||||
)
|
)
|
||||||
|
|
||||||
return min(
|
return min(
|
||||||
minimax(Depth + 1, nodeIndex * 2, True, scores, height),
|
minimax(depth + 1, node_index * 2, True, scores, height),
|
||||||
minimax(Depth + 1, nodeIndex * 2 + 1, True, scores, height),
|
minimax(depth + 1, node_index * 2 + 1, True, scores, height),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
def main():
|
||||||
|
|
||||||
scores = [90, 23, 6, 33, 21, 65, 123, 34423]
|
scores = [90, 23, 6, 33, 21, 65, 123, 34423]
|
||||||
height = math.log(len(scores), 2)
|
height = math.log(len(scores), 2)
|
||||||
|
|
||||||
print("Optimal value : ", end="")
|
print("Optimal value : ", end="")
|
||||||
print(minimax(0, 0, True, scores, height))
|
print(minimax(0, 0, True, scores, height))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
||||||
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user