-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
275 lines (198 loc) · 7.08 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
from tkinter import *
from tkinter import ttk
import random
board_list = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
move_count = 0
def winner_popup(winner):
"""
Creates a small popup window that prints the winner into a popup window
:param winner: string of the winner of the game ("draw" if the game is
a draw)
"""
# create a new window
pop_up = Tk()
pop_up.maxsize(300, 100)
pop_up.geometry("300x100")
# set the title and label for the winner
pop_up.wm_title("The Winner is...")
label = Label(pop_up, text="{}!".format(winner).title())
label.pack(side="top", fill="x", pady=10)
# Make an exit button for the window
exit_button = Button(pop_up, text="Okay", command=pop_up.destroy)
exit_button.pack()
pop_up.mainloop()
def check_win(board_list):
"""
Checks the given board and outputs the winner
:param board_list: 2D list of strings and ints to represent the board
"""
for i in range(0, 3):
# rows
if board_list[i][0] == board_list[i][1] == board_list[i][2]:
if board_list[i][0] != 0:
return board_list[i][0]
# columns
elif board_list[0][i] == board_list[1][i] == board_list[2][i]:
if board_list[0][i] != 0:
return board_list[0][i]
# diagonal (top left to bottom right)
elif board_list[0][0] == board_list[1][1] == board_list[2][2]:
if board_list[0][0] != 0:
return board_list[0][0]
# diagonal (bottom left to top right)
elif board_list[0][2] == board_list[1][1] == board_list[2][0]:
if board_list[0][2] != 0:
return board_list[0][2]
return None
def draw_x(row, column):
"""
Draws a single "x" on the board with a given row and column
:param row: Integer for the row of the board that the "x" will be at
:param column: Integer for the column of the board the the "x" will be at
"""
# adjusting the column and rows to fit the grid
if column != 1:
column *= 2
column -= 1
if row != 1:
row *= 2
row -= 1
# calculating the center position of the circle
x_center = row * 100
y_center = column * 77
radius = 40
# from top left to bottom right
x0 = x_center - radius
y0 = y_center - radius
x1 = x_center + radius
y1 = y_center + radius
canvas.create_line(x0, y0, x1, y1, fill="blue")
# from bottom left to top right
x2 = x_center + radius - 80
y2 = y_center + radius
x3 = x_center - radius + 80
y3 = y_center - radius
canvas.create_line(x2, y2, x3, y3, fill="blue")
return None
def draw_o(row, column):
"""
Drawa a single "o" on the board with a given row and column
:param row: Integer for the row of the board that the "o" will be at
:param column: Integer for the column of the board the the "o" will be at
"""
# adjusting the column and rows to fit the grid
if column != 1:
column *= 2
column -= 1
if row != 1:
row *= 2
row -= 1
# calculating the center position of the circle
x_center = row * 100
y_center = column * 77
radius = 40
# create the base circle (the red part)
x0 = x_center - radius
y0 = y_center - radius
x1 = x_center + radius
y1 = y_center + radius
canvas.create_oval(x0, y0, x1, y1, fill="red", outline="white")
# create the inner circle (the hole)
x2 = x_center - (radius // 1.05)
y2 = y_center - (radius // 1.05)
x3 = x_center + (radius // 1.05)
y3 = y_center + (radius // 1.05)
canvas.create_oval(x2, y2, x3, y3, fill="white", outline="white")
return None
def draw_board(board_list):
"""
Draws the board in the game space
:param board_list: 2D list of strings and ints to represent the board
"""
# clears out the canvas to make an empty board
canvas.delete("all")
# Horizontal Lines
canvas.create_rectangle(600, 170, 0, 160, fill="black")
canvas.create_rectangle(600, 330, 0, 320, fill="black")
# Vertical Lines
canvas.create_rectangle(210, 480, 200, 0, fill="black")
canvas.create_rectangle(410, 480, 400, 0, fill="black")
# iterate through the board and draw each of the positions
for row in range(len(board_list)):
for column in range(len(board_list[0])):
if board_list[row][column] == "x":
draw_x(row + 1, column + 1)
elif board_list[row][column] == "o":
draw_o(row + 1, column + 1)
winner = check_win(board_list)
# count how many empty spaces are left on the board
empty_count = 0
for i in range(len(board_list)):
empty_count += board_list[i].count(0)
# no spaces left and there is still no winner
if (winner is None) and (empty_count == 0):
winner = "Draw"
winner_popup(winner)
# There is a winner
elif winner is not None:
winner_popup(winner)
def end_turn():
"""
Starts the game or goes to the next move
"""
global board_list
global move_count
try:
# pull the inputs
row_input = int(row_entry.get()) - 1
column_input = int(column_entry.get()) - 1
# only draw the board when both values are in a reasonble range
if (
(row_input >= 0 and row_input <= 2)
and (column_input >= 0)
and (column_input <= 2)
):
# checks if the position was taken already
if board_list[row_input][column_input] != 0:
raise ValueError("Position was taken already")
else:
# player 1 has X's
if (move_count % 2) == 0:
board_list[row_input][column_input] = "x"
# player 2 has O's
else:
board_list[row_input][column_input] = "o"
move_count += 1
draw_board(board_list)
# produces the following output if not
else:
print("One(or both) of your values is out of range")
# if a number is not both boxes
except ValueError:
print("There is an error. Please try again.")
# make the base window
root = Tk()
root.title("Tic-Tac-Toe")
root.maxsize(900, 600)
root.config(bg="black")
opponent_choice = StringVar()
# create the options menu abuve
ui_frame = Frame(root, width=600, height=100, bg="grey")
ui_frame.grid(row=0, column=0, padx=10, pady=5)
# create the game space bwlow
canvas = Canvas(root, width=600, height=480, bg="white")
canvas.grid(row=1, column=0, padx=10, pady=5)
# makes the start button/ end turn button
Button(ui_frame, text="End Turn", command=end_turn, bg="red").grid(
row=2, column=3, padx=5, pady=5
)
# Row input
Label(ui_frame, text="Column:", bg="grey").grid(row=1, column=0, padx=5, pady=5)
row_entry = Entry(ui_frame)
row_entry.grid(row=1, column=1, padx=5, pady=5)
# Column input
Label(ui_frame, text="Row:", bg="grey").grid(row=2, column=0, padx=5, pady=5)
column_entry = Entry(ui_frame)
column_entry.grid(row=2, column=1, padx=5, pady=5)
# run the main loop and start the application
root.mainloop()