T21-86 AI Exp6
T21-86 AI Exp6
Theory:
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
return False
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
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):
# Example usage
initial_state = initialize_game()
best_move = find_best_move(initial_state, depth=3)
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.