This repository has been archived by the owner on Dec 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
labyrinth3_4.py
292 lines (231 loc) · 9.69 KB
/
labyrinth3_4.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
#!/usr/bin/env python3
import sys
import random
def input_maze_size():
"""
Inputs and returns two integers from the user.
They represent the width and the height of the labyrinth, respectively.
"""
print("Enter the size of your labyrinth:")
width = int(input("Width: "))
height = int(input("Height: "))
depth = int(input("Depth: "))
return width, height, depth
def generate_grid(width, height, depth):
"""
Generates the initial grid for the randomized Kruskal's maze generation algorithm.
The grid returned is a two-dimensional list of integers.
Its first dimension is of size the height specified and its second of size the width specified.
-1 represents walls, numbers from 0 to width * height represent open cells.
"""
grid = []
grid.append([[-1] * (width * 2 + 1) for y in range(height * 2 + 1)])
for z in range(depth):
line = []
line.append([-1] * (width * 2 + 1))
for y in range(height):
column = []
column.append(-1)
for x in range(width):
column.append(x + (y * width + z) * depth)
column.append(-1)
line.append(column)
line.append([-1] * (width * 2 + 1))
grid.append(line)
grid.append([[-1] * (width * 2 + 1) for y in range(height * 2 + 1)])
return grid
def is_maze_done(grid):
"""
Returns True if and only if the specified grid only contains -1 and 0.
This means the randomized Kruskal's maze generation algorithm is completed.
"""
for line in grid:
for column in line:
for cell in column:
if cell != -1 and cell != 0:
return False
return True
def get_adjacent_cells(grid, x, y, z):
"""
Returns all the adjacent valid cells to the one with the specified coordinates.
Assumes -1 represents walls and anything else represents open cells.
"""
adjacent = []
for dx, dy, dz in [(-1, 0, 0), (+1, 0, 0), (0, -1, 0), (0, +1, 0), (0, 0, -1), (0, 0, +1)]:
if z + dz < 0 or z + dz >= len(grid) or y + dy < 0 or y + dy >= len(grid[z + dz]) or x + dx < 0 or x + dx >= len(grid[z + dz][y + dy]):
continue
if grid[z + dz][y + dy][x + dx] == -1:
continue
adjacent.append((x + dx, y + dy, z + dz))
return adjacent
def propagate(grid, x, y, z):
"""
If the cell at the specified coordinates is a wall next to an open cell, replaces it with an open cell of the same value.
Then, merges all the neighboring sets into the smallest one recursively.
"""
if z < 0 or z >= len(grid) or y < 0 or y >= len(grid[z]) or x < 0 or x >= len(grid[z][y]):
return
minimum = None
adjacent = get_adjacent_cells(grid, x, y, z)
for cell in adjacent:
if minimum is None or grid[cell[2]][cell[1]][cell[0]] < minimum:
minimum = grid[cell[2]][cell[1]][cell[0]]
if minimum is None:
return
grid[z][y][x] = minimum
for cell in adjacent:
if grid[cell[2]][cell[1]][cell[0]] > minimum:
propagate(grid, cell[0], cell[1], cell[2])
def generate_maze(width, height, depth):
"""
Generates a maze of the specified size using a derived and randomized form of Kruskal's maze generation algorithm.
The grid returned is a two-dimensional list of integers.
Its first dimension is of size the height specified and its second of size the width specified.
-1 represents walls, spaces (" ") represent open cells.
"""
grid = generate_grid(width, height, depth)
while not is_maze_done(grid):
orientation = random.randint(0, 2)
if orientation == 0:
# Vertical
x, y, z = random.randrange(1, width) * 2, random.randrange(height) * 2 + 1, random.randrange(depth) * 2 + 1
if grid[z][y][x] != -1 or grid[z][y][x - 1] == grid[z][y][x + 1]:
continue
elif orientation == 1:
# Horizontal
x, y, z = random.randrange(width) * 2 + 1, random.randrange(1, height) * 2, random.randrange(depth) * 2 + 1
if grid[z][y][x] != -1 or grid[z][y - 1][x] == grid[z][y + 1][x]:
continue
elif orientation == 2:
# Upsidedown
x, y, z = random.randrange(width) * 2 + 1, random.randrange(height) * 2 + 1, random.randrange(1, depth) * 2
if grid[z][y][x] != -1 or grid[z - 1][y][x] == grid[z + 1][y][x]:
continue
if grid[z][y][x] != -1:
continue
propagate(grid, x, y, z)
for z in range(1, depth * 2):
for y in range(1, height * 2):
for x in range(1, width * 2):
if grid[z][y][x] == 0:
grid[z][y][x] = " "
return grid
def display_maze(grid):
"""
Displays the specified maze.
Cells with -1 are replaced with stars ("*").
Numbers above 9 are replaced with letters, or plus ("+") if they are above 61.
"""
first = True
characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+"
for z, line in enumerate(grid):
if not first:
print()
print("-" * (len(line[0]) * 2 - 1))
else:
first = False
print()
print("Level", z)
for y, column in enumerate(line):
for x, cell in enumerate(column):
if cell == -1:
print("*", end=" ")
else:
if type(cell) is int:
cell = characters[min(cell, len(characters) - 1)]
elif cell == " ":
if z > 0 and z < len(grid) - 1 and grid[z - 1][y][x] != -1 and grid[z + 1][y][x] != -1:
cell = "⇕"
elif z > 0 and grid[z - 1][y][x] != -1:
cell = "⇑"
elif z < len(grid) - 1 and grid[z + 1][y][x] != -1:
cell = "⇓"
print(cell, end=" ")
print()
print()
def input_points(grid):
"""
Inputs and returns four integers from the user.
The first two represent the x and y coordinates of the labyrinth's entrance respectively.
The last two represent the x and y coordinates of the labyrinth's exit respectively.
"""
valid = False
while not valid:
print("Enter the coordinates of the starting point (A):")
xA = int(input("X: "))
yA = int(input("Y: "))
zA = int(input("Z: "))
valid = len(get_adjacent_cells(grid, xA, yA, zA)) == 1
if not valid:
print("The starting point (A) must be an external wall adjacent to an empty cell.")
valid = False
while not valid:
print("Enter the coordinates of the ending point (B):")
xB = int(input("X: "))
yB = int(input("Y: "))
zB = int(input("Z: "))
valid = len(get_adjacent_cells(grid, xB, yB, zB)) == 1 and (xB != xA or yB != yA or zB != zA)
if not valid:
print("The ending point (B) must be an external wall adjacent to an empty cell and different from the starting point.")
return xA, yA, zA, xB, yB, zB
def get_path(grid, distances, x, y, z):
"""
Traces back the path from the specified coordinates to the point of distance 0.
Returns the grid specified, replacing spaces (" ") with dots (".") along the path.
WARNING: The distances should only describe one path of decrementing integers from the specified coordinates.
Otherwise, the behavior is undefined and the function may return None.
"""
solution = [[column.copy() for column in line] for line in grid]
d = distances[z][y][x]
while d > 0:
for cell in get_adjacent_cells(distances, x, y, z):
if distances[cell[2]][cell[1]][cell[0]] == d - 1:
x, y, z = cell
d -= 1
break
else:
return None
if solution[cell[2]][cell[1]][cell[0]] == " ":
solution[cell[2]][cell[1]][cell[0]] = "."
return solution
def solve_maze(grid, xA, yA, zA, xB, yB, zB):
"""
Finds the optimal solution of the specified maze grid, where the starting point is (xA, yA) and the exit has coordinates (xB, yB).
Returns the number of cells explored by the algorithm, the length of the shortest path, as well as the path itself.
Assumes -1 represents walls and anything else represents open cells.
The returned grid contains dots (".") along the path.
"""
distances = [[[-1] * len(column) for column in line] for line in grid]
explored = 0
pending = [(xA, yA, zA, 0)]
while pending:
x, y, z, d = pending.pop(0)
if distances[z][y][x] != -1 and distances[z][y][x] <= d:
continue
distances[z][y][x] = d
explored += 1
for cell in get_adjacent_cells(grid, x, y, z):
pending.append((cell[0], cell[1], cell[2], d + 1))
return explored, distances[zB][yB][xB], get_path(grid, distances, xB, yB, zB)
def main():
"""
Executes the labyrinth program.
"""
sys.setrecursionlimit(100000)
width, height, depth = input_maze_size()
grid = generate_maze(width, height, depth)
print("Here is the random labyrinth:")
display_maze(grid)
xA, yA, zA, xB, yB, zB = input_points(grid)
grid[zA][yA][xA] = "A"
grid[zB][yB][xB] = "B"
explored, length, solution = solve_maze(grid, xA, yA, zA, xB, yB, zB)
if solution is not None:
print("Here is the shortest path:")
display_maze(solution)
print("Number of explored cells to find the path:", explored)
print("Length of the shortest path:", length)
else:
print("No solution found to the maze.")
if __name__ == "__main__":
main()