From 5f1ffcc01c4d0e1db1472e3e3d885c80e4735c50 Mon Sep 17 00:00:00 2001 From: Maoake Teriierooiterai Date: Wed, 25 Mar 2026 17:00:14 +0100 Subject: [PATCH 1/7] 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"] From b8631d5b89a03f2c7d15d14328e5086f63cdcee1 Mon Sep 17 00:00:00 2001 From: Maoake Teriierooiterai Date: Wed, 25 Mar 2026 17:24:32 +0100 Subject: [PATCH 2/7] doing some test --- tests/test_MazeGenerator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_MazeGenerator.py b/tests/test_MazeGenerator.py index 948748a..ce9adc1 100644 --- a/tests/test_MazeGenerator.py +++ b/tests/test_MazeGenerator.py @@ -5,7 +5,7 @@ from amaz_lib.MazeGenerator import DepthFirstSearch class TestMazeGenerator: def test_generator(self) -> None: - w_h = (300, 300) + w_h = (50, 50) maze = numpy.array([]) generator = DepthFirstSearch().generator(*w_h) for output in generator: From 6b3fd9a6b7d2f33cd50ff034510541d9c04e619d Mon Sep 17 00:00:00 2001 From: Maoake Teriierooiterai Date: Wed, 25 Mar 2026 18:45:30 +0100 Subject: [PATCH 3/7] need to finish the unperfect maze --- a_maze_ing.py | 4 ++-- src/amaz_lib/MazeGenerator.py | 33 ++++++++++++++++++++++++++++++++- src/amaz_lib/MazeSolver.py | 3 ++- tests/test_MazeGenerator.py | 10 +++++++--- 4 files changed, 43 insertions(+), 7 deletions(-) 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 From e4c91dc4a1cf7f3c08666e56e5a5b23471c0f3ee Mon Sep 17 00:00:00 2001 From: Maoake TERIIEROOITERAI Date: Thu, 26 Mar 2026 00:10:10 +0100 Subject: [PATCH 4/7] finish the imperfect maze --- a_maze_ing.py | 10 ++--- src/amaz_lib/MazeGenerator.py | 80 ++++++++++++++++++++++++++--------- 2 files changed, 64 insertions(+), 26 deletions(-) diff --git a/a_maze_ing.py b/a_maze_ing.py index bf948d8..80e354c 100644 --- a/a_maze_ing.py +++ b/a_maze_ing.py @@ -7,14 +7,10 @@ import src.amaz_lib as g def main(maze_gen: MazeGenerator) -> None: # try: maze = Maze(maze=None) - solver = g.DepthFirstSearchSolver((1, 1), (8, 5)) - for alg in maze_gen.generator(15, 15): + for alg in maze_gen.generator(5, 2): maze.set_maze(alg) - os.system("clear") + os.system("clear") maze.ascii_print() - print(solver.solve(maze, 15, 15)) - # solver = AStar((1, 1), (14, 18)) - # print(solver.solve(maze)) # except Exception as err: @@ -22,4 +18,4 @@ def main(maze_gen: MazeGenerator) -> None: if __name__ == "__main__": - main(g.DepthFirstSearch((1, 1), (8, 5))) + main(g.DepthFirstSearch((1, 1), (1, 1), False)) diff --git a/src/amaz_lib/MazeGenerator.py b/src/amaz_lib/MazeGenerator.py index 8595d01..58b3d02 100644 --- a/src/amaz_lib/MazeGenerator.py +++ b/src/amaz_lib/MazeGenerator.py @@ -42,24 +42,54 @@ class MazeGenerator(ABC): @staticmethod def unperfect_maze(width: int, height: int, - maze: np.ndarray, forty_two: set + maze: np.ndarray, forty_two: set | None, + prob: float = 0.1 ) -> 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 + directions = { + "N": (0, -1), + "S": (0, 1), + "W": (-1, 0), + "E": (1, 0) + } - @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 + reverse = { + "N": "S", + "S": "N", + "W": "E", + "E": "W" + } + min_break = 3 + while True: + count = 0 + for y in range(height): + for x in range(width): + if forty_two and (x, y) in forty_two: + continue + for direc, (dx, dy) in directions.items(): + nx, ny = x + dx, y + dy + if forty_two and ( + (y, x) in forty_two + or (ny, nx) in forty_two + ): + continue + if not (0 <= nx < width and 0 < ny < height): + continue + if direc in ["S", "E"]: + continue + if np.random.random() < prob: + count += 1 + cell = maze[y][x] + cell_n = maze[ny][nx] + cell = DepthFirstSearch.broken_wall(cell, direc) + cell_n = DepthFirstSearch.broken_wall(cell_n, + reverse[ + direc]) + maze[y][x] = cell + maze[ny][nx] = cell_n + yield maze + if count > min_break: + break + return maze class Kruskal(MazeGenerator): @@ -160,6 +190,7 @@ class DepthFirstSearch(MazeGenerator): self.start = start self.end = end self.perfect = perfect + self.forty_two: set | None = None def generator( self, height: int, width: int, seed: int = None @@ -167,10 +198,15 @@ class DepthFirstSearch(MazeGenerator): if seed is not None: np.random.seed(seed) maze = self.init_maze(width, height) - forty_two = self.get_cell_ft(width, height) + if width > 9 and height > 9: + self.forty_two = self.get_cell_ft(width, height) visited = np.zeros((height, width), dtype=bool) - if self.start not in forty_two and self.end not in forty_two: - visited = self.lock_cell_ft(visited, forty_two) + if ( + self.forty_two + and self.start not in self.forty_two + and self.end not in self.forty_two + ): + visited = self.lock_cell_ft(visited, self.forty_two) path = list() w_h = (width, height) coord = (0, 0) @@ -201,6 +237,12 @@ class DepthFirstSearch(MazeGenerator): x, y = coord maze[y][x] = self.broken_wall(maze[y][x], wall_r) yield maze + if self.perfect is False: + gen = DepthFirstSearch.unperfect_maze(width, height, maze, + self.forty_two) + for res in gen: + maze = res + yield maze return maze @staticmethod From 5aec319f7b2543dbe1727072346abc1564f74d0c Mon Sep 17 00:00:00 2001 From: Maoake TERIIEROOITERAI Date: Thu, 26 Mar 2026 00:58:07 +0100 Subject: [PATCH 5/7] need to fix the unperfect maze and add the function in the kruskal generator --- Makefile | 3 ++ config.txt | 10 +++--- src/AMazeIng.py | 4 +-- src/amaz_lib/MazeGenerator.py | 2 +- src/parsing/Parsing.py | 9 ++++-- test.txt | 58 +++++++++++++++-------------------- 6 files changed, 41 insertions(+), 45 deletions(-) diff --git a/Makefile b/Makefile index 0d575a3..84ea907 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,9 @@ install: run: install uv run python3 a_maze_ing.py config.txt +run_windows: + .venv\Scripts\python -m a_maze_ing config.txt + debug: uv pdb python3 a_maze_ing.py config.txt diff --git a/config.txt b/config.txt index 8f3293a..b187b15 100644 --- a/config.txt +++ b/config.txt @@ -1,8 +1,8 @@ -WIDTH=30 -HEIGHT=30 +WIDTH=20 +HEIGHT=20 ENTRY=1,1 -EXIT=29,29 +EXIT=2,1 OUTPUT_FILE=maze.txt -PERFECT=True -GENERATOR=Kruskal +PERFECT=False +GENERATOR=DFS SOLVER=AStar diff --git a/src/AMazeIng.py b/src/AMazeIng.py index 3e1f951..03bcee4 100644 --- a/src/AMazeIng.py +++ b/src/AMazeIng.py @@ -8,8 +8,8 @@ from src.amaz_lib import Maze, MazeGenerator, MazeSolver class AMazeIng(BaseModel): model_config = ConfigDict(arbitrary_types_allowed=True) - width: int = Field(ge=3) - height: int = Field(ge=3) + width: int = Field(ge=4) + height: int = Field(ge=4) entry: tuple[int, int] exit: tuple[int, int] output_file: str = Field(min_length=3) diff --git a/src/amaz_lib/MazeGenerator.py b/src/amaz_lib/MazeGenerator.py index 933c604..fc4c163 100644 --- a/src/amaz_lib/MazeGenerator.py +++ b/src/amaz_lib/MazeGenerator.py @@ -58,7 +58,7 @@ class MazeGenerator(ABC): "W": "E", "E": "W" } - min_break = 3 + min_break = 2 while True: count = 0 for y in range(height): diff --git a/src/parsing/Parsing.py b/src/parsing/Parsing.py index 97bf839..75659c7 100644 --- a/src/parsing/Parsing.py +++ b/src/parsing/Parsing.py @@ -54,12 +54,14 @@ class DataMaze: res.update({key: DataMaze.convert_bool(data[key])}) res.update({"OUTPUT_FILE": data["OUTPUT_FILE"]}) res.update( - DataMaze.get_solver_generator(data, res["ENTRY"], res["EXIT"]) + DataMaze.get_solver_generator(data, res["ENTRY"], res["EXIT"], + res["PERFECT"]) ) return res @staticmethod - def get_solver_generator(data: dict, entry: int, exit: int) -> dict: + def get_solver_generator(data: dict, entry: tuple, exit: tuple, + perfect: bool) -> dict: available_generator = { "Kruskal": Kruskal, "DFS": DepthFirstSearch, @@ -68,7 +70,8 @@ class DataMaze: "AStar": AStar, } res = {} - res["GENERATOR"] = available_generator[data["GENERATOR"]]() + res["GENERATOR"] = available_generator[data["GENERATOR"]](entry, exit, + perfect) res["SOLVER"] = available_solver[data["SOLVER"]](entry, exit) return res diff --git a/test.txt b/test.txt index ba65f51..3ea6125 100644 --- a/test.txt +++ b/test.txt @@ -1,34 +1,24 @@ -957D3955553B957917B93D3D15553B -AB97AABBD16847D6ABAAAD452B97C6 -8687A8443EBC55538286E953A807D3 -878506952BAD3B942EAD503E86ED52 -852D4383C6C384438147D2C3879796 -ED2B96EC7D384792EC5792BEC38103 -BBC2C795516C13EC51556C053AAEAA -8452BD43D4396AD552D3BBEBE803EE -853A8552B902BABD5290029696AA93 -C7AC2B9686EE82ABB82EEC07C7E86A -D3AD0683C7D3EAC2C6C3D5693956D2 -9687E92EBBD0103857D43916A85796 -C52BD2A942D2EAE857D542E946D147 -D3EC12EE9692F852FFFBBE96957EBB -B87BAA97852AFED057FC07C507D506 -C2942C47C7EEFFFAFFFD4517ED53C3 -946BABB91553BBFEFD51796955147A -C3BA82AAC53C6AFBFFF816BEBD053A -D06AE828792B90543D568547856D42 -92BC56AAD2C6A87B83916D516BD53A -AAAD13C47AD3AED46EEA95103853C2 -EAAD2815143C43D3BBD0696EAED416 -B847EEC3EBC57AD4407C7C3B85792B -AEBBD5503A9156BD16D1792A8796AA -87AC3BD2C42C57C383D2BEAC47C3EA -C50542B857879112E812C1297BD6BA -D3C3D44417C7EEAC3EAC52C47C516A -BA903B91693B97AB87A938157D5052 -802A82EC5684414287AAEE87953AD2 -EEEEEC57D547D6D6C546D54547C47E - -1,1 -29,29 -SSSSEESSESSSENEEESWSSWSSWSSSSESESSENNNNESEESWSSSWWWSESSSSESWSEENESEEEENNNESESSSEENNNNESEESESSEENEESENNEESW +B91795551793955513D3 +AEC1413969686D396C52 +C392BA869694552C553A +BC6AAAC1296957A917AA +83968692C292956A816A +AAC3C3843C28293AAC3A +E83816ABC3E86AAA83C2 +96AAA92C3C3C56AC6C3A +A96AC2AF87AFFFA9152A +843A96EFC5057FAC2D2A +C7AA853FFFAFFFC3C142 +952AA9697FAFD5145692 +C3AAE83C3FAFFFC153AA +96AC16C383A9515696AA +C3C7C552AAAC3C53C16A +969555106829457A9416 +856953AA946E953843C3 +A93A906E85112BAE96BA +C6AAE81105684685292A +D56C546C4554554546C6 + +1,1 +2,1 +S From 170de8813a43a6d34103f1ba8339af202612778e Mon Sep 17 00:00:00 2001 From: Maoake Teriierooiterai Date: Thu, 26 Mar 2026 13:11:36 +0100 Subject: [PATCH 6/7] update the gitignore --- .gitignore | 1 + test.txt | 48 ++++++++++++++++++++++++------------------------ 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index c855401..4090c3b 100644 --- a/.gitignore +++ b/.gitignore @@ -214,4 +214,5 @@ __marimo__/ # Streamlit .streamlit/secrets.toml +test.txt diff --git a/test.txt b/test.txt index 3ea6125..7cb249a 100644 --- a/test.txt +++ b/test.txt @@ -1,24 +1,24 @@ -B91795551793955513D3 -AEC1413969686D396C52 -C392BA869694552C553A -BC6AAAC1296957A917AA -83968692C292956A816A -AAC3C3843C28293AAC3A -E83816ABC3E86AAA83C2 -96AAA92C3C3C56AC6C3A -A96AC2AF87AFFFA9152A -843A96EFC5057FAC2D2A -C7AA853FFFAFFFC3C142 -952AA9697FAFD5145692 -C3AAE83C3FAFFFC153AA -96AC16C383A9515696AA -C3C7C552AAAC3C53C16A -969555106829457A9416 -856953AA946E953843C3 -A93A906E85112BAE96BA -C6AAE81105684685292A -D56C546C4554554546C6 - -1,1 -2,1 -S +BD155539513D1395393B +A96D13AA96C3AC2BAAC6 +C69146A84396C52C4693 +9380556C7AC5538553AA +AC2C11555693BAA93C6A +8543C29515686AAAE912 +AD3856C3C39696C41686 +C3A853969687C5396BC3 +BC2C3AAF83EFFFA83C52 +A9696AAFEC157FA8293A +84383C6FFFAFFF86AAAA +A96AC13D3FAFD543AAC2 +86D438452FAFFF96AA96 +C553A83B87C553A92AC3 +917A86A8411516A8447A +AC5683AC7801696C3B96 +C393840116C414394683 +BC44696AE93D296E93AA +A95396BC3AC3EA956802 +C4544547C454546D5446 + +1,1 +2,1 +S From ef030f70a7c58fc6138ac91bf85720b211e513da Mon Sep 17 00:00:00 2001 From: Maoake Teriierooiterai Date: Thu, 26 Mar 2026 14:19:43 +0100 Subject: [PATCH 7/7] finish to synchronize the maze generator and the solver --- config.txt | 6 +++--- src/amaz_lib/MazeGenerator.py | 18 ++++++++++++++---- src/amaz_lib/MazeSolver.py | 2 ++ test.txt | 35 +++++++++++++---------------------- 4 files changed, 32 insertions(+), 29 deletions(-) diff --git a/config.txt b/config.txt index b187b15..2c64e03 100644 --- a/config.txt +++ b/config.txt @@ -1,6 +1,6 @@ -WIDTH=20 -HEIGHT=20 -ENTRY=1,1 +WIDTH=11 +HEIGHT=11 +ENTRY=4,3 EXIT=2,1 OUTPUT_FILE=maze.txt PERFECT=False diff --git a/src/amaz_lib/MazeGenerator.py b/src/amaz_lib/MazeGenerator.py index fc4c163..35a160a 100644 --- a/src/amaz_lib/MazeGenerator.py +++ b/src/amaz_lib/MazeGenerator.py @@ -9,7 +9,7 @@ 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 + self.perfect = perfect @abstractmethod def generator( @@ -93,6 +93,7 @@ class MazeGenerator(ABC): class Kruskal(MazeGenerator): + class Set: def __init__(self, cells: list[int]) -> None: self.cells: list[int] = cells @@ -174,6 +175,8 @@ class Kruskal(MazeGenerator): cells_ft = None if height > 10 and width > 10: cells_ft = self.get_cell_ft(width, height) + if cells_ft and (self.start in cells_ft or self.end in cells_ft): + cells_ft = None if seed is not None: np.random.seed(seed) @@ -204,13 +207,20 @@ class Kruskal(MazeGenerator): ): break print(f"nb sets: {len(sets.sets)}") - return self.walls_to_maze(walls, height, width) + maze = self.walls_to_maze(walls, height, width) + if self.perfect is False: + gen = Kruskal.unperfect_maze(width, height, maze, + cells_ft) + for res in gen: + maze = res + yield maze + return maze class DepthFirstSearch(MazeGenerator): def __init__(self, start: bool, end: bool, perfect: bool) -> None: - self.start = start - self.end = end + self.start = (start[0] - 1, start[1] - 1) + self.end = (end[0] - 1, end[1] - 1) self.perfect = perfect self.forty_two: set | None = None diff --git a/src/amaz_lib/MazeSolver.py b/src/amaz_lib/MazeSolver.py index 711b5f4..f5f14fe 100644 --- a/src/amaz_lib/MazeSolver.py +++ b/src/amaz_lib/MazeSolver.py @@ -190,6 +190,8 @@ class DepthFirstSearchSolver(MazeSolver): coord = self.next_cell(coord, next) for m in move: path_str += m + if not path: + raise Exception("Path not found") return path_str @staticmethod diff --git a/test.txt b/test.txt index 7cb249a..11839f6 100644 --- a/test.txt +++ b/test.txt @@ -1,24 +1,15 @@ -BD155539513D1395393B -A96D13AA96C3AC2BAAC6 -C69146A84396C52C4693 -9380556C7AC5538553AA -AC2C11555693BAA93C6A -8543C29515686AAAE912 -AD3856C3C39696C41686 -C3A853969687C5396BC3 -BC2C3AAF83EFFFA83C52 -A9696AAFEC157FA8293A -84383C6FFFAFFF86AAAA -A96AC13D3FAFD543AAC2 -86D438452FAFFF96AA96 -C553A83B87C553A92AC3 -917A86A8411516A8447A -AC5683AC7801696C3B96 -C393840116C414394683 -BC44696AE93D296E93AA -A95396BC3AC3EA956802 -C4544547C454546D5446 +BD1553D3913 +C3AD54386AA +BAC5396C7AA +82956C5396E +A86D553AC53 +C295552C512 +9283B9693AA +AAAAC456AAA +AC2C553D2C2 +83C3D3C3E96 +C454547C547 -1,1 +4,3 2,1 -S +EENWWNNEEESESWSEESSEEESSSSWSWWNWNWWWNNWSSSESWWNWNNNENNNNNW