diff --git a/a_maze_ing.py b/a_maze_ing.py index 22c620e..bf948d8 100644 --- a/a_maze_ing.py +++ b/a_maze_ing.py @@ -7,7 +7,7 @@ import src.amaz_lib as g def main(maze_gen: MazeGenerator) -> None: # try: maze = Maze(maze=None) - solver = g.DepthFirstSearchSolver((1, 1), (5, 8)) + solver = g.DepthFirstSearchSolver((1, 1), (8, 5)) for alg in maze_gen.generator(15, 15): maze.set_maze(alg) os.system("clear") @@ -22,4 +22,4 @@ def main(maze_gen: MazeGenerator) -> None: if __name__ == "__main__": - main(g.DepthFirstSearch()) + main(g.DepthFirstSearch((1, 1), (8, 5))) diff --git a/src/amaz_lib/MazeGenerator.py b/src/amaz_lib/MazeGenerator.py index 7248b81..8595d01 100644 --- a/src/amaz_lib/MazeGenerator.py +++ b/src/amaz_lib/MazeGenerator.py @@ -6,6 +6,11 @@ import math class MazeGenerator(ABC): + def __init__(self, start: tuple, end: tuple, perfect: bool) -> None: + self.start = (start[0] - 1, start[1] - 1) + self.end = (end[0] - 1, end[1] - 1) + self.bool = bool + @abstractmethod def generator( self, height: int, width: int, seed: int = None @@ -35,6 +40,27 @@ class MazeGenerator(ABC): forty_two.add((y + 2, x + 3)) return forty_two + @staticmethod + def unperfect_maze(width: int, height: int, + maze: np.ndarray, forty_two: set + ) -> Generator[np.ndarray, None, np.ndarray]: + broken_set = DepthFirstSearch.gen_broken_set(width, height) + final_set = broken_set.difference(forty_two) + for coord in broken_set: + yield maze + return broken_set + + @staticmethod + def gen_broken_set(width: int, height: int) -> set: + maxi = int(width * height * 0.1) + points = set() + while len(points) < maxi: + points.add( + (np.random.randint(low=1, high=(width - 1)), + np.random.randint(low=1, high=(height - 1))) + ) + return points + class Kruskal(MazeGenerator): class Set: @@ -130,6 +156,10 @@ class Kruskal(MazeGenerator): class DepthFirstSearch(MazeGenerator): + def __init__(self, start: bool, end: bool, perfect: bool) -> None: + self.start = start + self.end = end + self.perfect = perfect def generator( self, height: int, width: int, seed: int = None @@ -139,7 +169,8 @@ class DepthFirstSearch(MazeGenerator): maze = self.init_maze(width, height) forty_two = self.get_cell_ft(width, height) visited = np.zeros((height, width), dtype=bool) - visited = self.lock_cell_ft(visited, forty_two) + if self.start not in forty_two and self.end not in forty_two: + visited = self.lock_cell_ft(visited, forty_two) path = list() w_h = (width, height) coord = (0, 0) diff --git a/src/amaz_lib/MazeSolver.py b/src/amaz_lib/MazeSolver.py index e10a0bf..3fa5543 100644 --- a/src/amaz_lib/MazeSolver.py +++ b/src/amaz_lib/MazeSolver.py @@ -124,7 +124,8 @@ class AStar(MazeSolver): class DepthFirstSearchSolver(MazeSolver): def __init__(self, start, end): - super().__init__(start, end) + self.start = (start[1] - 1, start[0] - 1) + self.end = (end[1] - 1, end[0] - 1) def solve(self, maze: Maze, height: int = None, width: int = None) -> str: diff --git a/tests/test_MazeGenerator.py b/tests/test_MazeGenerator.py index ce9adc1..99278c0 100644 --- a/tests/test_MazeGenerator.py +++ b/tests/test_MazeGenerator.py @@ -1,14 +1,18 @@ import numpy -from amaz_lib.MazeGenerator import DepthFirstSearch +from amaz_lib.MazeGenerator import DepthFirstSearch, MazeGenerator class TestMazeGenerator: def test_generator(self) -> None: - w_h = (50, 50) + w_h = (10, 10) maze = numpy.array([]) - generator = DepthFirstSearch().generator(*w_h) + generator = DepthFirstSearch((1, 1), (2, 2), True).generator(*w_h) for output in generator: maze = output assert maze.shape == w_h + + def test_gen_broken(self) -> None: + test = MazeGenerator.gen_broken_set(50, 50) + assert len(test) > 0