Source code for UniformHolesRandomMaze

#####################################################################################################################################################
######################################################################## INFO #######################################################################
#####################################################################################################################################################

# This file is part of the PyRat library.
# It is meant to be used as a library, and not to be executed directly.
# Please import necessary elements using the following syntax:
#     from pyrat import <element_name>

"""
This module provides a maze that is created by removing random cells from a full maze uniformly.
It makes sure the maze remains connected.
"""

#####################################################################################################################################################
###################################################################### IMPORTS ######################################################################
#####################################################################################################################################################

# PyRat imports
from pyrat.src.RandomMaze import RandomMaze

#####################################################################################################################################################
###################################################################### CLASSES ######################################################################
#####################################################################################################################################################

[docs] class UniformHolesRandomMaze (RandomMaze): """ *(This class inherits from* ``RandomMaze`` *).* With this maze, holes are uniformly distributed in the maze. The maze is created by removing random cells from a full maze, and making sure the maze remains connected. """ ############################################################################################################################################# # MAGIC METHODS # #############################################################################################################################################
[docs] def __init__ ( self, *args: object, **kwargs: object ) -> None: """ Initializes a new instance of the class. Args: args: Arguments to pass to the parent constructor. kwargs: Keyword arguments to pass to the parent constructor. """ # Inherit from parent class super().__init__(*args, **kwargs) # Generate the maze self._create_maze()
############################################################################################################################################# # PROTECTED METHODS # ############################################################################################################################################# def _add_cells (self) -> None: """ *(This method redefines the method of the parent class with the same name).* It adds cells to the maze by starting from a full maze and removing cells one by one. """ # Initialize maze with all cells for row in range(self.get_height()): for col in range(self.get_width()): self.add_vertex(self.rc_to_i(row, col)) # Connect them for row in range(self.get_height()): for col in range(self.get_width()): if row > 0: self.add_edge(self.rc_to_i(row, col), self.rc_to_i(row - 1, col)) if col > 0: self.add_edge(self.rc_to_i(row, col), self.rc_to_i(row, col - 1)) # Remove some vertices until the desired density is reached while self.nb_vertices() > self._target_nb_vertices: # Remove a random vertex vertex = self._rng.choice(self.get_vertices()) neighbors = self.get_neighbors(vertex) self.remove_vertex(vertex) # Make sure the maze is still connected if not self.is_connected(): self.add_vertex(vertex) for neighbor in neighbors: self.add_edge(vertex, neighbor)
##################################################################################################################################################### #####################################################################################################################################################