-
-
Notifications
You must be signed in to change notification settings - Fork 47k
minimax #947
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
minimax #947
Changes from 4 commits
5810196
d576a2b
8c53b22
76f20c4
db77368
ff9b85a
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import math | ||
|
||
''' Minimax helps to achieve maximum score in a game by checking all possible moves | ||
depth is current depth in game tree. | ||
nodeIndex is index of current node in scores[]. | ||
if move is of maximizer return true else false | ||
leaves of game tree is stored in scores[] | ||
height is maximum height of Game tree | ||
''' | ||
|
||
def minimax (Depth, nodeIndex, isMax, scores, height): | ||
|
||
if (Depth == height): | ||
return scores[nodeIndex] | ||
|
||
if (isMax): | ||
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 () are not needed and not helpful. |
||
return max(minimax(Depth + 1, nodeIndex * 2, False, scores, height), minimax(Depth + 1, nodeIndex * 2 + 1, False, scores, height)) | ||
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 lines are too long to be read on GitHub without scrolling left and right so put the whole return value in () and then wrap the lines after the comma to improve readability. |
||
return min(minimax(Depth + 1, nodeIndex * 2, True, scores, height), minimax(Depth + 1, nodeIndex * 2 + 1, True, scores, height)) | ||
|
||
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. Insert `if name == 'main': and then indent all other lines below it. This allows us to run tests without running your code. |
||
|
||
scores = [90, 23, 6, 33, 21, 65, 123, 34423] | ||
|
||
height = math.log(len(scores), 2) | ||
|
||
print("Optimal value : ", end = "") | ||
print(minimax(0, 0, True, scores, height)) |
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.
These () are not needed and not helpful.