Skip to content

Minimax Algorithm #1

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

Merged
merged 1 commit into from
Jul 7, 2019
Merged
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
28 changes: 28 additions & 0 deletions backtracking/minimax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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:
return (max(minimax(Depth + 1, nodeIndex * 2, False, scores, height),
minimax(Depth + 1, nodeIndex * 2 + 1, False, scores, height)))
return (min(minimax(Depth + 1, nodeIndex * 2, True, scores, height),
minimax(Depth + 1, nodeIndex * 2 + 1, True, scores, height)))

if __name__ == "__main__":

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))
26 changes: 0 additions & 26 deletions minimax.py

This file was deleted.