def __generate_maze__(self, size): AbstractGenerator.__generate_maze__(self, size) """implement Prim's Algorithm""" self.mesh = Mesh(size) cell = self.mesh.choose_cell(self.random) self.queue = [cell] while len(self.queue) > 0: n = self.random.randint(0, len(self.queue) - 1) cell = self.queue[n] self.log.debug('looking at ' + str(cell)) wall = self.mesh.choose_wall(cell, self.random) if wall == cell.get_left(): neighbour = self.mesh.get_left_neighbour(cell) elif wall == cell.get_right(): neighbour = self.mesh.get_right_neighbour(cell) elif wall == cell.get_top(): neighbour = self.mesh.get_top_neighbour(cell) elif wall == cell.get_bottom(): neighbour = self.mesh.get_bottom_neighbour(cell) else: self.log.error('Invalid wall: ' + str(wall)) raise Exception('Invalid wall: ' + str(wall)) if cell.get_set() != neighbour.get_set(): self.log.debug('removing wall to ' + str(neighbour)) wall.remove() self.mesh.move_cell(neighbour.get_set(), cell.get_set()) self.queue.append(neighbour) else: self.__create_loops__(cell, neighbour, wall) if not self.mesh.has_neighbour_in_different_set(cell): self.log.debug('removing cell from queue') self.queue.remove(cell) return self.mesh
def __generate_maze__(self, size): AbstractGenerator.__generate_maze__(self, size) """implement Recursive Division Algorithm""" self.mesh = Mesh(size, True) for i in range(0, size): # create boundary walls self.mesh.get_cell(i, 0).create_left() self.mesh.get_cell(i, size - 1).create_right() self.mesh.get_cell(0, i).create_top() self.mesh.get_cell(size - 1, i).create_bottom() self.__divideAndGenerate__(0, size - 1, 0, size - 1) return self.mesh
def __generate_maze__(self, size): AbstractGenerator.__generate_maze__(self, size) """implement Binary Tree Algorithm""" mesh = Mesh(size) for i in range(1, size): mesh.get_cell(0, i).remove_left() mesh.get_cell(i, 0).remove_top() for i in range(1, size): for j in range(1, size): cell = mesh.get_cell(i, j) if self.random.random() > 0.5: cell.remove_left() else: cell.remove_top() self.__create_loops__(cell, size) return mesh
def __generate_maze__(self, size): AbstractGenerator.__generate_maze__(self, size) """implement Depth-first Search Algorithm""" self.mesh = Mesh(size) cell = self.mesh.choose_cell(self.random) self.stack = [] while self.mesh.has_multiple_sets(): if self.mesh.has_neighbour_in_different_set(cell): wall = self.mesh.choose_wall(cell, self.random) if wall == cell.get_left(): neighbour = self.mesh.get_left_neighbour(cell) if neighbour.get_set() != cell.get_set(): cell.remove_left() self.mesh.move_cell(neighbour.get_set(), cell.get_set()) self.stack.append(cell) cell = neighbour elif wall == cell.get_right(): neighbour = self.mesh.get_right_neighbour(cell) if neighbour.get_set() != cell.get_set(): cell.remove_right() self.mesh.move_cell(neighbour.get_set(), cell.get_set()) self.stack.append(cell) cell = neighbour elif wall == cell.get_top(): neighbour = self.mesh.get_top_neighbour(cell) if neighbour.get_set() != cell.get_set(): cell.remove_top() self.mesh.move_cell(neighbour.get_set(), cell.get_set()) self.stack.append(cell) cell = neighbour elif wall == cell.get_bottom(): neighbour = self.mesh.get_bottom_neighbour(cell) if neighbour.get_set() != cell.get_set(): cell.remove_bottom() self.mesh.move_cell(neighbour.get_set(), cell.get_set()) self.stack.append(cell) cell = neighbour else: self.log.error('Invalid wall') raise Exception('Invalid wall') else: cell = self.__handleDeadEnd__(cell) return self.mesh
def __generate_maze__(self, size): AbstractGenerator.__generate_maze__(self, size) """ Kruskal's Algorithm""" mesh = Mesh(size) while mesh.has_multiple_sets(): cell = mesh.choose_cell(self.random) wall = mesh.choose_wall(cell, self.random) if wall == cell.get_left(): neighbour = mesh.get_left_neighbour(cell) elif wall == cell.get_right(): neighbour = mesh.get_right_neighbour(cell) elif wall == cell.get_top(): neighbour = mesh.get_top_neighbour(cell) elif wall == cell.get_bottom(): neighbour = mesh.get_bottom_neighbour(cell) else: self.log.error('Invalid wall') raise Exception('Invalid wall') if neighbour.get_set() != cell.get_set(): wall.remove() mesh.move_cell(neighbour.get_set(), cell.get_set()) else: self.__create_loops__(wall, size) return mesh
def __init__(self): AbstractGenerator.__init__(self) self.log = logging.getLogger(__name__)