-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze_generator.py
343 lines (271 loc) · 13.9 KB
/
maze_generator.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
import numpy as np
from random import randrange
import math
import csv
class maze_generator:
def __init__(self):
pass
def generate_maze(self, size_x=70, size_y =100, start_coord = [0,1], finish_coord = [69,99], n_of_turns = 10, log = False):
"""
Generate a maze of size_x * size_y the maze is matrix with 0 for wall and X for floor
Params:
- size_x int for maze width (minimum 5)
- size_y int for maze hight (minimum 5)
- start_coord the coordinates for the starting position (can't be the same as finish_coord)
- finish_coord the coordinates for the finish position (can't be the same as start_coord)
- n_of_turns int how many turns must there be between start and finish (max of turns is size_x)
"""
if size_x < 5:
size_x = 5
if size_y < 5:
size_y = 5
if start_coord == finish_coord:
if start_coord[0] == 0: #if the x coord is on the 0 or size_x than the start coords are either on the left or right wall
finish_coord[0] = size_x
elif start_coord[0] == size_x:
finish_coord[0] = 0
elif start_coord[1] == 0:
finish_coord[1] = size_y
elif start_coord[1] == size_y:
finish_coord = 0
if size_x > n_of_turns:
n_of_turns = size_x
maze, path = self.base_maze(size_x=size_x, size_y=size_y, start_coord=start_coord, finish_coord=finish_coord, n_of_turns=n_of_turns)
maze, path = self.complicate_maze(size_x, size_y, maze, 15, path, mode = "closest")
if log:
print("unpacking maze")
print("---------------------")
self.print_maze(maze)
return maze, path
def base_maze(self, size_x, size_y, start_coord = [0,1], finish_coord = [0,3], n_of_turns = 2, log = False):
"""
Creates the direct path from start to finish
"""
maze = np.zeros((size_x,size_y))
maze[start_coord[0], start_coord[1]] = 3
maze[finish_coord[0], finish_coord[1]] = 4
turns_coords = []
excluded_x = []
i = 0
while i < n_of_turns:
x = randrange(1,size_x-1)
y = randrange(1,size_y-1)
if x not in excluded_x:
excluded_x.append(x)
new_coord = [x,y]
turns_coords.append(new_coord)
x,y = new_coord
maze[x,y] = 2
i+=1
turns_coords = self.sort_coords_by_closeness(turns_coords, start_coord)
if log:
print("start: ",start_coord, " end: ",finish_coord)
print("turns_coords in order: ", turns_coords)
path = [[start_coord[0],start_coord[1]]]
if i > 0:
for i in range (0,n_of_turns):
if i == 0:
maze, path_added = self.draw_path_between_coords([[start_coord[0],start_coord[1]], [turns_coords[i][0],turns_coords[i][1]]], maze)
path.extend(path_added)
if log:
print("connecting s: ", start_coord, " to ", turns_coords[i])
print("path in a ", path)
else:
maze, path_added = self.draw_path_between_coords([[turns_coords[i-1][0],turns_coords[i-1][1]], [turns_coords[i][0],turns_coords[i][1]]], maze)
path.extend(path_added)
if log:
print("connecting: ", turns_coords[i-1], " to ", turns_coords[i])
print("path in b ", path)
maze, path_added = self.draw_path_between_coords([[turns_coords[-1][0],turns_coords[-1][1]], [finish_coord[0],finish_coord[1]]], maze)
path.extend(path_added)
if log:
print("connecting: ", turns_coords[-1], " to ", finish_coord)
print("path in c ", path)
else:
maze, path_added = self.draw_path_between_coords([[start_coord[0],start_coord[1]], [finish_coord[0],finish_coord[1]]], maze)
path.extend(path_added)
if log:
print("connecting: ", start_coord, " to ", finish_coord)
print("path in d ", path)
maze[finish_coord[0],finish_coord[1]] = 4
if len(turns_coords) > 0:
for turn in turns_coords:
maze[turn[0],turn[1]] = 6
return maze, path
def complicate_maze(self, size_x, size_y, maze, n_new_points, path, mode = "random", log = False):
"""
adds new paths in the maze adding to the complexity of navigating it.
"""
original_path = path.copy()
i = 0
while i < n_new_points:
x = randrange(1,size_x-1)
y = randrange(1,size_y-1)
start_point = [x,y]
if start_point not in path:
if mode == "random":
finish_point = path[randrange(0,len(path)-1)]
maze[finish_point[0],finish_point[1]] = 5
elif mode == "closest":
ordered_path = self.sort_coords_by_closeness(original_path, start_point)
finish_point = ordered_path[0]
if log:
print("start ",start_point)
print("finish ",finish_point)
maze, path_added = self.draw_path_between_coords([[start_point[0],start_point[1]], [finish_point[0],finish_point[1]]], maze=maze)
path.extend(path_added)
maze[x,y] = 5
i+=1
return maze, path
def draw_path_between_coords(self, points = [], maze=[], path_char=1, log = False):
path = []
ordered_points = self.sort_points(points)
if log: print("sorted-points: ", ordered_points)
if ordered_points[0][0] == ordered_points[1][0]:
dir = 1
if ordered_points[0][1] > ordered_points[1][1]:
dir = -1
if log: print("DIRECT CONNECTION A-1")
else:
if log: print("DIRECT CONNECTION A")
maze, path_to_add = self.draw_horizontal_line(ordered_points[0], ordered_points[1], maze, path_char = path_char, direction=dir)
path.extend(path_to_add)
return maze, path
elif ordered_points[0][1] == ordered_points[1][1]:
dir = 1
if ordered_points[0][0] > ordered_points[1][0]:
dir = -1
if log: print("DIRECT CONNECTION B-1")
else:
if log: print("DIRECT CONNECTION B-1")
maze, path_to_add = self.draw_vertical_line(ordered_points[0], ordered_points[1], maze, path_char = path_char, direction=dir)
path.extend(path_to_add)
return maze, path
mid_point = []
direction = randrange(0,2)
if ordered_points[0][0] < ordered_points[1][0] and ordered_points[0][1] > ordered_points[1][1]:
direction = 0
elif ordered_points[0][0] < ordered_points[1][0] and ordered_points[0][1] < ordered_points[1][1]:
direction = 1
if direction == 0:
mid_point = [ordered_points[0][0], ordered_points[1][1]]
if log: print("connection with midpoint: ", mid_point, " type A-",direction)
new_coords = ordered_points[0]
dir = 1
if mid_point[0] == ordered_points[0][0] and mid_point[1] < ordered_points[0][1]:
dir = -1
if log: print("connecting inside ",new_coords," to ", mid_point)
maze, path_to_add = self.draw_horizontal_line(new_coords, mid_point, maze, direction=dir, path_char = path_char)
path.extend(path_to_add)
new_coords = [mid_point[0], mid_point[1]]
maze, path_to_add = self.draw_vertical_line(new_coords, ordered_points[1], maze, path_char = path_char)
path.extend(path_to_add)
else:
mid_point = [ordered_points[1][0], ordered_points[0][1]]
if log: print("connection with midpoint: ", mid_point, " type B-",direction)
new_coords = ordered_points[0]
if log: print("connect inner B: ", new_coords, " to ", mid_point)
maze, path_to_add = self.draw_vertical_line(new_coords, mid_point, maze, path_char = path_char)
path.extend(path_to_add)
new_coords = [mid_point[0], mid_point[1]]
maze, path_to_add = self.draw_horizontal_line(new_coords, ordered_points[1], maze, path_char = path_char)
path.extend(path_to_add)
maze[mid_point[0],mid_point[1]] = 2
return maze, path
def draw_horizontal_line(self, point1, point2, maze, path_char=1, direction=1):
#direction 1 means from going left to right, direction -1 right to left
path = []
new_coords = point1
while new_coords != point2:
new_coords[1] += direction
#print("a " ,new_coords)
if new_coords[1] >= len(maze[1])-1 or new_coords[1] < 0:
break
path.append([new_coords[0], new_coords[1]])
maze[new_coords[0],new_coords[1]] = path_char
return maze, path
def draw_vertical_line(self, point1, point2, maze, path_char=1, direction=1):
# direction 1 means from top to bottom, direction -1 is bottom to top
path = []
new_coords = point1
while new_coords != point2:
new_coords[0] += direction
if new_coords[0] >= len(maze[0])-1 or new_coords[0] < 0:
break
path.append([new_coords[0], new_coords[1]])
maze[new_coords[0],new_coords[1]] = path_char
return maze, path
def print_maze(self, maze, type_is = "int"):
print("printing maze")
# Iterate through rows and columns and print each element
i = 0
for row in maze:
print(i, end=" ")
i+=1
for element in row:
if type_is == "int":
print(int(element), end=" ") # Use end=" " to print elements in the same row with space
elif type_is == "float":
print(float(element), end=" ") # Use end=" " to print elements in the same row with space
print() # Print a newline to move to the next row
def sort_points(self, points, ordering = "descend", axis = 0):
if ordering == "ascend":
if axis == 0:
sorted_points = sorted(points, key=lambda point: point[0], reverse=True)
elif axis == 1:
sorted_points = sorted(points, key=lambda point: point[1], reverse=True)
elif ordering == "descend":
if axis == 0:
sorted_points = sorted(points, key=lambda point: point[0], reverse=False)
elif axis == 1:
sorted_points = sorted(points, key=lambda point: point[1], reverse=False)
return sorted_points
def euclidean_distance(self, coord_A = [0,0], coord_B = [1,1]):
return math.sqrt((coord_A[0] - coord_B[0])**2 + (coord_A[1] - coord_B[1])**2)
def sort_coords_by_closeness(self, coords, starting_coord):
coords.sort(key=lambda coord: self.euclidean_distance(coord, starting_coord))
return coords
def save_maze_as_csv(self, maze, filename, save_as = "int"):
try:
with open(filename, 'w', newline='') as csvfile:
csvwriter = csv.writer(csvfile)
for row in maze:
if save_as == "int":
csvwriter.writerow(map(int, row))
elif save_as == "float":
csvwriter.writerow(map(float, row))
print(f"maze saved to {filename} as CSV")
except Exception as e:
print(f"Error while saving the maze as CSV: {e}")
def load_maze_from_csv(self, filename, load_as = "int"):
try:
maze = []
with open(filename, 'r') as csvfile:
csvreader = csv.reader(csvfile)
for row in csvreader:
if load_as == "int":
row = list(map(int, row))
elif load_as == "float":
row = list(map(float, row))
maze.append(row)
maze = np.array(maze)
path, start_coord, finish_coord = self.find_non_zero_coordinates(maze)
return maze, path, start_coord, finish_coord
except FileNotFoundError:
print(f"File {filename} not found.")
except Exception as e:
print(f"Error while loading the maze from CSV: {e}")
return None, None, None, None
def find_non_zero_coordinates(self, maze, start_char = 3, finish_char = 4):
coordinates = []
start_coord = []
finish_coord = []
for i, row in enumerate(maze):
for j, value in enumerate(row):
if value != 0:
coordinates.append([i, j])
if value == start_char:
start_coord = [i,j]
elif value == finish_char:
finish_coord = [i,j]
return coordinates, start_coord, finish_coord