Module pacai.util.mazeGenerator
Maze Generator
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 and 3 always start in the bottom left; 2 and 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 @author: Jie Tang
Functions
def add_pacman_stuff(rng, maze, max_food=60, max_capsules=4, toskip=0)
-
Add pacmen starting position. Add food at dead ends plus some extra.
def copy_grid(grid)
def generateMaze(seed=None)
def make(rng, room, depth, gaps=1, vert=True, min_width=1, gapfactor=0.5)
-
Recursively build a maze.
def make_with_prison(rng, room, depth, gaps=1, vert=True, min_width=1, gapfactor=0.5)
-
Build a maze with 0,1,2 layers of prison (randomly).
Classes
class Maze (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.
Expand source code
class Maze(object): 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 = [[EMPTY 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] = [WALL] + self.grid[row] + [WALL] self.c += 2 self.grid.insert(0, [WALL for c in range(self.c)]) self.grid.append([WALL 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, rng, 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 0 not in slots: if self.root.grid[min(slots) - 1][add_c + i] == EMPTY: slots.remove(min(slots)) if len(slots) <= gaps: return 0 if (self.root.c - 1) not in slots: if self.root.grid[max(slots) + 1][add_c + i] == EMPTY: slots.remove(max(slots)) if len(slots) <= gaps: return 0 rng.shuffle(slots) for row in slots[int(round(gaps)):]: self.root.grid[row][add_c + i] = WALL 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 0 not in slots: if self.root.grid[add_r + i][min(slots) - 1] == EMPTY: slots.remove(min(slots)) if len(slots) <= gaps: return 0 if (self.root.r - 1) not in slots: if self.root.grid[add_r + i][max(slots) + 1] == EMPTY: slots.remove(max(slots)) if len(slots) <= gaps: return 0 rng.shuffle(slots) for col in slots[int(round(gaps)):]: self.root.grid[add_r + i][col] = WALL 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
Methods
def add_wall(self, rng, i, gaps=1, vert=True)
-
Add a wall with gaps.
def to_map(self)
-
Add a flipped symmetric copy on the right. Add a border.