-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollab_tic_tac_toe.py
148 lines (112 loc) · 4.65 KB
/
collab_tic_tac_toe.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import tkinter as tk
from frosch import hook
# ruff: noqa: E501
# Intializing Variables
hook()
plr = "X"
game_won = False
root = tk.Tk()
root.title("Tic Tac Toe")
root.geometry(f'{300}x{320}+{640}+{360}')
root["bg"] = "#ddd6f3"
root.columnconfigure(0, weight=1)
root.rowconfigure(1, weight=1)
root.resizable(False, False)
TicTacToeTitle = tk.Label(root, text="Tic Tac Toe", font=("Gotham", 20, "bold"), bg="#ddd6f3")
TicTacToeTitle.grid(row=0, column=0, sticky="ew")
# Check if there is a winner
def check_winner():
for i in range (len(buttonValues)):
global game_won
if game_won is True:
pass
# Horizontal Row Winning Condition
elif buttonValues[i][0] == buttonValues[i][1] and buttonValues[i][0] == buttonValues[i][2] and buttonValues[i][0] != 0:
game_won = True
# Vertical Column Winning Condition
elif buttonValues[0][i] == buttonValues[1][i] and buttonValues[0][i] == buttonValues[2][i] and buttonValues[0][i] != 0:
game_won = True
# Backward Slanting Winning Condition
elif buttonValues[i][i] == buttonValues[0][0] and buttonValues[i][i] == buttonValues[1][1] and buttonValues[i][i] == buttonValues[2][2] and buttonValues[i][i] != 0:
game_won = True
# Forward Slanting Winning Condition
elif buttonValues[0][2] == buttonValues[1][1] and buttonValues[0][2] == buttonValues[2][0] and buttonValues[0][2] != 0:
game_won = True
# Tie Condition
if check_if_tie() is True:
reset_game()
# Winning Code
if plr == "X" and game_won is True:
print("Player O won")
reset_game()
return True
elif plr == "O" and game_won is True:
print("Player X won")
reset_game()
return True
else:
return False
def check_if_tie():
global game_won
if game_won is False:
for i in range(3):
for j in range(3):
if buttonValues[i][j] == "X" or buttonValues[i][j] == "O":
if i == 2 and j == 2:
print("True Tie")
return True
elif buttonValues[i][j] == 0:
return False
else:
return False
def button_click(row, column):
global plr
if plr == "X" and buttonValues[row][column] == 0:
print(f"Button clicked: Row={row}, Column={column}")
buttonList[row][column].configure(text = plr, fg="blue")
buttonValues[row][column] = plr
plr = "O"
elif plr == "O" and buttonValues[row][column] == 0:
print(f"Button clicked: Row={row}, Column={column}")
buttonList[row][column].configure(text = plr, fg="red")
buttonValues[row][column] = plr
plr = "X"
check_winner()
def reset_game():
def onRestartClicked():
play_area.destroy()
PlayAreaBuilding()
RestartBtn.destroy()
RestartBtn = tk.Button(root, text="Restart?", command= onRestartClicked, font=("Gotham", 16, "bold"), bg="#ddd6f3", relief="solid")
RestartBtn.grid(row=0, column=0, sticky="ew", padx=5, pady=5)
def PlayAreaBuilding():
global game_won, buttonList, buttonValues, plr, play_area
game_won = False
plr = "X"
play_area = tk.Frame(root, bg="#ddd6f3")
play_area.grid(row=1, column=0, sticky="nsew")
root.bind("<Escape>", lambda e: root.destroy())
for i in range(3):
play_area.rowconfigure(i, weight=1)
play_area.columnconfigure(i, weight=1)
buttonList = [
[0,0,0],
[0,0,0],
[0,0,0]]
buttonValues = [
[0,0,0],
[0,0,0],
[0,0,0]]
for row in range(3):
for column in range(3):
buttonList[row][column] = tk.Button(play_area, text=" ", command=lambda r=row, c=column: button_click(r, c), bg="#d4aaff", borderwidth=0, font=("Gotham", 20, "bold"), width=90, height=90)
buttonList[row][column].grid(row=row, column=column, padx=5, pady=5, sticky="nsew")
def on_enter(button):
button.widget.config(background='#B591D9')
def on_leave(button):
button.widget.config(background='#d4aaff')
buttonList[row][column].bind("<Enter>", lambda button = buttonList[row][column]: on_enter(button))
buttonList[row][column].bind("<Leave>", lambda button = buttonList[row][column]: on_leave(button))
return
PlayAreaBuilding()
root.mainloop()