Tic Tac Toe Game
import tkinter as tk
from tkinter import messagebox
import random
def main_menu():
# Create the main menu window
menu = tk.Tk()
menu.title("Tic Tac Toe")
menu.geometry("300x200")
# Title label
title_label = tk.Label(menu, text="Tic Tac Toe", font=("Helvetica", 16))
title_label.pack(pady=10)
# Mode selection buttons
btn_single = tk.Button(menu, text="Single Player", width=15,
command=lambda: start_single_player(menu))
btn_multi = tk.Button(menu, text="Two Player", width=15,
command=lambda: start_two_player(menu))
btn_exit = tk.Button(menu, text="Exit", width=15,
command=menu.destroy)
btn_single.pack(pady=5)
btn_multi.pack(pady=5)
btn_exit.pack(pady=5)
menu.mainloop()
def start_two_player(menu):
# Close the menu and open the two-player game window
menu.destroy()
game = tk.Tk()
game.title("Tic Tac Toe - Two Player")
player = "X" # Current player ("X" starts)
# Track board state: None=empty, "X" or "O" for marks
state = [[None]*3 for _ in range(3)]
game_over = False
def check_win(mark):
# Return True if the given mark has a winning line
for i in range(3):
if state[i][0] == state[i][1] == state[i][2] == mark:
return True
if state[0][i] == state[1][i] == state[2][i] == mark:
return True
if state[0][0] == state[1][1] == state[2][2] == mark:
return True
if state[0][2] == state[1][1] == state[2][0] == mark:
return True
return False
def check_tie():
# Return True if all cells are filled (and no win)
for row in state:
if None in row:
return False
return True
def click_cell(r, c):
nonlocal player, game_over
# Ignore clicks if game is over or cell is occupied
if game_over or state[r][c] is not None:
return
# Mark the cell for current player
state[r][c] = player
buttons[r][c].config(text=player, state="disabled")
# Check for win
if check_win(player):
game_over = True
messagebox.showinfo("Game Over", f"Player {player} wins!")
# Ask to play again
if messagebox.askyesno("Play Again", "Do you want to play again?"):
game.destroy()
main_menu()
else:
game.destroy()
return
# Check for tie
if check_tie():
game_over = True
messagebox.showinfo("Game Over", "It's a tie!")
if messagebox.askyesno("Play Again", "Do you want to play again?"):
game.destroy()
main_menu()
else:
game.destroy()
return
# Switch player
player = "O" if player == "X" else "X"
status_label.config(text=f"Player {player}'s turn")
# Label to show whose turn it is
status_label = tk.Label(game, text="Player X's turn", font=("Helvetica", 14))
status_label.grid(row=0, column=0, columnspan=3, pady=5)
# Create 3x3 grid of buttons for the board
buttons = [[None]*3 for _ in range(3)]
for i in range(3):
for j in range(3):
buttons[i][j] = tk.Button(game, text="", font=("Helvetica", 20),
width=4, height=2,
command=lambda r=i, c=j: click_cell(r, c))
buttons[i][j].grid(row=i+1, column=j, padx=5, pady=5)
game.mainloop()
def start_single_player(menu):
# Close the menu and open the single-player game window
menu.destroy()
game = tk.Tk()
game.title("Tic Tac Toe - Single Player")
# Board state: None=empty, "X"=human, "O"=computer
state = [[None]*3 for _ in range(3)]
game_over = False
def check_win(mark):
# Same win check as above
for i in range(3):
if state[i][0] == state[i][1] == state[i][2] == mark:
return True
if state[0][i] == state[1][i] == state[2][i] == mark:
return True
if state[0][0] == state[1][1] == state[2][2] == mark:
return True
if state[0][2] == state[1][1] == state[2][0] == mark:
return True
return False
def check_tie():
for row in state:
if None in row:
return False
return True
def computer_move():
nonlocal game_over
# Choose a random empty cell for the computer's "O" move
empty_cells = [(r, c) for r in range(3) for c in range(3) if state[r][c] is None]
if not empty_cells:
return
r, c = random.choice(empty_cells)
state[r][c] = "O"
buttons[r][c].config(text="O", state="disabled")
# Check if computer won
if check_win("O"):
game_over = True
messagebox.showinfo("Game Over", "Computer wins!")
if messagebox.askyesno("Play Again", "Do you want to play again?"):
game.destroy()
main_menu()
else:
game.destroy()
return
# Check for tie
if check_tie():
game_over = True
messagebox.showinfo("Game Over", "It's a tie!")
if messagebox.askyesno("Play Again", "Do you want to play again?"):
game.destroy()
main_menu()
else:
game.destroy()
return
def click_cell(r, c):
nonlocal game_over
# Ignore if over or cell occupied
if game_over or state[r][c] is not None:
return
# Human plays "X"
state[r][c] = "X"
buttons[r][c].config(text="X", state="disabled")
status_label.config(text="Computer's turn")
# Check if human wins
if check_win("X"):
game_over = True
messagebox.showinfo("Game Over", "You win!")
if messagebox.askyesno("Play Again", "Do you want to play again?"):
game.destroy()
main_menu()
else:
game.destroy()
return
# Check for tie
if check_tie():
game_over = True
messagebox.showinfo("Game Over", "It's a tie!")
if messagebox.askyesno("Play Again", "Do you want to play again?"):
game.destroy()
main_menu()
else:
game.destroy()
return
# Computer makes a move
computer_move()
if not game_over:
status_label.config(text="Your turn (X)")
# Status label at top
status_label = tk.Label(game, text="Your turn (X)", font=("Helvetica", 14))
status_label.grid(row=0, column=0, columnspan=3, pady=5)
# Create board buttons
buttons = [[None]*3 for _ in range(3)]
for i in range(3):
for j in range(3):
buttons[i][j] = tk.Button(game, text="", font=("Helvetica", 20),
width=4, height=2,
command=lambda r=i, c=j: click_cell(r, c))
buttons[i][j].grid(row=i+1, column=j, padx=5, pady=5)
game.mainloop()
# Launch the application
if __name__ == "__main__":
main_menu()
#Output
Comments
Post a Comment