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_5b.py
232 lines (173 loc) · 6.35 KB
/
labyrinth3_5b.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
#!/usr/bin/env python3
import sys
import random
def input_maze_size():
print("Enter the size of your labyrinth:")
width = int(input("Width: "))
height = int(input("Height: "))
return width, height
def generate_grid(width, height):
grid = []
grid.append([-1] * (width * 2 + 1))
for y in range(height):
line = []
line.append(-1)
for x in range(width):
line.append(x + y * width)
line.append(-1)
grid.append(line)
grid.append([-1] * (width * 2 + 1))
return grid
def is_maze_done(grid, allow_walls):
for y in range(1, len(grid) - 1):
for x in range(1, len(grid[y]) - 1):
if grid[y][x] != 0 and (not allow_walls or grid[y][x] != -1):
return False
return True
def get_adjacent_cells(grid, x, y):
adjacent = []
for dx, dy in [(-1, 0), (+1, 0), (0, -1), (0, +1)]:
if y + dy < 0 or y + dy >= len(grid) or x + dx < 0 or x + dx >= len(grid[y + dy]) or grid[y + dy][x + dx] == -1:
continue
adjacent.append((x + dx, y + dy))
return adjacent
def propagate(grid, x, y):
if y < 0 or y >= len(grid) or x < 0 or x >= len(grid[y]):
return
minimum = None
for cell in get_adjacent_cells(grid, x, y):
if minimum is None or grid[cell[1]][cell[0]] < minimum:
minimum = grid[cell[1]][cell[0]]
if minimum is None:
return
grid[y][x] = minimum
for cell in get_adjacent_cells(grid, x, y):
if grid[cell[1]][cell[0]] > minimum:
propagate(grid, cell[0], cell[1])
def generate_cycles(grid, cycles):
i = 0
while i < cycles:
if is_maze_done(grid, False):
break
if random.getrandbits(1):
# Horizontal
x = random.randrange(len(grid[0]) // 2) * 2 + 1
y = random.randrange(1, len(grid) // 2) * 2
else:
# Vertical
x = random.randrange(1, len(grid[0]) // 2) * 2
y = random.randrange(len(grid) // 2) * 2 + 1
if grid[y][x] != -1:
continue
propagate(grid, x, y)
i += 1
return grid
def generate_maze(width, height, cycles):
grid = generate_grid(width, height)
while not is_maze_done(grid, True):
if random.getrandbits(1):
# Horizontal
x = random.randrange(width) * 2 + 1
y = random.randrange(1, height) * 2
if grid[y][x] != -1 or grid[y - 1][x] == grid[y + 1][x]:
continue
else:
# Vertical
x = random.randrange(1, width) * 2
y = random.randrange(height) * 2 + 1
if grid[y][x] != -1 or grid[y][x - 1] == grid[y][x + 1]:
continue
propagate(grid, x, y)
grid = generate_cycles(grid, cycles)
for y in range(1, height * 2):
for x in range(1, width * 2):
if grid[y][x] == 0:
grid[y][x] = " "
return grid
def display_maze(grid):
characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+"
for line in grid:
for cell in line:
if cell == -1:
print("*", end=" ")
else:
if type(cell) is int:
cell = characters[min(cell, len(characters) - 1)]
print(cell, end=" ")
print()
def input_points(grid):
valid = False
while not valid:
print("Enter the coordinates of the starting point (A):")
xA = int(input("X: "))
yA = int(input("Y: "))
valid = len(get_adjacent_cells(grid, xA, yA)) == 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: "))
valid = len(get_adjacent_cells(grid, xB, yB)) == 1 and (xB != xA or yB != y1)
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, xB, yB
def get_path(grid, distances, x, y):
solution = [line.copy() for line in grid]
d = distances[y][x]
while d > 0:
for cell in get_adjacent_cells(distances, x, y):
if distances[cell[1]][cell[0]] == d - 1:
x, y = cell
d -= 1
break
else:
return None
if solution[cell[1]][cell[0]] == " ":
solution[cell[1]][cell[0]] = "."
return solution
def solve_maze(grid, xA, yA, xB, yB):
distances = [[-1] * len(line) for line in grid]
explored = 0
x, y, direction, distance = xA, yA, 0, 0
while direction in [0, 1, 2, 3]:
if distances[y][x] != -1:
if distances[y][x] <= distance:
distance = distances[y][x]
else:
explored += 1
distances[y][x] = distance
dx, dy, wx, wy = [(0, -1, +1, 0), (-1, 0, 0, -1), (0, +1, -1, 0), (+1, 0, 0, +1)][direction]
if y + dy < 0 or y + dy >= len(grid) or x + dx < 0 or x + dx >= len(grid[y + dy]) or grid[y + dy][x + dx] == -1:
direction = (direction + 1) % 4
continue
x, y = x + dx, y + dy
distance += 1
if x == xB and y == yB:
distances[yB][xB] = distance
direction = -1
break
if y + wy < 0 or y + wy >= len(grid) or x + wx < 0 or x + wx >= len(grid[y + wy]) or grid[y + wy][x + wx] != -1:
direction = (direction + 3) % 4
return explored, distances[yB][xB], get_path(grid, distances, xB, yB)
def main():
sys.setrecursionlimit(100000)
width, height = input_maze_size()
cycles = int(input("How many cycles do you want in the maze approximately? "))
grid = generate_maze(width, height, cycles)
print("Here is the random labyrinth:")
display_maze(grid)
xA, yA, xB, yB = input_points(grid)
grid[yA][xA] = "A"
grid[yB][xB] = "B"
explored, length, solution = solve_maze(grid, xA, yA, xB, yB)
if solution is not None:
print("Here is the path:")
display_maze(solution)
print("Number of explored cells to find the path:", explored)
print("Length of the path:", length)
else:
print("No solution found to the maze.")
if __name__ == "__main__":
main()