-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
148 lines (123 loc) · 4.14 KB
/
main.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 tkinter import messagebox
root = tk.Tk()
root.title("Super Tic-Tac-Toe")
alphabets = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
buttons = {}
claimed = {}
isXturn = True
def checkThreeClaimed(a, b, c):
if (a in claimed and b in claimed and c in claimed):
if (claimed[a] == claimed[b] == claimed[c]):
messagebox.showinfo("GAME OVER", f"{claimed[a]} won the game")
def checkWinner():
if len(claimed) >= 3:
checkThreeClaimed("a", "b", "c")
checkThreeClaimed("d", "e", "f")
checkThreeClaimed("g", "h", "i")
checkThreeClaimed("a", "d", "g")
checkThreeClaimed("b", "e", "h")
checkThreeClaimed("c", "f", "i")
checkThreeClaimed("a", "e", "i")
checkThreeClaimed("c", "e", "g")
def checkDiag(alpha, letter):
if (aBT(f"{alpha}0") == aBT(f"{alpha}4") == aBT(f"{alpha}8") == letter):
return True
elif (aBT(f"{alpha}2") == aBT(f"{alpha}4") == aBT(f"{alpha}6") == letter):
return True
else:
return False
def enableUnclaimed(abet):
if abet in claimed:
disableGrid(abet)
else:
for i in range (0, 9):
id = f"{abet}{i}"
buttons[id].config(state = tk.NORMAL)
buttons[id].config(bg="#ffffff")
def disableGrid(alphabet):
for i in range (0, 9):
id = f"{alphabet}{i}"
buttons[id].config(state = tk.DISABLED)
buttons[id].config(bg="#808080")
def aBT(id: str):
return buttons[id].cget("text")
def checkGrid(alpha):
if checkDiag(alpha, "X"):
claimed[f"{alpha}"] = "X"
disableGrid(alpha)
elif checkDiag(alpha, "O"):
claimed[f"{alpha}"] = "O"
disableGrid(alpha)
rows = [0, 3, 6]
columns = [0, 1, 2]
for row in rows:
if checkThree(alpha, row, "X", True):
claimed[f"{alpha}"] = "X"
disableGrid(alpha)
elif checkThree(alpha, row, "O", True):
claimed[f"{alpha}"] = "O"
disableGrid(alpha)
for col in columns:
if checkThree(alpha, col, "X", False):
claimed[f"{alpha}"] = "X"
disableGrid(alpha)
elif checkThree(alpha, col, "O", False):
claimed[f"{alpha}"] = "O"
disableGrid(alpha)
def checkAll():
for i in alphabets:
checkGrid(i)
print(claimed)
def checkThree(alphabet, num, letter, row: bool):
if (row):
return aBT(f"{alphabet}{num}") == aBT(f"{alphabet}{num + 1}") == aBT(f"{alphabet}{num + 2}") == letter
else:
return aBT(f"{alphabet}{num}") == aBT(f"{alphabet}{num + 3}") == aBT(f"{alphabet}{num + 6}") == letter
def toggle():
global isXturn
isXturn = not isXturn
def disableOthers(id: str):
index = int(id[1])
alpha = alphabets[index]
for i in buttons:
if not i[0] == alpha or alpha in claimed:
buttons[i].config(state = tk.DISABLED)
buttons[i].config(bg="#808080")
if alpha in claimed:
for j in alphabets:
enableUnclaimed(j)
else:
buttons[i].config(state = tk.NORMAL)
buttons[i].config(bg="#ffffff")
def changeText(id: str):
if (isXturn):
buttons[id].config(text = "X")
toggle()
checkAll()
disableOthers(id=id)
else:
buttons[id].config(text = "O")
toggle()
checkAll()
disableOthers(id=id)
def createGrid(alphabet, columnStart, rowStart):
rowCounter = rowStart
columnCounter = columnStart
for i in range(0, 9):
button = tk.Button(root, text=" ", height=1, width=3, font=("Helvetica", 30), command=lambda sid=f"{alphabet}{i}": changeText(sid))
button.grid(row=rowCounter, column=columnCounter)
buttons[f"{alphabet}{i}"] = button
columnCounter = columnCounter + 1
if columnCounter == columnStart + 3:
rowCounter = rowCounter + 1
columnCounter = columnStart
rCounter = 0
cCounter = 0
for i in alphabets:
createGrid(alphabet=i, columnStart=cCounter, rowStart=rCounter)
cCounter = cCounter + 3
if cCounter == 9:
cCounter = 0
rCounter += 3
root.mainloop()