0% found this document useful (0 votes)
16 views

AI Lab Assigngment1

The document describes an AI assignment to develop a Tic-Tac-Toe game using the MinMax algorithm with two modes: player vs player and player vs computer. It includes the Python source code to create the GUI, board, player turns, check for winners/ties, and use MinMax to calculate the computer's best move.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

AI Lab Assigngment1

The document describes an AI assignment to develop a Tic-Tac-Toe game using the MinMax algorithm with two modes: player vs player and player vs computer. It includes the Python source code to create the GUI, board, player turns, check for winners/ties, and use MinMax to calculate the computer's best move.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

AI LAB ASSIGNMENT 1

Name: Mustafa Ali (13035),


M. Humza Feroz (12589)

Q1) Develop the Tic-Tac-Toe game (with two players and GUI based) using MinMax algorithm.
Source Code
# Import necessary libraries
import tkinter as tk
from tkinter import messagebox

# Define the TicTacToe class


class TicTacToe:
def __init__(self, master):
self.master = master
self.master.title("Tic Tac Toe")
self.board = [["" for _ in range(3)] for _ in range(3)]
self.buttons = [[None]*3 for _ in range(3)]
self.current_player = "X"
self.mode = tk.StringVar(value="player_vs_player")
self.create_board()
self.create_mode_selection()

def create_board(self):
for i in range(3):
for j in range(3):
self.buttons[i][j] = tk.Button(self.master, text="", font=("Helvetica",
20), width=5, height=2,
command=lambda row=i, col=j:
self.on_click(row, col))
self.buttons[i][j].grid(row=i, column=j)

def create_mode_selection(self):
player_vs_player_radio = tk.Radiobutton(self.master, text="Player vs Player",
variable=self.mode,
value="player_vs_player",
command=self.reset_board)
player_vs_player_radio.grid(row=3, column=0, padx=5, pady=5)
player_vs_computer_radio = tk.Radiobutton(self.master, text="Player vs Computer",
variable=self.mode,
value="player_vs_computer",
command=self.reset_board)
player_vs_computer_radio.grid(row=3, column=1, padx=5, pady=5)

def on_click(self, row, col):


if self.buttons[row][col]["text"] == "" and not self.check_game_over():
self.buttons[row][col]["text"] = self.current_player
self.board[row][col] = self.current_player
if self.check_winner(self.current_player):
messagebox.showinfo("Winner!", f"Player {self.current_player} wins!")
self.reset_board()
elif self.check_tie():
messagebox.showinfo("Tie!", "It's a tie!")
AI LAB ASSIGNMENT 1
self.reset_board()
else:
self.toggle_player()
if self.mode.get() == "player_vs_computer" and self.current_player == "O":
self.computer_move()

def toggle_player(self):
self.current_player = "O" if self.current_player == "X" else "X"

def check_winner(self, player):


# Check rows, columns, and diagonals for a winner
for i in range(3):
if all(self.board[i][j] == player for j in range(3)) or \
all(self.board[j][i] == player for j in range(3)) or \
(all(self.board[i][i] == player for i in range(3))) or \
(all(self.board[i][2-i] == player for i in range(3))):
return True
return False

def check_tie(self):
return all(self.board[i][j] != "" for i in range(3) for j in range(3))

def check_game_over(self):
return self.check_winner("X") or self.check_winner("O") or self.check_tie()

def reset_board(self):
for i in range(3):
for j in range(3):
self.buttons[i][j]["text"] = ""
self.board[i][j] = ""
self.current_player = "X"
if self.mode.get() == "player_vs_computer" and self.current_player == "O":
self.computer_move()

def computer_move(self):
best_score = float("-inf")
best_move = None
for i in range(3):
for j in range(3):
if self.board[i][j] == "":
self.board[i][j] = "O"
score = self.minimax(self.board, False)
self.board[i][j] = ""
if score > best_score:
best_score = score
best_move = (i, j)
row, col = best_move
self.buttons[row][col]["text"] = "O"
self.board[row][col] = "O"
if self.check_winner("O"):
messagebox.showinfo("Winner!", "Computer wins!")
self.reset_board()
elif self.check_tie():
messagebox.showinfo("Tie!", "It's a tie!")
self.reset_board()
else:
self.toggle_player()

def minimax(self, board, is_maximizing):


if self.check_winner("O"):
AI LAB ASSIGNMENT 1
return 1
elif self.check_winner("X"):
return -1
elif self.check_tie():
return 0

if is_maximizing:
best_score = float("-inf")
for i in range(3):
for j in range(3):
if board[i][j] == "":
board[i][j] = "O"
score = self.minimax(board, False)
board[i][j] = ""
best_score = max(score, best_score)
return best_score
else:
best_score = float("inf")
for i in range(3):
for j in range(3):
if board[i][j] == "":
board[i][j] = "X"
score = self.minimax(board, True)
board[i][j] = ""
best_score = min(score, best_score)
return best_score

def main():
root = tk.Tk()
game = TicTacToe(root)
root.mainloop()

if __name__ == "__main__":
main()

Output:

You might also like