pyrat.src.UniformHolesRandomMaze
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
1##################################################################################################################################################### 2######################################################################## INFO ####################################################################### 3##################################################################################################################################################### 4 5""" 6 This file is part of the PyRat library. 7 It is meant to be used as a library, and not to be executed directly. 8 Please import necessary elements using the following syntax: 9 from pyrat import <element_name> 10""" 11 12##################################################################################################################################################### 13###################################################################### IMPORTS ###################################################################### 14##################################################################################################################################################### 15 16# External imports 17from typing import * 18from typing_extensions import * 19from numbers import * 20 21# PyRat imports 22from pyrat.src.RandomMaze import RandomMaze 23 24##################################################################################################################################################### 25###################################################################### CLASSES ###################################################################### 26##################################################################################################################################################### 27 28class UniformHolesRandomMaze (RandomMaze): 29 30 """ 31 This class inherits from the RandomMaze class. 32 Therefore, it has the attributes and methods defined in the RandomMaze class in addition to the ones defined below. 33 34 With this maze, holes are uniformly distributed in the maze. 35 The maze is created by removing random cells from a full maze, and making sure the maze remains connected. 36 """ 37 38 ############################################################################################################################################# 39 # MAGIC METHODS # 40 ############################################################################################################################################# 41 42 def __init__ ( self: Self, 43 *args: Any, 44 **kwargs: Any 45 ) -> Self: 46 47 """ 48 This function is the constructor of the class. 49 When an object is instantiated, this method is called to initialize the object. 50 This is where you should define the attributes of the object and set their initial values. 51 Arguments *args and **kwargs are used to pass arguments to the parent constructor. 52 This is useful not to declare again all the parent's attributes in the child class. 53 In: 54 * self: Reference to the current object. 55 * args: Arguments to pass to the parent constructor. 56 * kwargs: Keyword arguments to pass to the parent constructor. 57 Out: 58 * A new instance of the class. 59 """ 60 61 # Inherit from parent class 62 super().__init__(*args, **kwargs) 63 64 # Generate the maze 65 self._create_maze() 66 67 ############################################################################################################################################# 68 # PROTECTED METHODS # 69 ############################################################################################################################################# 70 71 @override 72 def _add_cells ( self: Self, 73 ) -> None: 74 75 """ 76 This method redefines the abstract method of the parent class. 77 It adds cells to the maze by starting from a full maze and removing cells one by one. 78 In: 79 * self: Reference to the current object. 80 Out: 81 * None. 82 """ 83 84 # Initialize maze with all cells 85 for row in range(self.height): 86 for col in range(self.width): 87 self.add_vertex(self.rc_to_i(row, col)) 88 89 # Connect them 90 for row in range(self.height): 91 for col in range(self.width): 92 if row > 0: 93 self.add_edge(self.rc_to_i(row, col), self.rc_to_i(row - 1, col)) 94 if col > 0: 95 self.add_edge(self.rc_to_i(row, col), self.rc_to_i(row, col - 1)) 96 97 # Remove some vertices until the desired density is reached 98 while self.nb_vertices() > self._target_nb_vertices: 99 100 # Remove a random vertex 101 vertex = self._rng.choice(self.vertices) 102 neighbors = self.get_neighbors(vertex) 103 self.remove_vertex(vertex) 104 105 # Make sure the maze is still connected 106 if not self.is_connected(): 107 self.add_vertex(vertex) 108 for neighbor in neighbors: 109 self.add_edge(vertex, neighbor) 110 111##################################################################################################################################################### 112#####################################################################################################################################################
29class UniformHolesRandomMaze (RandomMaze): 30 31 """ 32 This class inherits from the RandomMaze class. 33 Therefore, it has the attributes and methods defined in the RandomMaze class in addition to the ones defined below. 34 35 With this maze, holes are uniformly distributed in the maze. 36 The maze is created by removing random cells from a full maze, and making sure the maze remains connected. 37 """ 38 39 ############################################################################################################################################# 40 # MAGIC METHODS # 41 ############################################################################################################################################# 42 43 def __init__ ( self: Self, 44 *args: Any, 45 **kwargs: Any 46 ) -> Self: 47 48 """ 49 This function is the constructor of the class. 50 When an object is instantiated, this method is called to initialize the object. 51 This is where you should define the attributes of the object and set their initial values. 52 Arguments *args and **kwargs are used to pass arguments to the parent constructor. 53 This is useful not to declare again all the parent's attributes in the child class. 54 In: 55 * self: Reference to the current object. 56 * args: Arguments to pass to the parent constructor. 57 * kwargs: Keyword arguments to pass to the parent constructor. 58 Out: 59 * A new instance of the class. 60 """ 61 62 # Inherit from parent class 63 super().__init__(*args, **kwargs) 64 65 # Generate the maze 66 self._create_maze() 67 68 ############################################################################################################################################# 69 # PROTECTED METHODS # 70 ############################################################################################################################################# 71 72 @override 73 def _add_cells ( self: Self, 74 ) -> None: 75 76 """ 77 This method redefines the abstract method of the parent class. 78 It adds cells to the maze by starting from a full maze and removing cells one by one. 79 In: 80 * self: Reference to the current object. 81 Out: 82 * None. 83 """ 84 85 # Initialize maze with all cells 86 for row in range(self.height): 87 for col in range(self.width): 88 self.add_vertex(self.rc_to_i(row, col)) 89 90 # Connect them 91 for row in range(self.height): 92 for col in range(self.width): 93 if row > 0: 94 self.add_edge(self.rc_to_i(row, col), self.rc_to_i(row - 1, col)) 95 if col > 0: 96 self.add_edge(self.rc_to_i(row, col), self.rc_to_i(row, col - 1)) 97 98 # Remove some vertices until the desired density is reached 99 while self.nb_vertices() > self._target_nb_vertices: 100 101 # Remove a random vertex 102 vertex = self._rng.choice(self.vertices) 103 neighbors = self.get_neighbors(vertex) 104 self.remove_vertex(vertex) 105 106 # Make sure the maze is still connected 107 if not self.is_connected(): 108 self.add_vertex(vertex) 109 for neighbor in neighbors: 110 self.add_edge(vertex, neighbor)
This class inherits from the RandomMaze class. Therefore, it has the attributes and methods defined in the RandomMaze class in addition to the ones defined below.
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.
43 def __init__ ( self: Self, 44 *args: Any, 45 **kwargs: Any 46 ) -> Self: 47 48 """ 49 This function is the constructor of the class. 50 When an object is instantiated, this method is called to initialize the object. 51 This is where you should define the attributes of the object and set their initial values. 52 Arguments *args and **kwargs are used to pass arguments to the parent constructor. 53 This is useful not to declare again all the parent's attributes in the child class. 54 In: 55 * self: Reference to the current object. 56 * args: Arguments to pass to the parent constructor. 57 * kwargs: Keyword arguments to pass to the parent constructor. 58 Out: 59 * A new instance of the class. 60 """ 61 62 # Inherit from parent class 63 super().__init__(*args, **kwargs) 64 65 # Generate the maze 66 self._create_maze()
This function is the constructor of the class. When an object is instantiated, this method is called to initialize the object. This is where you should define the attributes of the object and set their initial values. Arguments args and *kwargs are used to pass arguments to the parent constructor. This is useful not to declare again all the parent's attributes in the child class.
In:
- self: Reference to the current object.
- args: Arguments to pass to the parent constructor.
- kwargs: Keyword arguments to pass to the parent constructor.
Out:
- A new instance of the class.