-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.py
350 lines (294 loc) · 12.7 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import time
import random
from window import Window
from cell import Cell
class Maze:
__win: Window = None
def __init__(
self,
x,
y,
num_rows,
num_cols,
algorithm = 'iterative',
slow = False,
seed = None
):
self.__x: int = x
self.__y: int = y
self.__num_rows: int = num_rows
self.__num_cols: int = num_cols
self.__seed: int = seed if seed is not None else random.seed(seed)
self.__algorithm: str = algorithm
self.__slow: bool = slow
self.__cells: list[list[Cell]]|None = None
self.__create_cells()
def get_cells(self) -> list[list[Cell]]|None:
return self.__cells
@classmethod
def set_win(cls, win) -> None:
cls.__win = win
def __create_cells(self) -> None:
self.__cells = []
for i in range(self.__num_cols):
col = []
for j in range(self.__num_rows):
x = self.__x + (i * Cell.get_wall_length())
y = self.__y + (j * Cell.get_wall_length())
col.append(Cell(x, y))
self.__cells.append(col)
for i in range(len(self.__cells)):
for j in range(len(self.__cells[0])):
self.__draw_cell(i, j)
self.__break_entrance_and_exit()
if self.__algorithm == 'iterative':
self.__break_walls()
else:
self.__break_walls_r()
self.__reset_cells_visited()
def __break_entrance_and_exit(self) -> None:
"""
Creates entrance and exit. Entrance is always top left and exit bottom right.
:return:
"""
self.__cells[0][0].has_left_wall = False
self.__draw_cell(0, 0)
self.__cells[len(self.__cells) - 1][len(self.__cells[0]) - 1].has_right_wall = False
self.__draw_cell(len(self.__cells) - 1, len(self.__cells[0]) - 1)
def __break_walls(self, start_i=0, start_j=0) -> None:
"""
Breaks walls and creates a way through the maze using iterative approach
:param start_i:
:param start_j:
:return:
"""
visited = set()
stack = [(start_i, start_j)]
while stack:
i, j = stack[-1] # Peek at the last cell
if (i, j) not in visited:
visited.add((i, j))
self.__draw_cell(i, j)
# List to store possible directions
possible_directions = []
# Check all adjacent cells (up, right, down, left)
if j > 0 and (i, j - 1) not in visited:
possible_directions.append((i, j - 1))
if i < self.__num_cols - 1 and (i + 1, j) not in visited:
possible_directions.append((i + 1, j))
if j < self.__num_rows - 1 and (i, j + 1) not in visited:
possible_directions.append((i, j + 1))
if i > 0 and (i - 1, j) not in visited:
possible_directions.append((i - 1, j))
if possible_directions:
# Choose random direction and break walls
next_i, next_j = random.choice(possible_directions)
# Break walls between current and next cell
if next_i < i: # Moving left
self.__cells[i][j].has_left_wall = False
self.__cells[next_i][next_j].has_right_wall = False
elif next_i > i: # Moving right
self.__cells[i][j].has_right_wall = False
self.__cells[next_i][next_j].has_left_wall = False
elif next_j < j: # Moving up
self.__cells[i][j].has_top_wall = False
self.__cells[next_i][next_j].has_bottom_wall = False
else: # Moving down
self.__cells[i][j].has_bottom_wall = False
self.__cells[next_i][next_j].has_top_wall = False
stack.append((next_i, next_j))
else:
stack.pop() # Backtrack if no unvisited neighbors
def __break_walls_r(self, i=0, j=0, visited=None) -> None:
"""
Breaks walls and creates a way through the maze using a recursive approach
:param i:
:param j:
:param visited:
:return:
"""
# Initialize visited set on first call
if visited is None:
visited = set()
# Mark current cell as visited using tuple of coordinates
visited.add((i, j))
while True:
# List to store possible directions as (i, j) coordinates
possible_directions = []
# Check all adjacent cells (up, right, down, left)
# Up
if j > 0 and (i, j - 1) not in visited:
possible_directions.append((i, j - 1))
# Right
if i < self.__num_cols - 1 and (i + 1, j) not in visited:
possible_directions.append((i + 1, j))
# Down
if j < self.__num_rows - 1 and (i, j + 1) not in visited:
possible_directions.append((i, j + 1))
# Left
if i > 0 and (i - 1, j) not in visited:
possible_directions.append((i - 1, j))
# If no unvisited neighbors, we're done with this cell
if len(possible_directions) == 0:
self.__draw_cell(i, j)
return
# Choose a random direction
next_cell = random.choice(possible_directions)
next_i, next_j = next_cell
# Break down walls between current cell and chosen cell
if next_i < i: # Moving left
self.__cells[i][j].has_left_wall = False
self.__cells[next_i][next_j].has_right_wall = False
elif next_i > i: # Moving right
self.__cells[i][j].has_right_wall = False
self.__cells[next_i][next_j].has_left_wall = False
elif next_j < j: # Moving up
self.__cells[i][j].has_top_wall = False
self.__cells[next_i][next_j].has_bottom_wall = False
else: # Moving down
self.__cells[i][j].has_bottom_wall = False
self.__cells[next_i][next_j].has_top_wall = False
# Draw current cell after breaking walls
self.__draw_cell(i, j)
# Recursively visit the next cell
self.__break_walls_r(next_i, next_j, visited)
def __reset_cells_visited(self) -> None:
for coll in self.__cells:
for cell in coll:
cell.reset_visited()
def __draw_cell(self, i:int, j:int) -> None:
x = self.__x + (i * Cell.get_wall_length())
y = self.__y + (j * Cell.get_wall_length())
self.__cells[i][j].draw()
self.__animate()
def __animate(self) -> None:
self.__win.redraw()
if self.__slow:
time.sleep(0.05)
def solve(self) -> bool:
if self.__algorithm == 'iterative':
return self.__solve(0, 0)
else:
return self.__solve_r(0, 0)
def __solve(self, start_i: int, start_j: int) -> bool:
"""
Solves the maze using iterative approach
"""
stack = [(start_i, start_j, [])] # (i, j, path_to_here)
visited = set()
solution_path = None
while stack:
i, j, path = stack.pop()
current_cell = self.__cells[i][j]
if (i, j) not in visited:
self.__animate()
current_cell.set_visited()
visited.add((i, j))
# Draw red line from previous cell (will be greyed out if not part of solution)
if path:
prev_i, prev_j = path[-1]
self.__cells[prev_i][prev_j].draw_move(current_cell)
# If we reached the end
if i == self.__num_cols - 1 and j == self.__num_rows - 1:
solution_path = path + [(i, j)]
break
# Store possible moves
moves = []
# Right
if (i < self.__num_cols - 1 and
not current_cell.has_right_wall and
not self.__cells[i + 1][j].is_visited()):
moves.append((i + 1, j))
# Down
if (j < self.__num_rows - 1 and
not current_cell.has_bottom_wall and
not self.__cells[i][j + 1].is_visited()):
moves.append((i, j + 1))
# Left
if (i > 0 and
not current_cell.has_left_wall and
not self.__cells[i - 1][j].is_visited()):
moves.append((i - 1, j))
# Up
if (j > 0 and
not current_cell.has_top_wall and
not self.__cells[i][j - 1].is_visited()):
moves.append((i, j - 1))
if moves:
# Add moves to stack with updated path
new_path = path + [(i, j)]
for next_i, next_j in moves:
stack.append((next_i, next_j, new_path))
else:
# Dead end - grey out the path back to last junction
if path:
prev_i, prev_j = path[-1]
self.__cells[prev_i][prev_j].draw_move(current_cell, True)
else:
# If we've visited this cell before, grey out the move to it
if path:
prev_i, prev_j = path[-1]
self.__cells[prev_i][prev_j].draw_move(current_cell, True)
# Grey out all explored paths
visited.clear() # Reset visited set for final path drawing
for i in range(self.__num_cols):
for j in range(self.__num_rows):
cell = self.__cells[i][j]
# Grey out connections to neighboring cells
if i < self.__num_cols - 1 and not cell.has_right_wall:
cell.draw_move(self.__cells[i + 1][j], True)
if j < self.__num_rows - 1 and not cell.has_bottom_wall:
cell.draw_move(self.__cells[i][j + 1], True)
# If solution was found, draw the final path in red
if solution_path:
for idx in range(len(solution_path) - 1):
curr_i, curr_j = solution_path[idx]
next_i, next_j = solution_path[idx + 1]
self.__cells[curr_i][curr_j].draw_move(self.__cells[next_i][next_j])
return True
return False
def __solve_r(self, i: int, j: int) -> bool:
"""
Solves the maze using recursive approach
"""
self.__animate()
# Mark current cell as visited
self.__cells[i][j].set_visited()
# Did we reach the exit?
if i == self.__num_cols - 1 and j == self.__num_rows - 1:
return True
# Try each direction
# Try moving right
if (i < self.__num_cols - 1 and # cell exists
not self.__cells[i][j].has_right_wall and # no wall blocking
not self.__cells[i + 1][j].is_visited()): # not visited
self.__cells[i][j].draw_move(self.__cells[i + 1][j])
if self.__solve_r(i + 1, j):
return True
self.__cells[i][j].draw_move(self.__cells[i + 1][j], True) # undo
# Try moving down
if (j < self.__num_rows - 1 and
not self.__cells[i][j].has_bottom_wall and
not self.__cells[i][j + 1].is_visited()):
self.__cells[i][j].draw_move(self.__cells[i][j + 1])
if self.__solve_r(i, j + 1):
return True
self.__cells[i][j].draw_move(self.__cells[i][j + 1], True) # undo
# Try moving left
if (i > 0 and
not self.__cells[i][j].has_left_wall and
not self.__cells[i - 1][j].is_visited()):
self.__cells[i][j].draw_move(self.__cells[i - 1][j])
if self.__solve_r(i - 1, j):
return True
self.__cells[i][j].draw_move(self.__cells[i - 1][j], True) # undo
# Try moving up
if (j > 0 and
not self.__cells[i][j].has_top_wall and
not self.__cells[i][j - 1].is_visited()):
self.__cells[i][j].draw_move(self.__cells[i][j - 1])
if self.__solve_r(i, j - 1):
return True
self.__cells[i][j].draw_move(self.__cells[i][j - 1], True) # undo
# If no direction worked, this path is a dead end
return False