add generator to my maze generator DFS

This commit is contained in:
Maoake Teriierooiterai
2026-03-24 15:22:20 +01:00
parent a85e342a0a
commit 993bcce857
2 changed files with 9 additions and 17 deletions
+5 -14
View File
@@ -1,30 +1,21 @@
import os import os
from src.amaz_lib import Kruskal
from src.amaz_lib import DepthFirstSearch
from src.amaz_lib import Maze from src.amaz_lib import Maze
from src.amaz_lib import MazeGenerator
import src.amaz_lib as g
def main() -> None: def main(maze_gen: MazeGenerator) -> None:
# try: # try:
maze = Maze(maze=None) maze = Maze(maze=None)
gen = Kruskal().generator(10, 10) gen = maze_gen.generator(100, 100)
for alg in gen: for alg in gen:
maze.set_maze(alg) maze.set_maze(alg)
os.system("clear") os.system("clear")
maze.ascii_print() maze.ascii_print()
def main2() -> None:
maze = Maze(maze=None)
gen = DepthFirstSearch.generator(50, 50)
maze.set_maze(gen)
os.system("clear")
maze.ascii_print()
# except Exception as err: # except Exception as err:
# print(err) # print(err)
if __name__ == "__main__": if __name__ == "__main__":
main2() main(g.DepthFirstSearch())
+4 -3
View File
@@ -84,10 +84,10 @@ class Kruskal(MazeGenerator):
return self.walls_to_maze(walls, height, width) return self.walls_to_maze(walls, height, width)
class DepthFirstSearch: class DepthFirstSearch(MazeGenerator):
@staticmethod def generator(self, width: int, height: int
def generator(width: int, height: int) -> np.ndarray: ) -> Generator[np.ndarray, None, np.ndarray]:
maze = DepthFirstSearch.init_maze(width, height) maze = DepthFirstSearch.init_maze(width, height)
visited = np.zeros((height, width), dtype=bool) visited = np.zeros((height, width), dtype=bool)
path = list() path = list()
@@ -117,6 +117,7 @@ class DepthFirstSearch:
wall_r = DepthFirstSearch.reverse_path(wall) wall_r = DepthFirstSearch.reverse_path(wall)
x, y = coord x, y = coord
maze[y][x] = DepthFirstSearch.broken_wall(maze[y][x], wall_r) maze[y][x] = DepthFirstSearch.broken_wall(maze[y][x], wall_r)
yield maze
return maze return maze
@staticmethod @staticmethod