-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze_generator.py
135 lines (101 loc) · 3.7 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
##This file generates a random block maze to be solved
import random
print('Welcome to the maze generator!')
size = int(input("Please enter maze size: "))
#All cells are walls.
maze_list = []
for x in range(size):
inner_list = []
for y in range(size):
inner_list.append(1)
maze_list.append(inner_list)
#Randomly choose a cell B and mark it as free.
B = [random.randint(0,size-1), random.randint(0,size-1)]
print(f"Cell B: {B}")
maze_list[B[0]][B[1]] = 2
start = B
for x in range(size):
print(maze_list[x])
#Add that cell's neighbors to the wall list.
wall_list = []
for coordinates in [[B[0]+1, B[1]],[B[0], B[1]+1],[B[0]-1, B[1]],[B[0], B[1]-1]]:
if coordinates[0] < 0 or coordinates[0] >= size or coordinates[1] <0 or coordinates[1] >= size:
continue
else:
wall_list.append(coordinates)
print(f"Wall List: {wall_list}")
#While the wall list is not empty:
#while len(wall_list)>1:
for x in range(size*3):
print(B)
# Randomly choose a wall C from the wall list
C = random.choice(wall_list)
print(f"Cell C: {C}")
while True:
if C[0] == 0 or C[1] == 0 or C[0] == size-1 or C[1] == size-1: #C on edge of maze (could be trouble)
if B[0] == 0 or B[1] == 0 or B[0] == size-1 or B[1] == size-1: #B on edge of maze (this is okay)
if (C[0]==0 and C[1]==0) or (C[0] == 0 and C[1] == size-1) or (C[0]==size-1 and C[1]==0) or (C[0]==size-1 and C[1]==size-1):
print('Corner!')
while C in wall_list:
for coordinates in wall_list:
if coordinates == C:
wall_list.remove(coordinates)
C = random.choice(wall_list)
else:
break
else: #C on edge of maze, but B is not (means A will be off maze!!!)
while C in wall_list:
for coordinates in wall_list:
if coordinates == C:
wall_list.remove(coordinates)
C = random.choice(wall_list)
else:
break
print(f"Try Again Cell C: {C}")
#The wall divides two cells, A and B.
if B[0] == C[0] and B[1] > C[1]:
A = [B[0], C[1]-1]
elif B[0] == C[0] and B[1] < C[1]:
A = [B[0], C[1]+1]
elif B[1] == C[1] and B[0] > C[0]:
A = [C[0]-1, B[1]]
elif B[1] == C[1] and B[0] < C[0]:
A = [C[0]+1, B[1]]
#trying to account for negative As
#if A[0] < 0 or A[0] > size-1 or A[1] < 0 or A[1] > size-1:
# A = C
print(f"Cell A: {A}")
# If either A or B is a wall
# Let D be whichever of A and B that is the wall
## if maze_list[A[0]][A[1]] == 1:
## D = A
## elif maze_list[B[0]][B[1]] == 1:
## D = B
## else:
## break
#print(f"Cell D: {D}")
# Make C free
maze_list[C[0]][C[1]] = 0
# Make D free
maze_list[A[0]][A[1]] = 0
for x in range(size):
print(maze_list[x])
# Make A the new B
B = A
for coordinates in [[B[0]+1, B[1]],[B[0], B[1]+1],[B[0]-1, B[1]],[B[0], B[1]-1]]:
if coordinates in wall_list:
continue
elif coordinates[0] < 0 or coordinates[0] >= size or coordinates[1] <0 or coordinates[1] >= size:
continue
else:
wall_list.append(coordinates)
while C in wall_list:
for coordinates in wall_list:
if coordinates == C:
wall_list.remove(coordinates)
print(f"Adjusted Wall List: {wall_list}")
maze_list[A[0]][A[1]]=3
maze_list[start[0]][start[1]]=2
maze = maze_list
print(f"End of Loop wall_list: {wall_list}")
print("Maze Generated!")