Question: # Tic - Tac - Toe with Minimax Algorithm and Alpha - Beta Pruning import numpy as np # Initialize the board ( empty =

# Tic-Tac-Toe with Minimax Algorithm and Alpha-Beta Pruning
import numpy as np
# Initialize the board (empty =0, X =1, O =-1)
def initialize_board():
return np.zeros((3,3), dtype=int)
# Check for win condition (1 for X,-1 for O,0 for draw or incomplete)
def check_winner(board):
for i in range(3):
# Check rows and columns
if abs(sum(board[i, :]))==3:
return board[i,0]
if abs(sum(board[:, i]))==3:
return board[0, i]
# Check diagonals
if abs(sum(board.diagonal()))==3:
return board[0,0]
if abs(sum(np.fliplr(board).diagonal()))==3:
return board[0,2]
# No winner
return 0
# Check if the board is full (draw)
def is_board_full(board):
return not (board ==0).any()
# Minimax function with Alpha-Beta Pruning
def minimax(board, depth, alpha, beta, is_maximizing):
winner = check_winner(board)
if winner !=0:
return winner # return the utility value: +1 for X,-1 for O
if is_board_full(board):
return 0 # Draw case
if is_maximizing: # Maximizing for X
max_eval =-np.inf
for i in range(3):
for j in range(3):
if board[i, j]==0: # Check if the cell is empty
board[i, j]=1 # X makes a move
eval = minimax(board, depth +1, alpha, beta, False)
board[i, j]=0 # Undo move
max_eval = max(max_eval, eval)
alpha = max(alpha, eval)
if beta <= alpha:
break # Beta cut-off
return max_eval
else: # Minimizing for O
min_eval = np.inf
for i in range(3):
for j in range(3):
if board[i, j]==0: # Check if the cell is empty
board[i, j]=-1 # O makes a move
eval = minimax(board, depth +1, alpha, beta, True)
board[i, j]=0 # Undo move
min_eval = min(min_eval, eval)
beta = min(beta, eval)
if beta <= alpha:
break # Alpha cut-off
return min_eval
# Best move for X (Maximizing player)
def best_move(board):
best_val =-np.inf
move =(-1,-1)
for i in range(3):
for j in range(3):
if board[i, j]==0: # If cell is empty
board[i, j]=1 # X makes a move
move_val = minimax(board,0,-np.inf, np.inf, False)
board[i, j]=0 # Undo move
if move_val > best_val:
move =(i, j)
best_val = move_val
return move
# Display the board
def display_board(board):
chars ={1: 'X',-1: 'O',0: ''}
for row in board:
print('|'.join([chars[cell] for cell in row]))
print('-'*9)
# Simulate a game
def play_game():
board = initialize_board()
current_player =1 # X starts
while check_winner(board)==0 and not is_board_full(board):
display_board(board)
if current_player ==1: # X's turn (AI)
i, j = best_move(board)
print(f"X moves to ({i},{j})")
board[i, j]=1
else: # O's turn (random play for demo purposes)
i, j = np.random.choice(np.where(board ==0)[0]), np.random.choice(np.where(board ==0)[1])
print(f"O moves to ({i},{j})")
board[i, j]=-1
current_player *=-1 # Switch players
display_board(board)
winner = check_winner(board)
if winner ==1:
print("X wins!")
elif winner ==-1:
print("O wins!")
else:
print("It's a draw!")
# Run the simulation
play_game()
give the decision tree precisely according to the code and output for the code hand drawn according to these rules:
Decision Tree:
A hand-drawn or digital decision tree that represents a full or partial game of Tic-Tac-Toe.Clearly indicate where the Minimax algorithm evaluated game states and where Alpha-beta pruning was applied.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!