-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.py
325 lines (293 loc) · 13.1 KB
/
maze.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
from tkinter import Tk, BOTH, Canvas
import time
import random
class Window:
def __init__(self, width, height):
self.__root = Tk()
self.__root.title("boot.dev maze course")
self.__canvas = Canvas(self.__root, width=width, height=height)
self.__canvas.pack(fill=BOTH, expand=True)
self.__window_running = False
self.__root.protocol("WM_DELETE_WINDOW", self.close)
self._ww = width
self._wh = height
def redraw(self):
self.__root.update_idletasks()
self.__root.update()
def wait_for_close(self):
self.__window_running = True
while self.__window_running:
self.redraw()
def draw_line(self, line, fill_color="black"):
line.draw(self.__canvas, fill_color)
def close(self):
self.__window_running = False
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
class Line:
def __init__(self, point1, point2):
self.point1 = point1
self.point2 = point2
def draw(self, canvas, fill_color="black"):
canvas.create_line(self.point1.x, self.point1.y, self.point2.x, self.point2.y, fill=fill_color, width=2)
canvas.pack(fill=BOTH, expand=True)
class Cell:
def __init__(self, x1, y1, x2, y2, win=None):
self._x1 = x1
self._y1 = y1
self._x2 = x2
self._y2 = y2
self._win = win
self.has_left_wall = True
self.has_right_wall = True
self.has_top_wall = True
self.has_bottom_wall = True
self.dead_end = False
self.visited = False
self.left_wall = Line(Point(self._x1, self._y1), Point(self._x1, self._y2))
self.right_wall = Line(Point(self._x2, self._y1), Point(self._x2, self._y2))
self.top_wall = Line(Point(self._x1, self._y1), Point(self._x2, self._y1))
self.bottom_wall = Line(Point(self._x1, self._y2), Point(self._x2, self._y2))
def draw(self):
if self._win is None:
return
if self.has_left_wall:
self._win.draw_line(self.left_wall)
else:
self._win.draw_line(self.left_wall, "#d9d9d9")
if self.has_right_wall:
self._win.draw_line(self.right_wall)
else:
self._win.draw_line(self.right_wall, "#d9d9d9")
if self.has_top_wall:
self._win.draw_line(self.top_wall)
else:
self._win.draw_line(self.top_wall, "#d9d9d9")
if self.has_bottom_wall:
self._win.draw_line(self.bottom_wall)
else:
self._win.draw_line(self.bottom_wall, "#d9d9d9")
def draw_move(self, to_cell, undo=False):
if self._win is None:
return
self.from_cell_middle_x = (self._x2 + self._x1) / 2
self.from_cell_middle_y = (self._y2 + self._y1) / 2
self.to_cell_middle_x = (to_cell._x2 + to_cell._x1) / 2
self.to_cell_middle_y = (to_cell._y2 + to_cell._y1) / 2
self.from_cell_point = Point(self.from_cell_middle_x, self.from_cell_middle_y)
self.to_cell_point = Point(self.to_cell_middle_x, self.to_cell_middle_y)
fill_color = "red"
if undo:
fill_color = "grey"
self._win.draw_line(Line(self.from_cell_point, self.to_cell_point), fill_color)
class Maze:
def __init__(self, x1, y1, num_rows, num_cols, cell_size_x, cell_size_y, win=None, seed=None):
self._x1 = x1
self._y1 = y1
self._num_rows = num_rows
self._num_cols = num_cols
self._cell_size_x = cell_size_x
self._cell_size_y = cell_size_y
self._win = win
self._cells = []
self._create_cells()
self._break_entrance_and_exit()
if seed != None:
random.seed(seed)
#self._break_walls_r(num_cols - 1, num_rows - 1)
self._break_walls_r(0, 0)
self._reset_cells_visited()
def _create_cells(self):
for a in range(self._num_cols):
self._cells.append([])
cell_to_append_x1 = self._x1 + self._cell_size_x * a
cell_to_append_x2 = self._x1 + self._cell_size_x * (a + 1)
for b in range (self._num_rows):
cell_to_append_y1 = self._y1 + self._cell_size_y * b
cell_to_append_y2 = self._y1 + self._cell_size_y * (b + 1)
cell_to_append = Cell(cell_to_append_x1, cell_to_append_y1, cell_to_append_x2, cell_to_append_y2, self._win)
self._cells[a].append(cell_to_append)
self._draw_cell(a, b)
def _draw_cell(self, i, j):
self._cells[i][j].draw()
self._animate()
def _animate(self):
if self._win is None:
return
self._win.redraw()
time.sleep(0.00035)
def _break_entrance_and_exit(self):
self._cells[0][0].has_top_wall = False
self._draw_cell(0,0)
self._cells[self._num_cols-1][self._num_rows-1].has_bottom_wall = False
self._draw_cell(self._num_cols-1, self._num_rows-1)
def _break_walls_r(self, i, j):
self._cells[i][j].visited = True
#print(f"cell i: {i}, cell j: {j}")
while True:
cells_to_visit = []
to_right_x = i + 1
to_bottom_y = j + 1
to_left_x = i - 1
to_top_y = j - 1
if to_right_x != self._num_cols:
if self._cells[to_right_x][j].visited == False:
cells_to_visit.append((self._cells[to_right_x][j], "right"))
#print("ok to go right")
if to_bottom_y != self._num_rows:
if self._cells[i][to_bottom_y].visited == False:
cells_to_visit.append((self._cells[i][to_bottom_y], "bottom"))
#print("ok to go bottom")
if to_left_x >= 0:
if self._cells[to_left_x][j].visited == False:
cells_to_visit.append((self._cells[to_left_x][j], "left"))
#print("ok to go left")
if to_top_y >= 0:
if self._cells[i][to_top_y].visited == False:
cells_to_visit.append((self._cells[i][to_top_y], "top"))
#print("ok to go top")
if len(cells_to_visit) == 0:
self._draw_cell(i, j)
return
next_move = random.choice(range(0, len(cells_to_visit)))
#print(f"next move: {next_move}")
next_cell = cells_to_visit[next_move][0]
direction = cells_to_visit[next_move][1]
if direction == "right":
#print("moving right")
self._cells[i][j].has_right_wall = False
next_cell.has_left_wall = False
self._break_walls_r(i + 1, j)
if direction == "bottom":
#print("moving bottom")
self._cells[i][j].has_bottom_wall = False
next_cell.has_top_wall = False
self._break_walls_r(i, j + 1)
if direction == "left":
#print("moving left")
self._cells[i][j].has_left_wall = False
next_cell.has_right_wall = False
self._break_walls_r(i - 1, j)
if direction == "top":
#print("moving top")
self._cells[i][j].has_top_wall = False
next_cell.has_bottom_wall = False
self._break_walls_r(i, j - 1)
def _reset_cells_visited(self):
for cells_list in self._cells:
for cell in cells_list:
cell.visited = False
def solve(self):
if self._win is None:
return
start_l1_x1 = self._x1 + self._cell_size_x / 4
start_l1_x2 = self._x1 + 3 * self._cell_size_x / 4
start_l1_y1 = self._x1 + self._cell_size_y /4
start_l1_y2 = self._x1 + 3 * self._cell_size_y /4
self._win.draw_line(Line(Point(start_l1_x1, start_l1_y1), Point(start_l1_x2, start_l1_y2)))
self._win.draw_line(Line(Point(start_l1_x1, start_l1_y2), Point(start_l1_x2, start_l1_y1)))
end_l1_x1 = self._win._ww - start_l1_x2
end_l1_x2 = self._win._ww - start_l1_x1
end_l1_y1 = self._win._wh - start_l1_y2
end_l1_y2 = self._win._wh - start_l1_y1
self._win.draw_line(Line(Point(end_l1_x1, end_l1_y1), Point(end_l1_x2, end_l1_y2)))
self._win.draw_line(Line(Point(end_l1_x1, end_l1_y2), Point(end_l1_x2, end_l1_y1)))
return self._solve_r(0, 0)
def _solve_r(self, i, j):
#print(f"-- inside of cell i: {i}, cell j: {j}")
current_cell = self._cells[i][j]
self._animate()
current_cell.visited = True
if i == self._num_cols - 1 and j == self._num_rows - 1:
print("---- last cell reached")
return True
cells_to_visit = []
trace_back = []
to_right_x = i + 1
to_bottom_y = j + 1
to_left_x = i - 1
to_top_y = j - 1
if to_right_x != self._num_cols:
if not current_cell.has_right_wall and not self._cells[to_right_x][j].dead_end:
trace_back.append((to_right_x, j))
#print(f"---- added right cell to backup plan: {trace_back}")
if self._cells[to_right_x][j].visited == False:
cells_to_visit.append((to_right_x, j))
if to_bottom_y != self._num_rows:
if not current_cell.has_bottom_wall and not self._cells[i][to_bottom_y].dead_end:
trace_back.append((i, to_bottom_y))
#print(f"---- added bottom cell to backup plan: {trace_back}")
if self._cells[i][to_bottom_y].visited == False:
cells_to_visit.append((i, to_bottom_y))
if to_left_x >= 0:
if not current_cell.has_left_wall and not self._cells[to_left_x][j].dead_end:
trace_back.append((to_left_x, j))
#print(f"---- added left cell to backup plan: {trace_back}")
if self._cells[to_left_x][j].visited == False:
cells_to_visit.append((to_left_x, j))
if to_top_y >= 0:
if not current_cell.has_top_wall and not self._cells[i][to_top_y].dead_end:
trace_back.append((i, to_top_y))
#print(f"---- added bottom cell to backup plan: {trace_back}")
if self._cells[i][to_top_y].visited == False:
cells_to_visit.append((i, to_top_y))
if len(cells_to_visit) > 0:
next_cell = self._cells[cells_to_visit[0][0]][cells_to_visit[0][1]]
current_cell.draw_move(next_cell)
self._solve_r(cells_to_visit[0][0], cells_to_visit[0][1])
else:
#print("---- dead cell reached, turning back")
current_cell.dead_end = True
return_to_cell = self._cells[trace_back[0][0]][trace_back[0][1]]
current_cell.draw_move(return_to_cell, True)
self._solve_r(trace_back[0][0], trace_back[0][1])
return False
def solve_o(self):
if self._win is None:
return
start_l1_x1 = self._x1 + self._cell_size_x / 4
start_l1_x2 = self._x1 + 3 * self._cell_size_x / 4
start_l1_y1 = self._x1 + self._cell_size_y /4
start_l1_y2 = self._x1 + 3 * self._cell_size_y /4
self._win.draw_line(Line(Point(start_l1_x1, start_l1_y1), Point(start_l1_x2, start_l1_y2)))
self._win.draw_line(Line(Point(start_l1_x1, start_l1_y2), Point(start_l1_x2, start_l1_y1)))
end_l1_x1 = self._win._ww - start_l1_x2
end_l1_x2 = self._win._ww - start_l1_x1
end_l1_y1 = self._win._wh - start_l1_y2
end_l1_y2 = self._win._wh - start_l1_y1
self._win.draw_line(Line(Point(end_l1_x1, end_l1_y1), Point(end_l1_x2, end_l1_y2)))
self._win.draw_line(Line(Point(end_l1_x1, end_l1_y2), Point(end_l1_x2, end_l1_y1)))
gimmick_lines = []
start_l2_x1 = self._x1 + self._cell_size_x / 2
end_l2_y2 = self._x1 / 2
line2 = Line(Point(start_l2_x1, start_l2_x1), Point(start_l2_x1, end_l2_y2))
gimmick_lines.append(line2)
start_l3_x2 = self._win._ww - self._x1 / 2
line3 = Line(Point(start_l2_x1, end_l2_y2), Point(start_l3_x2, end_l2_y2))
gimmick_lines.append(line3)
end_l4_y2 = self._win._wh - end_l2_y2
line4 = Line(Point(start_l3_x2, end_l2_y2), Point(start_l3_x2, end_l4_y2))
gimmick_lines.append(line4)
end_l5_x2 = self._win._ww - start_l2_x1
line5 = Line(Point(start_l3_x2, end_l4_y2), Point(end_l5_x2, end_l4_y2))
gimmick_lines.append(line5)
end_l6_y2 = self._win._wh - self._x1 - self._cell_size_y / 2
line6 = Line(Point(end_l5_x2, end_l4_y2), Point(end_l5_x2, end_l6_y2))
gimmick_lines.append(line6)
for line in gimmick_lines:
self._win.draw_line(line, "green")
window_width = 800
window_height = 600
win = Window(window_width, window_height)
maze_num_cols = 32
maze_num_rows = 24
maze_window_border = 20
cell_width = (window_width - 2 * maze_window_border) / maze_num_cols
cell_height = (window_height - 2 * maze_window_border) / maze_num_rows
#seed = 0
the_maze = Maze(maze_window_border, maze_window_border, maze_num_rows, maze_num_cols, cell_width, cell_height, win)
the_maze.solve()
#the_maze.solve_o()
win.wait_for_close()