finish the generator DFS

This commit is contained in:
Maoake Teriierooiterai
2026-03-24 14:28:10 +01:00
parent 8b4ef7afce
commit 4d151664ab
5 changed files with 44 additions and 46 deletions
+9 -5
View File
@@ -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:
+21 -22
View File
@@ -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:
+3 -1
View File
@@ -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"]
+3 -11
View File
@@ -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)
+8 -7
View File
@@ -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)