def __generate_cells(self, active_cells): """Generate the cells for the maze. This method generates the cells for the maze using the Growing Tree Algorithm, storing the information about them in self.cells. Widgets are not added at this stage. Arguments: active_cells -- list of active cells. It should contain the starting cell as a level_cell.Cell. """ # Determines which index the algorithm generates from. Changing this yields different results. current_index = len(active_cells) - 1 # This generates from last index current_cell = active_cells[current_index] if current_cell.all_edges_initialised(): # Remove fully initialised cells from the list so that they are not revisited del active_cells[current_index] return direction = current_cell.get_random_uninitialised_direction() # Not using self.get_adjacent_cell as only coordinates are needed here next_cell_coords = Vector(*current_cell.coordinates) + direction.value if self.__contains_coordinates(next_cell_coords): next_cell = self.get_cell(next_cell_coords) if next_cell == None: # Create a new cell and set the relevant edges to passages if no cell exists at next_cell_coords next_cell = self.__create_cell(next_cell_coords) current_cell.get_edge(direction).type = level_cell.CellEdgeType.passage # Set corresponding edge of the other cell to a passage too, otherwise it will later become a wall next_cell.get_edge(direction.get_opposite()).type = level_cell.CellEdgeType.passage active_cells.append(next_cell) else: # If a cell exists at next_cell_coords, set the relevant edges to walls current_cell.get_edge(direction).type = level_cell.CellEdgeType.wall next_cell.get_edge(direction.get_opposite()).type = level_cell.CellEdgeType.wall # If next_cell_coords is outside the level boundaries, set the edge to a wall else: current_cell.get_edge(direction).type = level_cell.CellEdgeType.wall
def __set_edge_to_wall(self, cell, direction): """Change a given edge of a cell to a passage. This method sets a cell edge type of a given cell in a given direction to a wall, and also sets the corresponding edge in the adjacent cell to a wall. Arguments: cell - the level_cell.Cell whose edge will be changed direction - the direction of the edge to be changed as a direction.Direction """ edge = cell.get_edge(direction) edge.type = level_cell.CellEdgeType.wall adjacent_cell = self.get_adjacent_cell(cell, direction) # Also set relevant edge of adjacent cell if it exists if adjacent_cell != None: opposite_edge = adjacent_cell.get_edge(direction.get_opposite()) opposite_edge.type = level_cell.CellEdgeType.wall
def __set_edge_to_passage(self, cell, direction): """Change a given edge of a cell to a passage. This method sets a cell edge type of a given cell in a given direction to a passage, and also sets the corresponding edge in the adjacent cell to a passage. Note that edges at the edge of the level cannot be changed into passages. Arguments: cell - the level_cell.Cell whose edge will be changed direction - the direction of the edge to be changed as a direction.Direction """ edge = cell.get_edge(direction) adjacent_cell = self.get_adjacent_cell(cell, direction) if adjacent_cell != None: opposite_edge = adjacent_cell.get_edge(direction.get_opposite()) opposite_edge.type = level_cell.CellEdgeType.passage edge.type = level_cell.CellEdgeType.passage else: # Cell can only be set to passage if there is an adjacent cell raise error.NonExistentCellError("There is no adjacent cell.")