2 Commits

Author SHA1 Message Date
Maoake Teriierooiterai f8f0e31598 fix some bug with my unit testing on the DFS 2026-03-23 19:24:32 +01:00
Maoake Teriierooiterai e75e14110d adding my maze need to be tested 2026-03-23 18:49:13 +01:00
5 changed files with 145 additions and 14 deletions
+3
View File
@@ -20,3 +20,6 @@ lint-strict:
run_test_parsing: run_test_parsing:
PYTHONPATH=src uv run pytest tests/test_parsing.py PYTHONPATH=src uv run pytest tests/test_parsing.py
run_test_dfs:
PYTHONPATH=src uv run pytest tests/test_Depth.py
+106 -11
View File
@@ -3,6 +3,7 @@ from typing import Generator
import numpy as np import numpy as np
from .Cell import Cell from .Cell import Cell
import math import math
import random
class MazeGenerator(ABC): class MazeGenerator(ABC):
@@ -84,17 +85,111 @@ class Kruskal(MazeGenerator):
return self.walls_to_maze(walls, height, width) return self.walls_to_maze(walls, height, width)
def main(): class DepthFirstSearch:
try:
for alg in MazeGenerator.Kruskal.kruskal(10, 10):
maze = alg
# print(maze)
# print()
print(maze)
except GeneratorExit as maze: @staticmethod
print(maze) 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__": @staticmethod
main() 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 (x, y - 1) not in visited:
rand_cell.append("N")
# SOUTH
if y + 1 < height and (x, y + 1) not in visited:
rand_cell.append("S")
# WEST
if x - 1 >= 0 and (x - 1, y) not in visited:
rand_cell.append("W")
# EAST
if x + 1 < width and (x + 1, y) 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
+2 -2
View File
@@ -1,8 +1,8 @@
from .Cell import Cell from .Cell import Cell
from .Maze import Maze from .Maze import Maze
from .MazeGenerator import MazeGenerator from .MazeGenerator import MazeGenerator, DepthFirstSearch
from .MazeSolver import MazeSolver from .MazeSolver import MazeSolver
__version__ = "1.0.0" __version__ = "1.0.0"
__author__ = "us" __author__ = "us"
__all__ = ["Cell", "Maze", "MazeGenerator", "MazeSolver"] __all__ = ["Cell", "Maze", "MazeGenerator", "MazeSolver", "DepthFirstSearch"]
+33
View File
@@ -0,0 +1,33 @@
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:
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
+1 -1
View File
@@ -15,7 +15,7 @@ def test_maze_setter_getter() -> None:
) )
maze.set_maze(test) 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: def test_maze_str() -> None: