-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
added type hints for backtracking/minimax.py #2313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
3b12438
9282106
3616821
862545f
332c519
80daa57
e92e433
a1d1a44
c38dec0
1385e47
3b1c4f7
c0dcc55
25946e4
4d0a8f2
696cd47
1b3fec3
a191f89
c676956
2e790ce
f754c0d
20e98fc
d6bff5c
44b8cb0
4e5b730
799fde4
10aa214
cbbc43b
1ac75f4
86fb299
2de2267
0dea049
dc415ec
ecac7b0
363858e
697495b
b22596c
b05081a
9b73884
ea0759d
718be54
a1ea76b
6e6a49d
9200a2e
de3b68d
5f9be0a
4a3b8d6
902fe1c
3a275ca
08eb1ef
d3c3f90
584d1da
8e3c12f
bb84f97
549c798
7d1724a
b006adf
1338305
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from typing import List | ||
import math | ||
|
||
""" Minimax helps to achieve maximum score in a game by checking all possible moves | ||
|
@@ -9,26 +10,62 @@ | |
""" | ||
|
||
|
||
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: | ||
return scores[nodeIndex] | ||
if depth < 0: | ||
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( | ||
minimax(Depth + 1, nodeIndex * 2, False, scores, height), | ||
minimax(Depth + 1, nodeIndex * 2 + 1, False, scores, height), | ||
minimax(depth + 1, node_index * 2, False, scores, height), | ||
minimax(depth + 1, node_index * 2 + 1, False, scores, height), | ||
) | ||
|
||
return min( | ||
minimax(Depth + 1, nodeIndex * 2, True, scores, height), | ||
minimax(Depth + 1, nodeIndex * 2 + 1, True, scores, height), | ||
minimax(depth + 1, node_index * 2, 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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These five lines show the user something when they run the program. Removing them means that the program shows the user nothing when run. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cclauss thank you for your feedback, I have added these lines as method main, look at it if is it right way or not? |
||
height = math.log(len(scores), 2) | ||
|
||
print("Optimal value : ", end="") | ||
print(minimax(0, 0, True, scores, height)) | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rebase with master to pick up the #2464 changes.