221 Views тАв Sep 5, 2023 тАв Click to toggle off description
import tkinter as tk
from tkinter import messagebox
Create the main window
root = tk.Tk()
root.title("Tic-Tac-Toe")
Initialize variables
current_player = "X"
board = [" " for _ in range(9)]
Function to handle a button click
def on_click(button):
global current_player
index = int(button["text"]) - 1
if board[index] == " ":
board[index] = current_player
button["text"] = current_player
if check_winner(current_player):
messagebox.showinfo("Tic-Tac-Toe", f"Player {current_player} wins!")
reset_game()
elif " " not in board:
messagebox.showinfo("Tic-Tac-Toe", "It's a tie!")
reset_game()
else:
current_player = "O" if current_player == "X" else "X"
Function to check for a win
def check_winner(player):
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)]
for combo in winning_combinations:
if board[combo[0]] == board[combo[1]] == board[combo[2]] == player:
return True
return False
Function to reset the game
def reset_game():
global current_player, board
current_player = "X"
board = [" " for _ in range(9)]
for button in buttons:
button["text"] = " "
Create buttons for the Tic-Tac-Toe grid
buttons = [tk.Button(root, text=str(i + 1), font=("Helvetica", 24), width=3, height=1, command=lambda i=i: on_click(buttons[i]))
for i in range(9)]
Arrange buttons in a 3x3 grid
for i, button in enumerate(buttons):
row = i // 3
col = i % 3
button.grid(row=row, column=col)
Create a "Restart" button
restart_button = tk.Button(root, text="Restart", font=("Helvetica", 14), width=10, height=1, command=reset_game)
restart_button.grid(row=3, column=1)
Start the GUI main loop
root.mainloop()
Metadata And Engagement
Views : 221
Genre: People & Blogs
License: Standard YouTube License
Uploaded At Sep 5, 2023 ^^
warning: returnyoutubedislikes may not be accurate, this is just an estiment ehe :3
Rating : 5 (0/7 LTDR)
100.00% of the users lieked the video!!
0.00% of the users dislieked the video!!
User score: 100.00- Masterpiece Video
RYD date created : 2024-02-09T10:59:09.57853Z
See in json
@UP14_Programmer
1 year ago
import tkinter as tk
from tkinter import messagebox
# Create the main window
root = tk.Tk()
root.title("Tic-Tac-Toe")
# Initialize variables
current_player = "X"
board = [" " for _ in range(9)]
# Function to handle a button click
def on_click(button):
global current_player
index = int(button["text"]) - 1
if board[index] == " ":
board[index] = current_player
button["text"] = current_player
if check_winner(current_player):
messagebox.showinfo("Tic-Tac-Toe", f"Player {current_player} wins!")
reset_game()
elif " " not in board:
messagebox.showinfo("Tic-Tac-Toe", "It's a tie!")
reset_game()
else:
current_player = "O" if current_player == "X" else "X"
# Function to check for a win
def check_winner(player):
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)]
for combo in winning_combinations:
if board[combo[0]] == board[combo[1]] == board[combo[2]] == player:
return True
return False
# Function to reset the game
def reset_game():
global current_player, board
current_player = "X"
board = [" " for _ in range(9)]
for button in buttons:
button["text"] = " "
# Create buttons for the Tic-Tac-Toe grid
buttons = [tk.Button(root, text=str(i + 1), font=("Helvetica", 24), width=3, height=1, command=lambda i=i: on_click(buttons[i]))
for i in range(9)]
# Arrange buttons in a 3x3 grid
for i, button in enumerate(buttons):
row = i // 3
col = i % 3
button.grid(row=row, column=col)
# Create a "Restart" button
restart_button = tk.Button(root, text="Restart", font=("Helvetica", 14), width=10, height=1, command=reset_game)
restart_button.grid(row=3, column=1)
# Start the GUI main loop
root.mainloop()
|