Repository navigation
Expand file tree
/
Copy pathmaze.py
More file actions
223 lines (168 loc) · 4.92 KB
/
Copy pathmaze.py
File metadata and controls
223 lines (168 loc) · 4.92 KB
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
# Maze generator
import sys
sys.path.append('pythonami_lib:')
import random
WALL = 0
FLOOR = 1
GRAVE = 2
PORTAL = 3
MAGIC_TORCH = 4
START = 5
MAX_GRAVES = 5
MAX_PORTALS = 2
MAX_MAGIC_TORCH = MAX_PORTALS + 1
def make_row(width):
row = []
x = 0
while x < width:
row.append(WALL)
x = x + 1
return row
def make_maze_array(width, height):
maze = []
y = 0
while y < height:
maze.append(make_row(width))
y = y + 1
return maze
def generate_maze(width, height):
maze = make_maze_array(width, height)
# Start at (1, 1).
maze[1][1] = FLOOR
# Explicit stacks instead of recursive function calls.
stack_x = [1]
stack_y = [1]
while len(stack_x) > 0:
x = stack_x[len(stack_x) - 1]
y = stack_y[len(stack_y) - 1]
# Maximum four possible neighbors.
nx = []
ny = []
# North
if y > 2:
if maze[y - 2][x] == WALL:
nx.append(x)
ny.append(y - 2)
# East
if x + 2 < width - 1:
if maze[y][x + 2] == WALL:
nx.append(x + 2)
ny.append(y)
# South
if y + 2 < height - 1:
if maze[y + 2][x] == WALL:
nx.append(x)
ny.append(y + 2)
# West
if x > 2:
if maze[y][x - 2] == WALL:
nx.append(x - 2)
ny.append(y)
count = len(nx)
if count == 0:
stack_x.pop()
stack_y.pop()
else:
i = random.randint(0, count - 1)
new_x = nx[i]
new_y = ny[i]
# Remove wall between the cells.
wall_x = (x + new_x) // 2
wall_y = (y + new_y) // 2
maze[wall_y][wall_x] = FLOOR
maze[new_y][new_x] = FLOOR
stack_x.append(new_x)
stack_y.append(new_y)
return maze
def generate_graves_and_portals(maze):
magic_torch_placed = 0
width = len(maze[0])
height = len(maze)
graves_placed = 0
portals_placed = 0
while graves_placed < MAX_GRAVES:
if magic_torch_placed < MAX_MAGIC_TORCH:
x = random.randint(1, width - 2)
y = random.randint(1, height - 2)
if maze[y][x] == FLOOR:
maze[y][x] = MAGIC_TORCH
magic_torch_placed += 1
x = random.randint(1, width - 2)
y = random.randint(1, height - 2)
if maze[y][x] == FLOOR:
maze[y][x] = GRAVE
graves_placed += 1
while portals_placed < MAX_PORTALS:
x = random.randint(1, width - 2)
y = random.randint(1, height - 2)
if maze[y][x] == FLOOR:
maze[y][x] = PORTAL
portals_placed += 1
return maze
def select_start_position(maze):
width = len(maze[0])
height = len(maze)
while True:
x = random.randint(1, width - 2)
y = random.randint(1, height - 2)
if maze[y][x] == FLOOR:
return x, y
def place_start_position(maze):
x, y = select_start_position(maze)
if x is not None and y is not None:
maze[y][x] = START
return maze
def save_to_file(maze, filename):
f = fopen(filename, "w")
y = 0
while y < len(maze):
x = 0
line = ""
while x < len(maze[y]):
if maze[y][x] == WALL:
line = line + "#"
elif maze[y][x] == GRAVE:
line = line + "G"
elif maze[y][x] == PORTAL:
line = line + "P"
elif maze[y][x] == MAGIC_TORCH:
line = line + "M"
elif maze[y][x] == START:
line = line + "S"
else:
line = line + "."
x = x + 1
fwrite(f, line + "\n")
y = y + 1
fclose(f)
print("Maze saved to " + filename)
def print_maze(maze):
y = 0
while y < len(maze):
x = 0
line = ""
while x < len(maze[y]):
if maze[y][x] == WALL:
line = line + "#"
elif maze[y][x] == GRAVE:
line = line + "G"
elif maze[y][x] == PORTAL:
line = line + "P"
elif maze[y][x] == MAGIC_TORCH:
line = line + "M"
elif maze[y][x] == START:
line = line + "S"
else:
line = line + "."
x = x + 1
print(line)
y = y + 1
if __name__ == "__main__":
random.seed(time_tick())
width = 20
height = 11
maze = generate_maze(width, height)
maze = generate_graves_and_portals(maze)
maze = place_start_position(maze)
print_maze(maze)
save_to_file(maze, "maze.txt")