Projects

Still In Development...

import tkinter as tk
from tkinter import messagebox, simpledialog

# Define colors for players
player_x_color = "light blue"
player_o_color = "light green"

# Function to disable all buttons after the game is over
def disable_all_buttons():
for button in button_list:
button.config(
state=tk.DISABLED)

# Function to check for a winner
def check_for_winner():
for combination in winning_combinations:
if button_list[combination[0]].cget('text') == "O" and button_list[combination[1]].cget('text') == "O" and button_list[combination[2]].cget('text') == "O":
button_list[combination[
0]].config(bg="red")
button_list[combination[
1]].config(bg="red")
button_list[combination[
2]].config(bg="red")
messagebox.showinfo(
"Tic Tac Toe", f"Congratulations! {player_o_name} wins!")
disable_all_buttons()
ask_play_again()

elif button_list[combination[0]].cget('text') == "X" and button_list[combination[1]].cget('text') == "X" and button_list[combination[2]].cget('text') == "X":
button_list[combination[
0]].config(bg="red")
button_list[combination[
1]].config(bg="red")
button_list[combination[
2]].config(bg="red")
messagebox.showinfo(
"Tic Tac Toe", f"Congratulations! {player_x_name} wins!")
disable_all_buttons()
ask_play_again()

if count == 9:
messagebox.showinfo(
"Tic Tac Toe", "It's a Tie!\n No one wins!")
disable_all_buttons()
ask_play_again()

# Function to handle button click event
def b_click(b):
global clicked, count

if b["text"] == " " and clicked == True:
b[
"text"] = "X"
b.config(bg=player_x_color)
clicked =
False
count += 1
check_for_winner()
elif b["text"] == " " and clicked == False:
b[
"text"] = "O"
b.config(bg=player_o_color)
clicked =
True
count += 1
check_for_winner()
else:
messagebox.showerror(
"Tic Tac Toe", "That box has already been selected\nPick another box...")

# Function to ask the user if they want to play again
def ask_play_again():
response = messagebox.askquestion(
"Tic Tac Toe", "Would you like to play again?")
if response == 'yes':
reset()
else:
root.quit()

# Function to start the game over
def reset():
global button_list, clicked, count
clicked =
True
count = 0

for button in button_list:
button.config(
text=" ", state=tk.NORMAL, bg="SystemButtonFace")

# Create the main window
root = tk.Tk()
root.title(
'Tic Tac Toe')

# Ask for players' names
player_x_name = simpledialog.askstring("Player Name", "Enter player X name:")
player_o_name = simpledialog.askstring(
"Player Name", "Enter player O name:")

# Initialize variables
clicked = True
count = 0

# List of buttons for the game
button_list = []

# Winning combinations
winning_combinations = [
(
0, 1, 2),
(
3, 4, 5),
(
6, 7, 8),
(
0, 3, 6),
(
1, 4, 7),
(
2, 5, 8),
(
0, 4, 8),
(
2, 4, 6)
]

# Create buttons for the game
for i in range(9):
button = tk.Button(root,
text=" ", font=('Helvetica', 20), height=3, width=6, bg="SystemButtonFace", command=lambda i=i: b_click(button_list[i]))
button.grid(
row=i//3, column=i%3)
button_list.append(button)

# Create menu
menu = tk.Menu(root)
root.config(
menu=menu)

# Create options menu
options_menu = tk.Menu(menu, tearoff=False)
menu.add_cascade(
label="Options", menu=options_menu)
options_menu.add_command(
label="Restart Game", command=reset)

root.mainloop()

I am working on how to implement python code into webpages. Until then, copy this code and paste it in to play tic tac toe in your computer.

The code will ask you to enter your and other player's names,

Enjoy!