-
Notifications
You must be signed in to change notification settings - Fork 6
/
mazeGenerator.py
223 lines (193 loc) · 7.32 KB
/
mazeGenerator.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
# mazeGenerator.py
# ----------------
# Licensing Information: Please do not distribute or publish solutions to this
# project. You are free to use and extend these projects for educational
# purposes. The Pacman AI projects were developed at UC Berkeley, primarily by
# John DeNero (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu).
# For more info, see http://inst.eecs.berkeley.edu/~cs188/sp09/pacman.html
import random
"""
maze generator code
algorithm:
start with an empty grid
draw a wall with gaps, dividing the grid in 2
repeat recursively for each sub-grid
pacman details:
players 1,3 always start in the bottom left; 2,4 in the top right
food is placed in dead ends and then randomly (though not too close to the pacmen starting positions)
notes:
the final map includes a symmetric, flipped copy
the first wall has k gaps, the next wall has k/2 gaps, etc. (min=1)
@author: Dan Gillick
"""
W = '%'
F = '.'
C = 'o'
E = ' '
class Maze:
def __init__(self, rows, cols, anchor=(0, 0), root=None):
"""
generate an empty maze
anchor is the top left corner of this grid's position in its parent grid
"""
self.r = rows
self.c = cols
self.grid = [[E for col in range(cols)] for row in range(rows)]
self.anchor = anchor
self.rooms = []
self.root = root
if not self.root: self.root = self
def to_map(self):
"""
add a flipped symmetric copy on the right
add a border
"""
## add a flipped symmetric copy
for row in range(self.r):
for col in range(self.c-1, -1, -1):
self.grid[self.r-row-1].append(self.grid[row][col])
self.c *= 2
## add a border
for row in range(self.r):
self.grid[row] = [W] + self.grid[row] + [W]
self.c += 2
self.grid.insert(0, [W for c in range(self.c)])
self.grid.append([W for c in range(self.c)])
self.r += 2
def __str__(self):
s = ''
for row in range(self.r):
for col in range(self.c):
s += self.grid[row][col]
s += '\n'
return s[:-1]
def add_wall(self, i, gaps=1, vert=True):
"""
add a wall with gaps
"""
add_r, add_c = self.anchor
if vert:
gaps = min(self.r, gaps)
slots = [add_r+x for x in range(self.r)]
if not 0 in slots:
if self.root.grid[min(slots)-1][add_c+i] == E: slots.remove(min(slots))
if len(slots) <= gaps: return 0
if not self.root.c-1 in slots:
if self.root.grid[max(slots)+1][add_c+i] == E: slots.remove(max(slots))
if len(slots) <= gaps: return 0
random.shuffle(slots)
for row in slots[gaps:]:
self.root.grid[row][add_c+i] = W
self.rooms.append(Maze(self.r, i, (add_r,add_c), self.root))
self.rooms.append(Maze(self.r, self.c-i-1, (add_r,add_c+i+1), self.root))
else:
gaps = min(self.c, gaps)
slots = [add_c+x for x in range(self.c)]
if not 0 in slots:
if self.root.grid[add_r+i][min(slots)-1] == E: slots.remove(min(slots))
if len(slots) <= gaps: return 0
if not self.root.r-1 in slots:
if self.root.grid[add_r+i][max(slots)+1] == E: slots.remove(max(slots))
if len(slots) <= gaps: return 0
random.shuffle(slots)
for col in slots[gaps:]:
self.root.grid[add_r+i][col] = W
self.rooms.append(Maze(i, self.c, (add_r,add_c), self.root))
self.rooms.append(Maze(self.r-i-1, self.c, (add_r+i+1,add_c), self.root))
return 1
def make(room, depth, gaps=1, vert=True, min_width=1):
"""
recursively build a maze
TODO: randomize number of gaps?
"""
## extreme base case
if room.r <= min_width and room.c <= min_width: return
## decide between vertical and horizontal wall
if vert: num = room.c
else: num = room.r
if num < min_width + 2:
vert = not vert
if vert: num = room.c
else: num = room.r
## add a wall to the current room
if depth==0: wall_slots = [num-2] ## fix the first wall
else: wall_slots = range(1, num-1)
if len(wall_slots) == 0: return
choice = random.choice(wall_slots)
if not room.add_wall(choice, gaps, vert): return
## recursively add walls
for sub_room in room.rooms:
make(sub_room, depth+1, max(1,gaps/2), not vert, min_width)
def copy_grid(grid):
new_grid = []
for row in range(len(grid)):
new_grid.append([])
for col in range(len(grid[row])):
new_grid[row].append(grid[row][col])
return new_grid
def add_pacman_stuff(maze, max_food=60, max_capsules=4):
"""
add pacmen starting position
add food at dead ends plus some extra
"""
## parameters
max_depth = 2
## add food at dead ends
depth = 0
total_food = 0
while True:
new_grid = copy_grid(maze.grid)
depth += 1
num_added = 0
for row in range(1, maze.r-1):
for col in range(1, (maze.c/2)-1):
if (row > maze.r-6) and (col < 6): continue
if maze.grid[row][col] != E: continue
neighbors = (maze.grid[row-1][col]==E) + (maze.grid[row][col-1]==E) + (maze.grid[row+1][col]==E) + (maze.grid[row][col+1]==E)
if neighbors == 1:
new_grid[row][col] = F
new_grid[maze.r-row-1][maze.c-(col)-1] = F
num_added += 2
total_food += 2
maze.grid = new_grid
if num_added == 0: break
if depth >= max_depth: break
## starting pacmen positions
maze.grid[maze.r-2][1] = '5'
maze.grid[maze.r-3][1] = '3'
maze.grid[maze.r-4][1] = '1'
maze.grid[1][maze.c-2] = '6'
maze.grid[2][maze.c-2] = '4'
maze.grid[3][maze.c-2] = '2'
## add capsules
total_capsules = 0
while total_capsules < max_capsules:
row = random.randint(1, maze.r-1)
col = random.randint(1, (maze.c/2)-2)
if (row > maze.r-6) and (col < 6): continue
if(abs(col - maze.c/2) < 3): continue
if maze.grid[row][col] == E:
maze.grid[row][col] = C
maze.grid[maze.r-row-1][maze.c-(col)-1] = C
total_capsules += 2
## extra random food
while total_food < max_food:
row = random.randint(1, maze.r-1)
col = random.randint(1, (maze.c/2)-1)
if (row > maze.r-6) and (col < 6): continue
if(abs(col - maze.c/2) < 3): continue
if maze.grid[row][col] == E:
maze.grid[row][col] = F
maze.grid[maze.r-row-1][maze.c-(col)-1] = F
total_food += 2
MAX_DIFFERENT_MAZES = 10000
def generateMaze():
import random, sys
random.seed(random.randint(1,MAX_DIFFERENT_MAZES))
maze = Maze(16,16)
make(maze, depth=0, gaps=6, vert=True, min_width=1)
maze.to_map()
add_pacman_stuff(maze, 2*(maze.r*maze.c/20))
return str(maze)
if __name__ == '__main__':
print generateMaze()