0% found this document useful (0 votes)
26 views5 pages

T21-86 AI Exp6

The document describes an AI lab experiment to implement adversarial search using the minimax algorithm. It discusses representing the game state as a tree, defining an evaluation function to assign values to terminal states, and building a minimax tree to choose the best move for the maximizing player by maximizing values and minimizing the opponent's values. Pseudocode is provided for the minimax algorithm and an example implementation for tic-tac-toe to find the best opening move.

Uploaded by

Gaurang Patyane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

T21-86 AI Exp6

The document describes an AI lab experiment to implement adversarial search using the minimax algorithm. It discusses representing the game state as a tree, defining an evaluation function to assign values to terminal states, and building a minimax tree to choose the best move for the maximizing player by maximizing values and minimizing the opponent's values. Pseudocode is provided for the minimax algorithm and an example implementation for tic-tac-toe to find the best opening move.

Uploaded by

Gaurang Patyane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

AI Lab Experiment No.

Aim: Implement adversarial search using min-max algorithm.

Theory:

Adversarial search is a fundamental concept in artificial intelligence used in games, decision-


making, and optimization problems. The Min-Max algorithm is commonly used in
adversarial search to find the best possible move for a player in a two-player, zero-sum game,
such as chess or tic-tac-toe. The goal is to maximize the player's chances of winning while
minimizing the opponent's chances.

Here's a high-level overview of the Min-Max algorithm:


o Game Representation: Represent the game state and the available moves. In most
cases, this is done using a game tree, where each node represents a possible game
state, and the edges represent possible moves.
o Evaluation Function: Define an evaluation function that assigns a value to each
terminal state (i.e., a state where the game ends). This function should be designed to
reflect the desirability of the outcome for the player.
o Min-Max Tree: Build a tree of possible game states, starting from the current state
and expanding it recursively. At each level, alternate between two players: the
maximizing player (Max) and the minimizing player (Min).
o Minimize and Maximize: For each Min node, choose the child node with the lowest
value, and for each Max node, choose the child node with the highest value. This is
done because Min represents the opponent's move, and Max represents the player's
move.
o Backpropagation: Propagate the values up the tree until you reach the root node. The
root node's selected move will be the one leading to the child with the highest value.
o Alpha-Beta Pruning (optional): To optimize the Min-Max algorithm, you can
implement alpha-beta pruning, which reduces the number of nodes evaluated by
eliminating branches that are guaranteed not to affect the final result.

TSEC T21 Gaurang Patyane-86


AI Lab Experiment No.6

CODE:
def print_board(board):
for row in board:
print(' '.join(row))

def initialize_game():
# Create an empty 3x3 tic-tac-toe board
return [[' ' for _ in range(3)] for _ in range(3)]

def game_over(state):
# Check if the game is over (e.g., someone has won or it's a
draw)
for row in state:
if row.count(row[0]) == len(row) and row[0] != ' ':
return True

for col in range(len(state[0])):


check = []
for row in state:
check.append(row[col])
if check.count(check[0]) == len(check) and check[0] != ' ':
return True

if state[0][0] == state[1][1] == state[2][2] != ' ':


return True

if state[0][2] == state[1][1] == state[2][0] != ' ':


return True

if all(cell != ' ' for row in state for cell in row):


return True

return False

TSEC T21 Gaurang Patyane-86


AI Lab Experiment No.6

def evaluate(state):
# Evaluate the current game state (e.g., assign scores for
different outcomes)
if game_over(state) and state[1][1] == 'X':
return 1 # Player X wins
elif game_over(state) and state[1][1] == 'O':
return -1 # Player O wins
else:
return 0 # It's a draw

def get_possible_moves(state):
# Get a list of possible moves from the current state
moves = []
for i in range(len(state)):
for j in range(len(state[0])):
if state[i][j] == ' ':
new_state = [row[:] for row in state]
new_state[i][j] = 'X'
moves.append(new_state)
return moves

def min_max(state, depth, maximizing_player):


if depth == 0 or game_over(state):
return evaluate(state)

if maximizing_player:
max_eval = float('-inf')
for child in get_possible_moves(state):
eval = min_max(child, depth - 1, False)
max_eval = max(max_eval, eval)
return max_eval
else:
min_eval = float('inf')
for child in get_possible_moves(state):

TSEC T21 Gaurang Patyane-86


AI Lab Experiment No.6

eval = min_max(child, depth - 1, True)


min_eval = min(min_eval, eval)
return min_eval

def find_best_move(state, depth):


best_move = None
best_eval = float('-inf')
for move in get_possible_moves(state):
eval = min_max(move, depth - 1, False)
if eval > best_eval:
best_eval = eval
best_move = move
return best_move

# Example usage
initial_state = initialize_game()
best_move = find_best_move(initial_state, depth=3)

# Print the intermediate board states


for move in best_move:
print_board(move)

# Print the final best move (3x3 tic-tac-toe board)


print("Best Move:")
print_board(best_move)

TSEC T21 Gaurang Patyane-86


AI Lab Experiment No.6

OUTPUT :

CONCLUSION :
In this experiment, we implemented the Min-Max algorithm for adversarial search,
which is commonly used in two-player, zero-sum games. We learned how to represent
the game state, construct a game tree, and evaluate possible moves using an evaluation
function. We also discussed the concept of alpha-beta pruning for optimizing the
algorithm.
By implementing and understanding the Min-Max algorithm, we have gained valuable
insights into the world of adversarial search in artificial intelligence, which can be
applied to various games and decision-making scenarios.

TSEC T21 Gaurang Patyane-86

You might also like