From 5f1ffcc01c4d0e1db1472e3e3d885c80e4735c50 Mon Sep 17 00:00:00 2001 From: Maoake Teriierooiterai Date: Wed, 25 Mar 2026 17:00:14 +0100 Subject: [PATCH] finish the solver --- a_maze_ing.py | 4 +- src/amaz_lib/MazeGenerator.py | 2 +- src/amaz_lib/MazeSolver.py | 86 +++++++++++++++++++++++++++++++++-- src/amaz_lib/__init__.py | 4 +- 4 files changed, 87 insertions(+), 9 deletions(-) diff --git a/a_maze_ing.py b/a_maze_ing.py index b1fc419..22c620e 100644 --- a/a_maze_ing.py +++ b/a_maze_ing.py @@ -7,10 +7,12 @@ import src.amaz_lib as g def main(maze_gen: MazeGenerator) -> None: # try: maze = Maze(maze=None) - for alg in maze_gen.generator(10, 10): + solver = g.DepthFirstSearchSolver((1, 1), (5, 8)) + for alg in maze_gen.generator(15, 15): maze.set_maze(alg) os.system("clear") maze.ascii_print() + print(solver.solve(maze, 15, 15)) # solver = AStar((1, 1), (14, 18)) # print(solver.solve(maze)) diff --git a/src/amaz_lib/MazeGenerator.py b/src/amaz_lib/MazeGenerator.py index 0630f90..7248b81 100644 --- a/src/amaz_lib/MazeGenerator.py +++ b/src/amaz_lib/MazeGenerator.py @@ -230,7 +230,7 @@ class DepthFirstSearch(MazeGenerator): return {"N": "S", "S": "N", "W": "E", "E": "W"}[direction] @staticmethod - def back_on_step(path: list, w_h: tuple, visited: np.array) -> list: + def back_on_step(path: list, w_h: tuple, visited: np.ndarray) -> list: while path: last = path[-1] if DepthFirstSearch.random_cells(visited, last, w_h): diff --git a/src/amaz_lib/MazeSolver.py b/src/amaz_lib/MazeSolver.py index b5ae9a7..e10a0bf 100644 --- a/src/amaz_lib/MazeSolver.py +++ b/src/amaz_lib/MazeSolver.py @@ -9,7 +9,8 @@ class MazeSolver(ABC): self.end = (end[0] - 1, end[1] - 1) @abstractmethod - def solve(self, maze: Maze) -> str: ... + def solve(self, maze: Maze, height: int = None, + width: int = None) -> str: ... class AStar(MazeSolver): @@ -107,11 +108,11 @@ class AStar(MazeSolver): case _: return actual - def get_path(self, maze: np.ndarray) -> str | None: - actual = self.start - path = "" + # def get_path(self, maze: np.ndarray) -> str | None: + # actual = self.start + # path = "" - return None + # return None def solve(self, maze: Maze) -> str: print(maze) @@ -119,3 +120,78 @@ class AStar(MazeSolver): if res is None: raise Exception("Path not found") return res + + +class DepthFirstSearchSolver(MazeSolver): + def __init__(self, start, end): + super().__init__(start, end) + + def solve(self, maze: Maze, height: int = None, + width: int = None) -> str: + path_str = "" + visited = np.zeros((height, width), dtype=bool) + path = list() + move = list() + maze_s = maze.get_maze() + coord = self.start + h_w = (height, width) + while coord != self.end: + visited[coord] = True + path.append(coord) + rand_p = self.random_path(visited, coord, maze_s, h_w) + + if not rand_p: + path, move = self.back_on_step(path, visited, maze_s, h_w, + move) + if not path: + break + coord = path[-1] + rand_p = self.random_path(visited, coord, maze_s, h_w) + next = self.next_path(rand_p) + move.append(next) + coord = self.next_cell(coord, next) + for m in move: + path_str += m + return path_str + + @staticmethod + def random_path(visited: np.ndarray, coord: tuple, + maze: np.ndarray, h_w: tuple) -> list: + random_p = [] + h, w = h_w + y, x = coord + + if y - 1 >= 0 and not maze[y][x].get_north() and not visited[y - 1][x]: + random_p.append("N") + + if y + 1 < h and not maze[y][x].get_south() and not visited[y + 1][x]: + random_p.append("S") + + if x - 1 >= 0 and not maze[y][x].get_west() and not visited[y][x - 1]: + random_p.append("W") + + if x + 1 < w and not maze[y][x].get_est() and not visited[y][x + 1]: + random_p.append("E") + return random_p + + @staticmethod + def next_path(rand_path: list) -> str: + return np.random.choice(rand_path) + + @staticmethod + def back_on_step(path: list, visited: np.ndarray, + maze: np.ndarray, h_w: tuple, move: list) -> list: + while path: + last = path[-1] + if DepthFirstSearchSolver.random_path(visited, last, maze, h_w): + break + path.pop() + move.pop() + return path, move + + @staticmethod + def next_cell(coord: tuple, next: str) -> tuple: + y, x = coord + next_step = {"N": (-1, 0), "S": (1, 0), "W": (0, -1), "E": (0, 1)} + add_y, add_x = next_step[next] + return (y + add_y, x + add_x) diff --git a/src/amaz_lib/__init__.py b/src/amaz_lib/__init__.py index fda6b32..2c20d16 100644 --- a/src/amaz_lib/__init__.py +++ b/src/amaz_lib/__init__.py @@ -2,9 +2,9 @@ from .Cell import Cell from .Maze import Maze from .MazeGenerator import MazeGenerator, DepthFirstSearch from .MazeGenerator import Kruskal -from .MazeSolver import MazeSolver, AStar +from .MazeSolver import MazeSolver, AStar, DepthFirstSearchSolver __version__ = "1.0.0" __author__ = "us" -__all__ = ["Cell", "Maze", "MazeGenerator", +__all__ = ["Cell", "Maze", "MazeGenerator", "DepthFirstSearchSolver", "MazeSolver", "AStar", "Kruskal", "DepthFirstSearch"]