Wa0000
Wa0000
Experiment No. 6
THEORY:
● Min-Max algorithm:
The Min Max algorithm is a decision-making algorithm used in the field of game theory
and artificial intelligence. It is used to determine the optimal move for a player in a two-
player game by considering all possible outcomes of the game. The algorithm helps in
selecting the move that minimizes the maximum possible loss. The Min Max algorithm has
many applications in game AI, decision-making, and optimization.
● Algorithm:
Step 1: Generate the Game Tree
Objective: Create a tree structure representing all possible moves from the current
game state.
Details: Each node represents a game state, and each edge represents a possible move.
Step 2: Evaluate Terminal States
Objective: Assign utility values to the terminal nodes of the game tree.
Details: These values represent the outcome of the game (win, lose, or draw).
Step 3: Propagate Utility Values Upwards
Objective: Starting from the terminal nodes, propagate the utility values upwards
through the tree.
Details: For each non-terminal node:
If it's the maximizing player's turn, select the maximum value from the child nodes.
If it's the minimizing player's turn, select the minimum value from the child nodes.
Step 4: Select Optimal Move
Objective: At the root of the game tree, the maximizing player selects the move
that leads to the highest utility value.
● Code:
import math def minimax(node,
is_maximizing, tree):
if is_maximizing:
best_value = -math.inf
for child in tree[node]:
best_value = max(best_value, minimax(child, False, tree))
return best_value
else:
best_value = math.inf
for child in tree[node]:
best_value = min(best_value, minimax(child, True, tree))
return best_value
tree = get_tree()
root =
input("Enter root
node: ")
● Output: