-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
164 lines (146 loc) · 5.88 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# Tic Tac Toe Game Board ===========================================
import tkinter as tk
from tkinter import font as tkFont
from math import floor
from time import sleep
config = {
"rows": 3,
"cols": 3,
"blank": ' ',
"x_turn": True,
"btns": {}
}
def check_win():
'''Examines the board and checks for a win'''
last_row = config.get("rows") - 1
last_col = config.get("cols") - 1
cols = False
diagonals = False
for row in range(0, config.get("rows")):
if cols == False: cols = []
if diagonals == False: diagonals = [[], []]
row_winner = True
row_val = False
for col in range(0, config.get("cols")):
val = config['btns'][row][col]['text']
if col == row: diagonals[0].append(val)
if col == (config.get("cols") - row - 1): diagonals[1].insert(0, val)
if row == 0: cols.append([])
cols[col].append(val)
if row_val == False: row_val = val
if row_val != val: row_winner = False
# Checking the last column in a row
if col >= last_col:
if row_winner and val != config.get('blank'):
player_wins(row, 0, type="Row")
else:
row_val = False
# Checking the last row
if row >= last_row:
if len(set(cols[col])) == 1 and cols[col][0] != config.get('blank'):
player_wins(0, col, type="Column")
# Checking the last cell
if row >= last_row and col >= last_col:
if (len(set(diagonals[0])) == 1 and diagonals[0][0] != config.get('blank')):
player_wins(floor(config.get('rows') / 2), floor(config.get('cols') / 2), type="Diagonal Backward")
elif (len(set(diagonals[1])) == 1 and diagonals[1][0] != config.get('blank')):
player_wins(floor(config.get('rows') / 2), floor(config.get('cols') / 2), type="Diagonal Forward")
row_val = val
def highlightWin(row, col, type):
win_color = "#66FF66"
if type == "Row":
for col in range(0, config.get("cols")):
config['btns'][row][col]['bg'] = win_color
config['btns'][row][col].config(font=fontWin)
elif type == "Column":
for row in range(0, config.get("rows")):
config['btns'][row][col]['bg'] = win_color
config['btns'][row][col].config(font=fontWin)
elif type == "Diagonal Forward":
for row in range(0, config.get("rows")):
config['btns'][row][config.get("cols") - row - 1]['bg'] = win_color
config['btns'][row][config.get("cols") - row - 1].config(font=fontWin)
elif type == "Diagonal Backward":
for row in range(0, config.get("rows")):
config['btns'][row][row]['bg'] = win_color
config['btns'][row][row].config(font=fontWin)
if config['x_turn']:
display["text"] = "O Wins!"
else:
display["text"] = "X Wins!"
def player_wins(row, col, type="unknown"):
winner = config['btns'][row][col]['text']
disableAll()
if (type != "unknown"):
highlightWin(row, col, type)
def reset():
for row in range(0, config.get("rows")):
for col in range(0, config.get("cols")):
cell = config['btns'][row][col]
cell['text'] = config['blank']
cell['bg'] = "#fff"
cell['font'] = fontNormal
cell['state'] = tk.NORMAL
config['x_turn'] = True
display['text'] = "X's Turn"
def disableAll():
for row in range(0, config.get("rows")):
for col in range(0, config.get("cols")):
config['btns'][row][col]["state"] = tk.DISABLED
def update_cell(event):
"""Updates a cell on the board, bound to a button"""
widget = event.widget
txt = widget['text']
if (txt != config.get("blank")):
return
if txt == config.get("blank") and config.get("x_turn"):
widget['text'] = 'X'
widget["state"] = tk.DISABLED
config["x_turn"] = False
display['text'] = "O's Turn"
elif txt == config.get("blank") and not(config.get("x_turn")):
widget['text'] = 'O'
widget["state"] = tk.DISABLED
config["x_turn"] = True
display['text'] = "X's Turn"
check_win()
root = tk.Tk()
root.title("Tic Tac Toe")
root.geometry("400x300");
fontNormal = tkFont.Font(family="Helvetica", size=36, weight="normal")
fontWin = tkFont.Font(family="Helvetica", size=36, weight="bold")
fontControl = tkFont.Font(family="Helvetica", size=16, weight="normal")
screen = tk.Frame(root)
screen.rowconfigure(0, weight=1)
game = tk.Frame(screen)
game.rowconfigure([0, 1, 2], minsize=5, weight=1)
game.columnconfigure([0, 1, 2], minsize=5, weight=1)
for row in range(0, config.get("rows")):
config['btns'][row] = {}
for col in range(0, config.get("cols")):
btn = tk.Button(
master=game,
text=config["blank"],
width=5,
height=5,
bg="#fff",
fg="#000",
font=fontNormal
)
btn.grid(row=row, column=col, sticky="nsew")
btn.bind("<Button-1>", lambda e: update_cell(e))
config['btns'][row][col] = btn
controls = tk.Frame(screen)
controls.columnconfigure([0], minsize=10, weight=2)
controls.columnconfigure([1], minsize=10, weight=1)
starting_display_text = "X's Turn"
if (not(config['x_turn'])):
starting_display_text = "O's Turn"
display = tk.Label(master=controls, font=fontControl, height=1, text=starting_display_text)
display.grid(row=0, column=0, sticky="nsew")
resetBtn = tk.Button(master=controls, width=5, height=1,text="Reset", bg="#ff4444", activebackground="#ff4444", fg="#fff", activeforeground="#fff", font=fontControl, command=reset, relief="groove")
resetBtn.grid(row=0, column=1, sticky="nsew", padx=5, pady=5)
controls.pack(side="top", fill="x")
game.pack(side="bottom", fill="x")
screen.pack()
root.mainloop()