-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomMaze.py
61 lines (46 loc) · 1.75 KB
/
randomMaze.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
import os
import random
def generate_complex_maze(width, height, start, goal):
# Initialize maze with walls
maze = [['#' for _ in range(2 * width + 1)] for _ in range(2 * height + 1)]
# add passages
def add_passages(x, y):
stack = [(x, y)]
while stack:
x, y = stack[-1]
maze[2*y+1][2*x+1] = ' '
# Find unvisited neighbours
neighbours = []
for nx, ny in [(x-1, y), (x+1, y), (x, y-1), (x, y+1)]:
if 0 <= nx < width and 0 <= ny < height:
if maze[2*ny+1][2*nx+1] == '#':
neighbours.append((nx, ny))
if neighbours:
nx, ny = random.choice(neighbours)
stack.append((nx, ny))
# Remove wall between cells
maze[y+ny+1][x+nx+1] = ' '
else:
stack.pop()
# Start adding passages
add_passages(random.randint(0, width-1), random.randint(0, height-1))
# start and goal
maze[2*start[1]+1][2*start[0]+1] = 'A'
maze[2*goal[1]+1][2*goal[0]+1] = 'B'
return maze
def save_maze_to_file(maze, file_path):
directory = os.path.dirname(file_path)
if not os.path.exists(directory):
os.makedirs(directory)
with open(file_path, "w") as file:
for row in maze:
file.write(''.join(row) + '\n')
# maze size
maze_width, maze_height = 15, 10
maze_start, maze_goal = (0, 0), (maze_width - 1, maze_height - 1)
# final generation
maze = generate_complex_maze(maze_width, maze_height, maze_start, maze_goal)
#saving to text file
maze_path = r"MazeSolver/maze.txt"
save_maze_to_file(maze, maze_path)
print(f"Maze saved to {maze_path}")