0% found this document useful (0 votes)
8 views10 pages

Case Study1 Intro To AI

The document presents a case study on implementing a Noughts and Crosses game using the Minimax algorithm with Alpha-Beta pruning. It includes code for the game, detailing functions for checking winners, determining moves, and managing game flow. The analysis highlights how the algorithm enhances decision-making efficiency and reduces time complexity in gameplay.

Uploaded by

abcalok56
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)
8 views10 pages

Case Study1 Intro To AI

The document presents a case study on implementing a Noughts and Crosses game using the Minimax algorithm with Alpha-Beta pruning. It includes code for the game, detailing functions for checking winners, determining moves, and managing game flow. The analysis highlights how the algorithm enhances decision-making efficiency and reduces time complexity in gameplay.

Uploaded by

abcalok56
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/ 10

Case Study-1

Name : Verma Alokkumar Amardayal


Course : MCA
Roll no : 202410116100239
Subject : Introduction to AI
Section :D
9. Noughts and Crosses with Alpha-Beta Pruning.

CODE:
import math

# Initialize board
board = [[" " for _ in range(3)] for _ in range(3)]

# Function to print the board


def print_board(board):
for row in board:
print("|".join(row))
print("-" * 5)

# Check for a winner


def check_winner(board):
for row in board:
if row[0] == row[1] == row[2] and row[0] != " ":
return row[0]

for col in range(3):


if board[0][col] == board[1][col] == board[2][col] and
board[0][col] != " ":
return board[0][col]

if board[0][0] == board[1][1] == board[2][2] and board[0][0]


!= " ":
return board[0][0]

if board[0][2] == board[1][1] == board[2][0] and board[0][2]


!= " ":
return board[0][2]

return None

# Check if board is full (draw condition)


def is_full(board):
return all(cell != " " for row in board for cell in row)

# Minimax with Alpha-Beta Pruning


def minimax(board, depth, is_maximizing, alpha, beta):
winner = check_winner(board)
if winner == "X":
return -10 + depth # Favor faster wins
elif winner == "O":
return 10 - depth # Favor faster wins
elif is_full(board):
return 0 # Draw

if is_maximizing:
best_score = -math.inf
for i in range(3):
for j in range(3):
if board[i][j] == " ":
board[i][j] = "O"
score = minimax(board, depth + 1, False, alpha,
beta)
board[i][j] = " "
best_score = max(best_score, score)
alpha = max(alpha, best_score)
if beta <= alpha:
break # Alpha-Beta pruning
return best_score
else:
best_score = math.inf
for i in range(3):
for j in range(3):
if board[i][j] == " ":
board[i][j] = "X"
score = minimax(board, depth + 1, True, alpha,
beta)
board[i][j] = " "
best_score = min(best_score, score)
beta = min(beta, best_score)
if beta <= alpha:
break # Alpha-Beta pruning
return best_score

# Find best move for AI


def best_move(board):
best_score = -math.inf
move = (-1, -1)
for i in range(3):
for j in range(3):
if board[i][j] == " ":
board[i][j] = "O"
score = minimax(board, 0, False, -math.inf, math.inf)
board[i][j] = " "
if score > best_score:
best_score = score
move = (i, j)
return move

# Game loop
def play_game():
print("Welcome to Noughts and Crosses!")
print_board(board)

while True:
# Player Move
row, col = map(int, input("Enter row and column (0-2)
separated by space: ").split())
if board[row][col] != " ":
print("Invalid move! Try again.")
continue
board[row][col] = "X"

print_board(board)

if check_winner(board):
print("You win!")
break

if is_full(board):
print("It's a draw!")
break

# AI Move
ai_row, ai_col = best_move(board)
board[ai_row][ai_col] = "O"
print("\nAI's move:")
print_board(board)

if check_winner(board):
print("AI wins!")
break
if is_full(board):
print("It's a draw!")
break

# Start the game


play_game()
Link of code: https://colab.research.google.com/drive/1UW-
Z3VhaKVKz34Zehkx3lSRIacqfipYx?usp=sharing
Example Output:
Analysis:
❖ How It Works
• User's turn: The player inputs the row and column number
(0-2).
• AI's turn: The Minimax algorithm (with Alpha-Beta
pruning) picks the best move.
• Win/Loss conditions: If a player wins or the board is full,
the game ends.

❖ Why Use Alpha-Beta Pruning?


• Speeds up decision-making by eliminating unnecessary
branches.
• Improves efficiency compared to standard Minimax.
• Reduces time complexity significantly.

You might also like