-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Expand file tree
/
Copy pathtic_tac_toe_gui.py
More file actions
73 lines (61 loc) · 2.45 KB
/
tic_tac_toe_gui.py
File metadata and controls
73 lines (61 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import tkinter as tk
from tkinter import messagebox
class TicTacToe:
def __init__(self):
# Create the main window
self.window = tk.Tk()
self.window.title("Tic Tac Toe - Python GUI")
# X always starts
self.turn = "X"
self.buttons = []
self.game_over = False
# Create a 3x3 Grid of Buttons
for i in range(3):
row = []
for j in range(3):
# Create a single button
btn = tk.Button(self.window, text="", font=("Arial", 20), width=5, height=2,
command=lambda x=i, y=j: self.on_click(x, y))
# Place it on the grid
btn.grid(row=i, column=j)
row.append(btn)
self.buttons.append(row)
# Start the application loop
self.window.mainloop()
def on_click(self, row, col):
# If the button is already clicked or game is over, do nothing
if self.game_over or self.buttons[row][col]["text"] != "":
return
# Set the button text to X or O
self.buttons[row][col]["text"] = self.turn
# Check if someone won
if self.check_winner():
messagebox.showinfo("Game Over", f"Player {self.turn} wins!")
self.game_over = True
elif self.check_draw():
messagebox.showinfo("Game Over", "It's a Draw!")
self.game_over = True
else:
# Switch turns
self.turn = "O" if self.turn == "X" else "X"
def check_winner(self):
# Check all rows, columns, and diagonals for a match
for i in range(3):
if self.buttons[i][0]["text"] == self.buttons[i][1]["text"] == self.buttons[i][2]["text"] != "":
return True
if self.buttons[0][i]["text"] == self.buttons[1][i]["text"] == self.buttons[2][i]["text"] != "":
return True
# Diagonals
if self.buttons[0][0]["text"] == self.buttons[1][1]["text"] == self.buttons[2][2]["text"] != "":
return True
if self.buttons[0][2]["text"] == self.buttons[1][1]["text"] == self.buttons[2][0]["text"] != "":
return True
return False
def check_draw(self):
for row in self.buttons:
for btn in row:
if btn["text"] == "":
return False
return True
if __name__ == "__main__":
game = TicTacToe()