minimax

 # 5)MINI MAX Algorithm


import math
def minimax(curDepth, nodeIndex,
            maxTurn, scores,
            targetDepth):
    # base case : targetDepth reached
    if (curDepth == targetDepth):
        return scores[nodeIndex]

    if (maxTurn):
        return max(minimax(curDepth + 1, nodeIndex * 2,
                           False, scores, targetDepth),
                   minimax(curDepth + 1, nodeIndex * 2 + 1,
                           False, scores, targetDepth))

    else:
        return min(minimax(curDepth + 1, nodeIndex * 2,
                           True, scores, targetDepth),
                   minimax(curDepth + 1, nodeIndex * 2 + 1,
                           True, scores, targetDepth))


# Driver code
# scores = [3, 5, 2, 9, 12, 5, 23, 23]
scores = [-1, 4, 2, 6, -3, -5, 0, 7]
treeDepth = math.log(len(scores), 2)

print("The optimal value is : ", end="")
print(minimax(0, 0, True, scores, treeDepth))

Comments

Popular posts from this blog

1.Import and Export(How to read csv file using manualvfunction)

How to became a junior Engineer

Pima Dataset