From e75e14110dfaca6c1e5845e4c2c9605f893523de Mon Sep 17 00:00:00 2001 From: Maoake Teriierooiterai Date: Mon, 23 Mar 2026 18:49:13 +0100 Subject: [PATCH 1/7] adding my maze need to be tested --- Makefile | 3 + src/amaz_lib/MazeGenerator.py | 117 ++++++++++++++++++++++++++++++---- src/amaz_lib/__init__.py | 4 +- tests/test_Depth.py | 17 +++++ tests/test_Maze.py | 2 +- 5 files changed, 129 insertions(+), 14 deletions(-) create mode 100644 tests/test_Depth.py diff --git a/Makefile b/Makefile index 7027e1d..beae4f9 100644 --- a/Makefile +++ b/Makefile @@ -20,3 +20,6 @@ lint-strict: run_test_parsing: PYTHONPATH=src uv run pytest tests/test_parsing.py + +run_test_dfs: + PYTHONPATH=src uv run pytest tests/test_Depth.py diff --git a/src/amaz_lib/MazeGenerator.py b/src/amaz_lib/MazeGenerator.py index 5f2f7a2..0d4e102 100644 --- a/src/amaz_lib/MazeGenerator.py +++ b/src/amaz_lib/MazeGenerator.py @@ -3,6 +3,7 @@ from typing import Generator import numpy as np from .Cell import Cell import math +import random class MazeGenerator(ABC): @@ -84,17 +85,111 @@ class Kruskal(MazeGenerator): return self.walls_to_maze(walls, height, width) -def main(): - try: - for alg in MazeGenerator.Kruskal.kruskal(10, 10): - maze = alg - # print(maze) - # print() - print(maze) +class DepthFirstSearch: - except GeneratorExit as maze: - print(maze) + @staticmethod + def generator(width: int, height: int) -> np.ndarray: + maze = DepthFirstSearch.init_maze(width, height) + visited = [] + path = [] + w_h = (width, height) + coord = (0, 0) + while len(visited) < width * height: + x, y = coord + rand_steps = DepthFirstSearch.random_cells(visited, coord, w_h) + if len(rand_steps) == 0: + path = DepthFirstSearch.back_on_step(path, w_h) + coord = DepthFirstSearch.last(path) + rand_steps = DepthFirstSearch.random_cells(path, coord, w_h) + x, y = coord + wall = DepthFirstSearch.next_step(rand_steps) + wall_r = DepthFirstSearch.reverse_path(wall) + maze[y][x] = DepthFirstSearch.broken_wall(maze[y][x], wall) + visited = DepthFirstSearch.add_cell_visited(coord, visited) + path = DepthFirstSearch.add_cell_visited(coord, path) + coord = DepthFirstSearch.next_cell(x, y, wall) + x, y = coord + maze[y][x] = DepthFirstSearch.broken_wall(maze[y][x], wall_r) + return maze + @staticmethod + def init_maze(width: int, height: int) -> np.ndarray: + maze = np.array([[Cell(value=15) for _ in range(width)] + for _ in range(height)]) + return maze -if __name__ == "__main__": - main() + @staticmethod + def add_cell_visited(coord: tuple, visited: list = []) -> list: + visited.append(coord) + return visited + + @staticmethod + def random_cells(visited: list, coord: tuple, w_h: tuple) -> list: + rand_cell = [] + x, y = coord + width, height = w_h + # NORTH + if y - 1 >= 0 and coord not in visited: + rand_cell.append("N") + + # SOUTH + if y + 1 < height and coord not in visited: + rand_cell.append("S") + + # WEST + if x - 1 >= 0 and coord not in visited: + rand_cell.append("W") + + # EAST + if x + 1 < width and coord not in visited: + rand_cell.append("E") + return rand_cell + + @staticmethod + def next_step(rand_cell: list) -> str: + return random.choice(rand_cell) + + @staticmethod + def broken_wall(cell: Cell, wall: str) -> Cell: + if wall == "N": + cell.set_north(False) + elif wall == "S": + cell.set_south(False) + elif wall == "W": + cell.set_west(False) + elif wall == "E": + cell.set_est(False) + return cell + + @staticmethod + def next_cell(x: int, y: int, next: str) -> tuple: + next_step = { + "N": (0, -1), + "S": (0, 1), + "W": (-1, 0), + "E": (1, 0) + } + add_x, add_y = next_step[next] + return (x + add_x, y + add_y) + + def reverse_path(next: str) -> str: + reverse = { + "N": "S", + "S": "N", + "W": "E", + "E": "W" + } + return reverse[next] + + @staticmethod + def last(path: list): + return path(len(path) - 1) + + def back_on_step(path: list, w_h: tuple) -> list: + last = DepthFirstSearch.last(path) + r_cells = DepthFirstSearch.random_cells(path, last, w_h) + while len(r_cells == 0): + path.pop(len(path) - 1) + last = DepthFirstSearch.last(path) + r_cells = DepthFirstSearch.random_cells(path, last, w_h) + return path diff --git a/src/amaz_lib/__init__.py b/src/amaz_lib/__init__.py index 9a6cf1d..f9ebb1b 100644 --- a/src/amaz_lib/__init__.py +++ b/src/amaz_lib/__init__.py @@ -1,8 +1,8 @@ from .Cell import Cell from .Maze import Maze -from .MazeGenerator import MazeGenerator +from .MazeGenerator import MazeGenerator, DepthFirstSearch from .MazeSolver import MazeSolver __version__ = "1.0.0" __author__ = "us" -__all__ = ["Cell", "Maze", "MazeGenerator", "MazeSolver"] +__all__ = ["Cell", "Maze", "MazeGenerator", "MazeSolver", "DepthFirstSearch"] diff --git a/tests/test_Depth.py b/tests/test_Depth.py new file mode 100644 index 0000000..c7e6eed --- /dev/null +++ b/tests/test_Depth.py @@ -0,0 +1,17 @@ +from amaz_lib.MazeGenerator import DepthFirstSearch +from amaz_lib.Cell import Cell + + +class TestDepth: + + def test_init_maze(self) -> None: + maze = DepthFirstSearch.init_maze(10, 10) + cell = Cell(value=15) + maze[1][1].set_est(False) + assert maze[0][0].value == cell.value + + def test_rand_cells(self) -> None: + maze = DepthFirstSearch.init_maze(10, 10) + lst = DepthFirstSearch.add_cell_visited(maze[0][0]) + rand_cells = DepthFirstSearch.random_cells(lst, maze, 0, 1) + assert len(rand_cells) == 2 diff --git a/tests/test_Maze.py b/tests/test_Maze.py index 7bdf580..6c0ae53 100644 --- a/tests/test_Maze.py +++ b/tests/test_Maze.py @@ -15,7 +15,7 @@ def test_maze_setter_getter() -> None: ) maze.set_maze(test) - assert numpy.array_equal(maze.get_maze(), test) == True + assert numpy.array_equal(maze.get_maze(), test) is True def test_maze_str() -> None: From f8f0e315985d0e962ec5fe2103961eafaa785744 Mon Sep 17 00:00:00 2001 From: Maoake Teriierooiterai Date: Mon, 23 Mar 2026 19:24:32 +0100 Subject: [PATCH 2/7] fix some bug with my unit testing on the DFS --- src/amaz_lib/MazeGenerator.py | 10 +++++----- tests/test_Depth.py | 22 +++++++++++++++++++--- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/amaz_lib/MazeGenerator.py b/src/amaz_lib/MazeGenerator.py index 0d4e102..0e23d75 100644 --- a/src/amaz_lib/MazeGenerator.py +++ b/src/amaz_lib/MazeGenerator.py @@ -129,19 +129,19 @@ class DepthFirstSearch: x, y = coord width, height = w_h # NORTH - if y - 1 >= 0 and coord not in visited: + if y - 1 >= 0 and (x, y - 1) not in visited: rand_cell.append("N") # SOUTH - if y + 1 < height and coord not in visited: + if y + 1 < height and (x, y + 1) not in visited: rand_cell.append("S") # WEST - if x - 1 >= 0 and coord not in visited: + if x - 1 >= 0 and (x - 1, y) not in visited: rand_cell.append("W") # EAST - if x + 1 < width and coord not in visited: + if x + 1 < width and (x + 1, y) not in visited: rand_cell.append("E") return rand_cell @@ -183,7 +183,7 @@ class DepthFirstSearch: @staticmethod def last(path: list): - return path(len(path) - 1) + return path[len(path) - 1] def back_on_step(path: list, w_h: tuple) -> list: last = DepthFirstSearch.last(path) diff --git a/tests/test_Depth.py b/tests/test_Depth.py index c7e6eed..8933db8 100644 --- a/tests/test_Depth.py +++ b/tests/test_Depth.py @@ -11,7 +11,23 @@ class TestDepth: assert maze[0][0].value == cell.value def test_rand_cells(self) -> None: - maze = DepthFirstSearch.init_maze(10, 10) - lst = DepthFirstSearch.add_cell_visited(maze[0][0]) - rand_cells = DepthFirstSearch.random_cells(lst, maze, 0, 1) + w_h = (10, 10) + lst = DepthFirstSearch.add_cell_visited((0, 0)) + rand_cells = DepthFirstSearch.random_cells(lst, (0, 1), w_h) assert len(rand_cells) == 2 + + def test_next_cell(self) -> None: + coord = (5, 4) + x, y = coord + assert DepthFirstSearch.next_cell(x, y, "N") == (5, 3) + + def test_reverse_path(self) -> None: + assert DepthFirstSearch.reverse_path("N") == "S" + + def test_last(self) -> None: + lst = [(0, 0), (1, 1)] + assert DepthFirstSearch.last(lst) == (1, 1) + + def test_BOS(self) -> None: + path = [(0, 0), (0, 2), (1, 1)] + assert len(DepthFirstSearch.random_cells(path, (0, 1), (10, 10))) == 0 From 030c6142bafe145464d7f4feb142069b693611a5 Mon Sep 17 00:00:00 2001 From: Maoake Teriierooiterai Date: Tue, 24 Mar 2026 09:34:53 +0100 Subject: [PATCH 3/7] need to fix my infinite while so i make a checkpoint if i need to restore it --- a_maze_ing.py | 8 +++++-- src/amaz_lib/MazeGenerator.py | 45 ++++++++++++++++++++++------------- tests/test_Depth.py | 14 ++++++----- 3 files changed, 42 insertions(+), 25 deletions(-) diff --git a/a_maze_ing.py b/a_maze_ing.py index cfb67f2..63e4432 100644 --- a/a_maze_ing.py +++ b/a_maze_ing.py @@ -1,6 +1,6 @@ import os -from numpy import ma from src.amaz_lib import MazeGenerator +from src.amaz_lib import DepthFirstSearch from src.amaz_lib import Maze @@ -14,9 +14,13 @@ def main() -> None: maze.export_maze("test.txt") +def main2() -> None: + DepthFirstSearch.generator(5, 5) + + # except Exception as err: # print(err) if __name__ == "__main__": - main() + main2() diff --git a/src/amaz_lib/MazeGenerator.py b/src/amaz_lib/MazeGenerator.py index 0e23d75..4240759 100644 --- a/src/amaz_lib/MazeGenerator.py +++ b/src/amaz_lib/MazeGenerator.py @@ -90,17 +90,22 @@ class DepthFirstSearch: @staticmethod def generator(width: int, height: int) -> np.ndarray: maze = DepthFirstSearch.init_maze(width, height) - visited = [] - path = [] + visited = set() + path = list() w_h = (width, height) coord = (0, 0) while len(visited) < width * height: + print(f"visited {len(visited)}") x, y = coord rand_steps = DepthFirstSearch.random_cells(visited, coord, w_h) if len(rand_steps) == 0: - path = DepthFirstSearch.back_on_step(path, w_h) - coord = DepthFirstSearch.last(path) + path = DepthFirstSearch.back_on_step(path, w_h, visited) + coord = path[-1] rand_steps = DepthFirstSearch.random_cells(path, coord, w_h) + print(f"coord {coord}") + print(f"visited = {visited}") + print(f" rand steps {rand_steps}") + print(f"path = {path}") x, y = coord wall = DepthFirstSearch.next_step(rand_steps) wall_r = DepthFirstSearch.reverse_path(wall) @@ -108,6 +113,7 @@ class DepthFirstSearch: visited = DepthFirstSearch.add_cell_visited(coord, visited) path = DepthFirstSearch.add_cell_visited(coord, path) coord = DepthFirstSearch.next_cell(x, y, wall) + print(f"coord 2 {coord}") x, y = coord maze[y][x] = DepthFirstSearch.broken_wall(maze[y][x], wall_r) return maze @@ -119,12 +125,15 @@ class DepthFirstSearch: return maze @staticmethod - def add_cell_visited(coord: tuple, visited: list = []) -> list: - visited.append(coord) + def add_cell_visited(coord: tuple, visited: list | set) -> list | set: + if isinstance(visited, list): + visited.append(coord) + if isinstance(visited, set): + visited.add(coord) return visited @staticmethod - def random_cells(visited: list, coord: tuple, w_h: tuple) -> list: + def random_cells(visited: set, coord: tuple, w_h: tuple) -> list: rand_cell = [] x, y = coord width, height = w_h @@ -172,6 +181,7 @@ class DepthFirstSearch: add_x, add_y = next_step[next] return (x + add_x, y + add_y) + @staticmethod def reverse_path(next: str) -> str: reverse = { "N": "S", @@ -182,14 +192,15 @@ class DepthFirstSearch: return reverse[next] @staticmethod - def last(path: list): - return path[len(path) - 1] - - def back_on_step(path: list, w_h: tuple) -> list: - last = DepthFirstSearch.last(path) - r_cells = DepthFirstSearch.random_cells(path, last, w_h) - while len(r_cells == 0): - path.pop(len(path) - 1) - last = DepthFirstSearch.last(path) - r_cells = DepthFirstSearch.random_cells(path, last, w_h) + def back_on_step(path: list, w_h: tuple, visited: set) -> list: + last = path[-1] + r_cells = DepthFirstSearch.random_cells(visited, last, w_h) + while len(path) > 0: + print(f"path {len(path)}") + path.pop() + last = path[-1] + r_cells = DepthFirstSearch.random_cells(visited, last, w_h) + if r_cells: + print(f"cells {len(r_cells)}") + break return path diff --git a/tests/test_Depth.py b/tests/test_Depth.py index 8933db8..31f4624 100644 --- a/tests/test_Depth.py +++ b/tests/test_Depth.py @@ -1,5 +1,6 @@ from amaz_lib.MazeGenerator import DepthFirstSearch from amaz_lib.Cell import Cell +import numpy as np class TestDepth: @@ -12,7 +13,7 @@ class TestDepth: def test_rand_cells(self) -> None: w_h = (10, 10) - lst = DepthFirstSearch.add_cell_visited((0, 0)) + lst = DepthFirstSearch.add_cell_visited((0, 0), set()) rand_cells = DepthFirstSearch.random_cells(lst, (0, 1), w_h) assert len(rand_cells) == 2 @@ -24,10 +25,11 @@ class TestDepth: def test_reverse_path(self) -> None: assert DepthFirstSearch.reverse_path("N") == "S" - def test_last(self) -> None: - lst = [(0, 0), (1, 1)] - assert DepthFirstSearch.last(lst) == (1, 1) - def test_BOS(self) -> None: - path = [(0, 0), (0, 2), (1, 1)] + path = {(0, 0), (0, 2), (1, 1)} assert len(DepthFirstSearch.random_cells(path, (0, 1), (10, 10))) == 0 + + def test_generator(self): + maze = np.array([]) + maze = DepthFirstSearch.generator(10, 10) + assert maze.shape == (10, 10) From 8b4ef7afcea1302b60d168cade1334a7c3bc04a8 Mon Sep 17 00:00:00 2001 From: Maoake Teriierooiterai Date: Tue, 24 Mar 2026 11:10:16 +0100 Subject: [PATCH 4/7] finish the maze generator --- Makefile | 3 +++ src/amaz_lib/MazeGenerator.py | 33 ++++++++++++++------------------- tests/test_MazeGenerator.py | 21 ++++++++++++++------- 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index beae4f9..c29a4c8 100644 --- a/Makefile +++ b/Makefile @@ -23,3 +23,6 @@ run_test_parsing: run_test_dfs: PYTHONPATH=src uv run pytest tests/test_Depth.py + +run_test_maze_gen: + PYTHONPATH=src uv run pytest tests/test_MazeGenerator.py diff --git a/src/amaz_lib/MazeGenerator.py b/src/amaz_lib/MazeGenerator.py index 4240759..daeede7 100644 --- a/src/amaz_lib/MazeGenerator.py +++ b/src/amaz_lib/MazeGenerator.py @@ -94,26 +94,22 @@ class DepthFirstSearch: path = list() w_h = (width, height) coord = (0, 0) + x, y = coord + while len(visited) < width * height: - print(f"visited {len(visited)}") - x, y = coord - rand_steps = DepthFirstSearch.random_cells(visited, coord, w_h) - if len(rand_steps) == 0: - path = DepthFirstSearch.back_on_step(path, w_h, visited) - coord = path[-1] - rand_steps = DepthFirstSearch.random_cells(path, coord, w_h) - print(f"coord {coord}") - print(f"visited = {visited}") - print(f" rand steps {rand_steps}") - print(f"path = {path}") - x, y = coord - wall = DepthFirstSearch.next_step(rand_steps) - wall_r = DepthFirstSearch.reverse_path(wall) - maze[y][x] = DepthFirstSearch.broken_wall(maze[y][x], wall) visited = DepthFirstSearch.add_cell_visited(coord, visited) path = DepthFirstSearch.add_cell_visited(coord, path) + random_c = DepthFirstSearch.random_cells(visited, coord, w_h) + if len(random_c) == 0: + path = DepthFirstSearch.back_on_step(path, w_h, visited) + if path: + coord = path[-1] + random_c = DepthFirstSearch.random_cells(path, coord, w_h) + x, y = coord + wall = DepthFirstSearch.next_step(random_c) + maze[y][x] = DepthFirstSearch.broken_wall(maze[y][x], wall) coord = DepthFirstSearch.next_cell(x, y, wall) - print(f"coord 2 {coord}") + wall_r = DepthFirstSearch.reverse_path(wall) x, y = coord maze[y][x] = DepthFirstSearch.broken_wall(maze[y][x], wall_r) return maze @@ -196,11 +192,10 @@ class DepthFirstSearch: last = path[-1] r_cells = DepthFirstSearch.random_cells(visited, last, w_h) while len(path) > 0: - print(f"path {len(path)}") path.pop() - last = path[-1] + if path: + last = path[-1] r_cells = DepthFirstSearch.random_cells(visited, last, w_h) if r_cells: - print(f"cells {len(r_cells)}") break return path diff --git a/tests/test_MazeGenerator.py b/tests/test_MazeGenerator.py index cb0aa6b..a9bf22b 100644 --- a/tests/test_MazeGenerator.py +++ b/tests/test_MazeGenerator.py @@ -1,11 +1,18 @@ import numpy -from amaz_lib.MazeGenerator import Kruskal +from amaz_lib.MazeGenerator import DepthFirstSearch -def test_kruskal_output_shape() -> None: - generator = Kruskal() - maze = numpy.array([]) - for output in generator.generator(10, 10): - maze = output +class TestMazeGenerator: - assert maze.shape == (10, 10) + # def test_kruskal_output_shape() -> None: + # generator = Kruskal() + # maze = numpy.array([]) + # for output in generator.generator(10, 10): + # maze = output + + # assert maze.shape == (10, 10) + + def test_generator(self): + maze = numpy.array([]) + maze = DepthFirstSearch.generator(10, 10) + assert maze.shape == (10, 10) From 4d151664abc8113874e9ec9a3899af0a08d3e7dd Mon Sep 17 00:00:00 2001 From: Maoake Teriierooiterai Date: Tue, 24 Mar 2026 14:28:10 +0100 Subject: [PATCH 5/7] finish the generator DFS --- a_maze_ing.py | 14 ++++++++---- src/amaz_lib/MazeGenerator.py | 43 +++++++++++++++++------------------ src/amaz_lib/__init__.py | 4 +++- tests/test_Depth.py | 14 +++--------- tests/test_MazeGenerator.py | 15 ++++++------ 5 files changed, 44 insertions(+), 46 deletions(-) diff --git a/a_maze_ing.py b/a_maze_ing.py index 63e4432..d127115 100644 --- a/a_maze_ing.py +++ b/a_maze_ing.py @@ -1,21 +1,25 @@ import os -from src.amaz_lib import MazeGenerator +from src.amaz_lib import Kruskal from src.amaz_lib import DepthFirstSearch from src.amaz_lib import Maze def main() -> None: # try: - maze = Maze(maze=None, start=(1, 1), end=(16, 15)) - for alg in MazeGenerator.Kruskal.kruskal(20, 20): + maze = Maze(maze=None) + gen = Kruskal().generator(10, 10) + for alg in gen: maze.set_maze(alg) os.system("clear") maze.ascii_print() - maze.export_maze("test.txt") def main2() -> None: - DepthFirstSearch.generator(5, 5) + maze = Maze(maze=None) + gen = DepthFirstSearch.generator(50, 50) + maze.set_maze(gen) + os.system("clear") + maze.ascii_print() # except Exception as err: diff --git a/src/amaz_lib/MazeGenerator.py b/src/amaz_lib/MazeGenerator.py index daeede7..fdd3a15 100644 --- a/src/amaz_lib/MazeGenerator.py +++ b/src/amaz_lib/MazeGenerator.py @@ -3,7 +3,6 @@ from typing import Generator import numpy as np from .Cell import Cell import math -import random class MazeGenerator(ABC): @@ -90,24 +89,30 @@ class DepthFirstSearch: @staticmethod def generator(width: int, height: int) -> np.ndarray: maze = DepthFirstSearch.init_maze(width, height) - visited = set() + visited = np.zeros((height, width), dtype=bool) path = list() w_h = (width, height) coord = (0, 0) x, y = coord + first = True - while len(visited) < width * height: - visited = DepthFirstSearch.add_cell_visited(coord, visited) + while path or first: + first = False + visited[y, x] = True path = DepthFirstSearch.add_cell_visited(coord, path) random_c = DepthFirstSearch.random_cells(visited, coord, w_h) if len(random_c) == 0: path = DepthFirstSearch.back_on_step(path, w_h, visited) if path: coord = path[-1] - random_c = DepthFirstSearch.random_cells(path, coord, w_h) + random_c = DepthFirstSearch.random_cells(visited, coord, w_h) x, y = coord + if not path: + break + wall = DepthFirstSearch.next_step(random_c) maze[y][x] = DepthFirstSearch.broken_wall(maze[y][x], wall) + coord = DepthFirstSearch.next_cell(x, y, wall) wall_r = DepthFirstSearch.reverse_path(wall) x, y = coord @@ -121,38 +126,32 @@ class DepthFirstSearch: return maze @staticmethod - def add_cell_visited(coord: tuple, visited: list | set) -> list | set: - if isinstance(visited, list): - visited.append(coord) - if isinstance(visited, set): - visited.add(coord) - return visited + def add_cell_visited(coord: tuple, path: set) -> list: + path.append(coord) + return path @staticmethod - def random_cells(visited: set, coord: tuple, w_h: tuple) -> list: + def random_cells(visited: np.array, coord: tuple, w_h: tuple) -> list: rand_cell = [] x, y = coord width, height = w_h - # NORTH - if y - 1 >= 0 and (x, y - 1) not in visited: + + if y - 1 >= 0 and not visited[y - 1][x]: rand_cell.append("N") - # SOUTH - if y + 1 < height and (x, y + 1) not in visited: + if y + 1 < height and not visited[y + 1][x]: rand_cell.append("S") - # WEST - if x - 1 >= 0 and (x - 1, y) not in visited: + if x - 1 >= 0 and not visited[y][x - 1]: rand_cell.append("W") - # EAST - if x + 1 < width and (x + 1, y) not in visited: + if x + 1 < width and not visited[y][x + 1]: rand_cell.append("E") return rand_cell @staticmethod def next_step(rand_cell: list) -> str: - return random.choice(rand_cell) + return np.random.choice(rand_cell) @staticmethod def broken_wall(cell: Cell, wall: str) -> Cell: @@ -188,7 +187,7 @@ class DepthFirstSearch: return reverse[next] @staticmethod - def back_on_step(path: list, w_h: tuple, visited: set) -> list: + def back_on_step(path: list, w_h: tuple, visited: np.array) -> list: last = path[-1] r_cells = DepthFirstSearch.random_cells(visited, last, w_h) while len(path) > 0: diff --git a/src/amaz_lib/__init__.py b/src/amaz_lib/__init__.py index f9ebb1b..d772717 100644 --- a/src/amaz_lib/__init__.py +++ b/src/amaz_lib/__init__.py @@ -1,8 +1,10 @@ from .Cell import Cell from .Maze import Maze from .MazeGenerator import MazeGenerator, DepthFirstSearch +from .MazeGenerator import Kruskal from .MazeSolver import MazeSolver __version__ = "1.0.0" __author__ = "us" -__all__ = ["Cell", "Maze", "MazeGenerator", "MazeSolver", "DepthFirstSearch"] +__all__ = ["Cell", "Maze", "MazeGenerator", + "MazeSolver", "DepthFirstSearch", "Kruskal"] diff --git a/tests/test_Depth.py b/tests/test_Depth.py index 31f4624..32a6dad 100644 --- a/tests/test_Depth.py +++ b/tests/test_Depth.py @@ -13,23 +13,15 @@ class TestDepth: def test_rand_cells(self) -> None: w_h = (10, 10) - lst = DepthFirstSearch.add_cell_visited((0, 0), set()) + lst = np.zeros((10, 10), dtype=bool) + lst[0, 0] = True rand_cells = DepthFirstSearch.random_cells(lst, (0, 1), w_h) assert len(rand_cells) == 2 def test_next_cell(self) -> None: coord = (5, 4) x, y = coord - assert DepthFirstSearch.next_cell(x, y, "N") == (5, 3) + assert DepthFirstSearch.next_cell(x, y, "N") == (2, 3) def test_reverse_path(self) -> None: assert DepthFirstSearch.reverse_path("N") == "S" - - def test_BOS(self) -> None: - path = {(0, 0), (0, 2), (1, 1)} - assert len(DepthFirstSearch.random_cells(path, (0, 1), (10, 10))) == 0 - - def test_generator(self): - maze = np.array([]) - maze = DepthFirstSearch.generator(10, 10) - assert maze.shape == (10, 10) diff --git a/tests/test_MazeGenerator.py b/tests/test_MazeGenerator.py index a9bf22b..1c1e868 100644 --- a/tests/test_MazeGenerator.py +++ b/tests/test_MazeGenerator.py @@ -1,18 +1,19 @@ import numpy from amaz_lib.MazeGenerator import DepthFirstSearch +from amaz_lib.MazeGenerator import Kruskal class TestMazeGenerator: - # def test_kruskal_output_shape() -> None: - # generator = Kruskal() - # maze = numpy.array([]) - # for output in generator.generator(10, 10): - # maze = output + def test_kruskal_output_shape() -> None: + generator = Kruskal().generator(10, 10) + maze = numpy.array([]) + for output in generator: + maze = output - # assert maze.shape == (10, 10) + assert maze.shape == (10, 10) - def test_generator(self): + def test_generator(self) -> None: maze = numpy.array([]) maze = DepthFirstSearch.generator(10, 10) assert maze.shape == (10, 10) From 993bcce85774150efb6cb9a80695fbeea21fb6dd Mon Sep 17 00:00:00 2001 From: Maoake Teriierooiterai Date: Tue, 24 Mar 2026 15:22:20 +0100 Subject: [PATCH 6/7] add generator to my maze generator DFS --- a_maze_ing.py | 19 +++++-------------- src/amaz_lib/MazeGenerator.py | 7 ++++--- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/a_maze_ing.py b/a_maze_ing.py index d127115..8df2f40 100644 --- a/a_maze_ing.py +++ b/a_maze_ing.py @@ -1,30 +1,21 @@ 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 MazeGenerator +import src.amaz_lib as g -def main() -> None: +def main(maze_gen: MazeGenerator) -> None: # try: maze = Maze(maze=None) - gen = Kruskal().generator(10, 10) + gen = maze_gen.generator(100, 100) for alg in gen: maze.set_maze(alg) os.system("clear") 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: # print(err) if __name__ == "__main__": - main2() + main(g.DepthFirstSearch()) diff --git a/src/amaz_lib/MazeGenerator.py b/src/amaz_lib/MazeGenerator.py index fdd3a15..feb39e6 100644 --- a/src/amaz_lib/MazeGenerator.py +++ b/src/amaz_lib/MazeGenerator.py @@ -84,10 +84,10 @@ class Kruskal(MazeGenerator): return self.walls_to_maze(walls, height, width) -class DepthFirstSearch: +class DepthFirstSearch(MazeGenerator): - @staticmethod - def generator(width: int, height: int) -> np.ndarray: + def generator(self, width: int, height: int + ) -> Generator[np.ndarray, None, np.ndarray]: maze = DepthFirstSearch.init_maze(width, height) visited = np.zeros((height, width), dtype=bool) path = list() @@ -117,6 +117,7 @@ class DepthFirstSearch: wall_r = DepthFirstSearch.reverse_path(wall) x, y = coord maze[y][x] = DepthFirstSearch.broken_wall(maze[y][x], wall_r) + yield maze return maze @staticmethod From c478400640364976d4f3c07fbf56051703e7d4b2 Mon Sep 17 00:00:00 2001 From: Maoake Teriierooiterai Date: Tue, 24 Mar 2026 15:34:43 +0100 Subject: [PATCH 7/7] fix the cell pydantic cause the program was too long --- src/amaz_lib/Cell.py | 8 +++++--- tests/test_MazeGenerator.py | 13 ++++--------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/amaz_lib/Cell.py b/src/amaz_lib/Cell.py index 56f0c61..cc205ab 100644 --- a/src/amaz_lib/Cell.py +++ b/src/amaz_lib/Cell.py @@ -1,8 +1,10 @@ -from pydantic import BaseModel, Field +from dataclasses import dataclass -class Cell(BaseModel): - value: int = Field(ge=0, le=15) +@dataclass +class Cell: + def __init__(self, value: int) -> None: + self.value = value def __str__(self) -> str: return hex(self.value).removeprefix("0x").upper() diff --git a/tests/test_MazeGenerator.py b/tests/test_MazeGenerator.py index 1c1e868..948748a 100644 --- a/tests/test_MazeGenerator.py +++ b/tests/test_MazeGenerator.py @@ -1,19 +1,14 @@ import numpy from amaz_lib.MazeGenerator import DepthFirstSearch -from amaz_lib.MazeGenerator import Kruskal class TestMazeGenerator: - def test_kruskal_output_shape() -> None: - generator = Kruskal().generator(10, 10) + def test_generator(self) -> None: + w_h = (300, 300) maze = numpy.array([]) + generator = DepthFirstSearch().generator(*w_h) for output in generator: maze = output - assert maze.shape == (10, 10) - - def test_generator(self) -> None: - maze = numpy.array([]) - maze = DepthFirstSearch.generator(10, 10) - assert maze.shape == (10, 10) + assert maze.shape == w_h