-
Notifications
You must be signed in to change notification settings - Fork 0
/
four_in_a_row.py
338 lines (279 loc) · 11.7 KB
/
four_in_a_row.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import tkinter as tk
from engine import EngineInterface
from engine import GameState
class MainWindow(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.resizable(False, False)
self.board = Board(self)
self.board.pack()
self.player_color = "yellow"
self.engine_color = "red"
self.new_game_flag = False
self.difficulty_level = tk.StringVar()
self.difficulty_level.set("Medium")
self.player_make_first_move = True
self.protocol("WM_DELETE_WINDOW", self.close_window)
self.score = [0, 0]
self.title("Four in a row: 0 - 0")
self.animations = False
def new_game_dialog_box(self):
self.protocol("WM_DELETE_WINDOW", self.dont_close_window) # Disable close window
dialog_box = DialogBox(main_window, "New game")
if self.new_game_flag:
self.new_game_flag = False
self.protocol("WM_DELETE_WINDOW", self.close_window) # Enable close window
else:
self.destroy()
def update_difficulty_level(self, *args):
"""Update the difficulty level in the engine and reset score if
the level is changed.
"""
current_level = engine_interface.difficulty_level
if self.difficulty_level.get() == "Easy":
engine_interface.difficulty_level = 1
elif self.difficulty_level.get() == "Medium":
engine_interface.difficulty_level = 2
elif self.difficulty_level.get() == "Hard":
engine_interface.difficulty_level = 3
if engine_interface.difficulty_level != current_level:
self.score = [0, 0]
self.title_update()
def title_update(self):
self.title("Four in a row: " + str(self.score[0]) + " - " + str(self.score[1]))
def update_and_pause(self, time_in_ms):
self.board.unbind_mouse()
self.update_idletasks()
self.after(time_in_ms)
self.update() # Handle possible events.
self.board.rebind_mouse()
def mouse_click(self, column_number):
"""This function is called if the column with column_number have been
clicked on.
"""
def dialog(text):
dialog_box = DialogBox(main_window, text)
if self.new_game_flag:
self.protocol("WM_DELETE_WINDOW", self.close_window) # Enable close window
self.new_game()
else:
self.destroy()
self.protocol("WM_DELETE_WINDOW", self.dont_close_window) # Disable close window
# Player make a move, if there is empty places left in the column.
if engine_interface.legal(column_number):
engine_interface.make_move(column_number)
self.board.add_disk_to_top_of_column(column_number, self.player_color, self.animations)
self.update_idletasks()
else:
self.protocol("WM_DELETE_WINDOW", self.close_window) # Enable close window
return
# If player win.
if engine_interface.four_in_a_row():
self.score[0] += 1
self.title_update()
self.highlight_four_in_a_row(self.player_color)
self.update_and_pause(1000)
dialog("You win! Congratulations!")
return
# If draw.
if engine_interface.draw():
self.update_and_pause(600)
dialog("Draw")
return
# Engine makes a move
column_number = engine_interface.engine_move()
engine_interface.make_move(column_number)
if self.animations:
self.update_and_pause(50)
else:
self.update_and_pause(300)
self.board.add_disk_to_top_of_column(column_number, self.engine_color, self.animations)
# If engine win.
if engine_interface.four_in_a_row():
self.score[1] += 1
self.title_update()
self.highlight_four_in_a_row(self.engine_color)
self.update_and_pause(1000)
dialog("Computer win!")
return
# If draw.
if engine_interface.draw():
self.update_and_pause(600)
dialog("Draw")
return
self.protocol("WM_DELETE_WINDOW", self.close_window) # Enable close window
def highlight_four_in_a_row(self, color):
positions = engine_interface.four_in_a_row_positions()
self.update_and_pause(500)
for (column, row) in positions:
self.board.remove_disk(column, row)
self.update_and_pause(500)
for (column, row) in positions:
self.board.add_disk(column, row, color)
def new_game(self):
self.new_game_flag = False
self.player_make_first_move = not self.player_make_first_move
engine_interface.new_game()
self.board.remove_all_disks()
if not self.player_make_first_move:
column_number = engine_interface.engine_move()
engine_interface.make_move(column_number)
self.update_and_pause(300)
self.board.add_disk_to_top_of_column(column_number, self.engine_color, self.animations)
def dont_close_window(self):
pass
def close_window(self):
self.destroy()
class Board(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.parent = parent
self.column_list = []
for column_number in range(7):
column = Column(self, column_number)
column.pack(side=tk.LEFT)
self.column_list.append(column)
def mouse_click(self, column_number):
self.parent.mouse_click(column_number)
def add_disk_to_top_of_column(self, column_number, color, animations):
"""column_number is 0,1 to 6. animations is True or False."""
self.column_list[column_number].add_disk_to_top_of_column(color, animations)
def add_disk(self, column, row, color):
self.column_list[column].add_disk(row, color)
def remove_disk(self, column, row):
self.column_list[column].remove_disk(row)
def remove_all_disks(self):
for column in self.column_list:
column.remove_all_disks()
def unbind_mouse(self):
for column in self.column_list:
column.unbind_mouse()
def rebind_mouse(self):
for column in self.column_list:
column.rebind_mouse()
class Column(tk.Frame):
def __init__(self, parent, column_number):
"""column_number is 0,1 to 6 and is used as an identifier."""
tk.Frame.__init__(self, parent)
self.parent = parent
self.column_number = column_number
self.disks_in_column = 0
self.column = []
for cell in range(6):
new_cell = Cell(self, 90)
new_cell.pack(side=tk.BOTTOM)
self.column.append(new_cell)
def mouse_click(self, event):
self.parent.mouse_click(self.column_number)
def add_disk_to_top_of_column(self, color, animations):
"""animations is True or False."""
if animations:
time_in_each_row = [0.41421356237309515, 0.31783724519578205, 0.2679491924311228,
0.2360679774997898, 0.21342176528338808]
total_time = 0
min_time = 170
self.add_disk(5, color)
self.update_idletasks()
row = 4
while row >= self.disks_in_column:
pause_time = round(170*time_in_each_row[row])
self.after(pause_time)
total_time += pause_time
self.remove_disk(row + 1)
self.add_disk(row, color)
self.update_idletasks()
row -=1
if total_time < min_time:
self.after(min_time - total_time)
else:
self.add_disk(self.disks_in_column, color)
self.disks_in_column += 1
def add_disk(self, row, color):
self.column[row].add_disk(color)
def remove_disk(self, row):
self.column[row].remove_disk()
def remove_all_disks(self):
self.disks_in_column = 0
for cell in self.column:
cell.remove_disk()
def unbind_mouse(self):
for cell in self.column:
cell.unbind_mouse()
def rebind_mouse(self):
for cell in self.column:
cell.rebind_mouse()
class Cell(tk.Canvas):
def __init__(self, parent, side_length):
"""A cell is the a square-shaped piece of the board consisting of
one empty space where a disk can be placed.
"""
self.parent = parent
self.background_color = "#1439f9"
tk.Canvas.__init__(self, parent, width=side_length, height=side_length,
bg=self.background_color, highlightthickness=0)
# An odd diameter can give a better looking circle.
radius = (9 * side_length) // 20
d = (side_length - (2 * radius + 1)) // 2
self.disk = self.create_oval(d, d, d + 2 * radius + 1, d + 2 * radius + 1,
width=2, outline="#0000AA")
self.bind("<Button-1>", parent.mouse_click)
def add_disk(self, color):
self.itemconfig(self.disk, fill=color)
def remove_disk(self):
self.itemconfig(self.disk, fill=self.background_color)
def unbind_mouse(self):
self.unbind("<Button-1>")
def rebind_mouse(self):
self.bind("<Button-1>", self.parent.mouse_click)
class DialogBox(tk.Toplevel):
def __init__(self, parent, text):
"""Return 'play' or 'quit'."""
tk.Toplevel.__init__(self, parent)
self.parent = parent
self.transient(parent)
self.title("Four in a row")
box_width = 300
box_height = 120
parent_width = parent.winfo_width()
parent_height = parent.winfo_height()
if box_width >= parent_width:
x_offset = parent.winfo_rootx()
else:
x_offset = parent.winfo_rootx() + (parent_width - box_width) // 2
y_offset = parent.winfo_rooty() + (parent_height - box_height - 40) // 2
if y_offset < parent.winfo_rooty():
y_offset = parent.winfo_rooty()
self.geometry("%dx%d+%d+%d" % (box_width, box_height, x_offset, y_offset))
self.wait_visibility() # Window needs to be visible for the grab.
self.grab_set() # Routes all events for this application to this widget.
self.focus_set()
text = tk.Label(self, text=text, font=("", 11, "bold"), borderwidth=10)
text.pack()
radio_button_frame = tk.Frame(master=self)
tk.Radiobutton(radio_button_frame, text="Easy", font=("", 10),
variable=parent.difficulty_level, value="Easy").pack(side=tk.LEFT)
tk.Radiobutton(radio_button_frame, text="Medium", font=("", 10),
variable=parent.difficulty_level, value="Medium").pack(side=tk.LEFT)
tk.Radiobutton(radio_button_frame, text="Hard", font=("", 10),
variable=parent.difficulty_level, value="Hard").pack()
radio_button_frame.pack()
button_frame = tk.Frame(master=self, pady=10)
button_frame.pack()
tk.Button(button_frame, text="Play", font=("", 10), width=8,
command=self.play).pack(side=tk.LEFT)
tk.Button(button_frame, text="Quit", font=("", 10), width=8,
command=self.quit).pack()
self.bind("<Return>", self.play)
self.bind("<Escape>", self.quit)
parent.wait_window(window=self) # Wait for the dialog box to be destroyed.
def play(self, event=None):
self.parent.new_game_flag = True
self.parent.update_difficulty_level()
self.destroy()
def quit(self, event=None):
self.destroy()
engine_interface = EngineInterface(2)
main_window = MainWindow()
main_window.update()
main_window.new_game_dialog_box()
main_window.mainloop()