need to fix my infinite while so i make a checkpoint if i need to restore it

This commit is contained in:
Maoake Teriierooiterai
2026-03-24 09:34:53 +01:00
parent f8f0e31598
commit 030c6142ba
3 changed files with 42 additions and 25 deletions
+6 -2
View File
@@ -1,6 +1,6 @@
import os import os
from numpy import ma
from src.amaz_lib import MazeGenerator from src.amaz_lib import MazeGenerator
from src.amaz_lib import DepthFirstSearch
from src.amaz_lib import Maze from src.amaz_lib import Maze
@@ -14,9 +14,13 @@ def main() -> None:
maze.export_maze("test.txt") maze.export_maze("test.txt")
def main2() -> None:
DepthFirstSearch.generator(5, 5)
# except Exception as err: # except Exception as err:
# print(err) # print(err)
if __name__ == "__main__": if __name__ == "__main__":
main() main2()
+28 -17
View File
@@ -90,17 +90,22 @@ class DepthFirstSearch:
@staticmethod @staticmethod
def generator(width: int, height: int) -> np.ndarray: def generator(width: int, height: int) -> np.ndarray:
maze = DepthFirstSearch.init_maze(width, height) maze = DepthFirstSearch.init_maze(width, height)
visited = [] visited = set()
path = [] path = list()
w_h = (width, height) w_h = (width, height)
coord = (0, 0) coord = (0, 0)
while len(visited) < width * height: while len(visited) < width * height:
print(f"visited {len(visited)}")
x, y = coord x, y = coord
rand_steps = DepthFirstSearch.random_cells(visited, coord, w_h) rand_steps = DepthFirstSearch.random_cells(visited, coord, w_h)
if len(rand_steps) == 0: if len(rand_steps) == 0:
path = DepthFirstSearch.back_on_step(path, w_h) path = DepthFirstSearch.back_on_step(path, w_h, visited)
coord = DepthFirstSearch.last(path) coord = path[-1]
rand_steps = DepthFirstSearch.random_cells(path, coord, w_h) 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 x, y = coord
wall = DepthFirstSearch.next_step(rand_steps) wall = DepthFirstSearch.next_step(rand_steps)
wall_r = DepthFirstSearch.reverse_path(wall) wall_r = DepthFirstSearch.reverse_path(wall)
@@ -108,6 +113,7 @@ class DepthFirstSearch:
visited = DepthFirstSearch.add_cell_visited(coord, visited) visited = DepthFirstSearch.add_cell_visited(coord, visited)
path = DepthFirstSearch.add_cell_visited(coord, path) path = DepthFirstSearch.add_cell_visited(coord, path)
coord = DepthFirstSearch.next_cell(x, y, wall) coord = DepthFirstSearch.next_cell(x, y, wall)
print(f"coord 2 {coord}")
x, y = coord x, y = coord
maze[y][x] = DepthFirstSearch.broken_wall(maze[y][x], wall_r) maze[y][x] = DepthFirstSearch.broken_wall(maze[y][x], wall_r)
return maze return maze
@@ -119,12 +125,15 @@ class DepthFirstSearch:
return maze return maze
@staticmethod @staticmethod
def add_cell_visited(coord: tuple, visited: list = []) -> list: def add_cell_visited(coord: tuple, visited: list | set) -> list | set:
visited.append(coord) if isinstance(visited, list):
visited.append(coord)
if isinstance(visited, set):
visited.add(coord)
return visited return visited
@staticmethod @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 = [] rand_cell = []
x, y = coord x, y = coord
width, height = w_h width, height = w_h
@@ -172,6 +181,7 @@ class DepthFirstSearch:
add_x, add_y = next_step[next] add_x, add_y = next_step[next]
return (x + add_x, y + add_y) return (x + add_x, y + add_y)
@staticmethod
def reverse_path(next: str) -> str: def reverse_path(next: str) -> str:
reverse = { reverse = {
"N": "S", "N": "S",
@@ -182,14 +192,15 @@ class DepthFirstSearch:
return reverse[next] return reverse[next]
@staticmethod @staticmethod
def last(path: list): def back_on_step(path: list, w_h: tuple, visited: set) -> list:
return path[len(path) - 1] last = path[-1]
r_cells = DepthFirstSearch.random_cells(visited, last, w_h)
def back_on_step(path: list, w_h: tuple) -> list: while len(path) > 0:
last = DepthFirstSearch.last(path) print(f"path {len(path)}")
r_cells = DepthFirstSearch.random_cells(path, last, w_h) path.pop()
while len(r_cells == 0): last = path[-1]
path.pop(len(path) - 1) r_cells = DepthFirstSearch.random_cells(visited, last, w_h)
last = DepthFirstSearch.last(path) if r_cells:
r_cells = DepthFirstSearch.random_cells(path, last, w_h) print(f"cells {len(r_cells)}")
break
return path return path
+8 -6
View File
@@ -1,5 +1,6 @@
from amaz_lib.MazeGenerator import DepthFirstSearch from amaz_lib.MazeGenerator import DepthFirstSearch
from amaz_lib.Cell import Cell from amaz_lib.Cell import Cell
import numpy as np
class TestDepth: class TestDepth:
@@ -12,7 +13,7 @@ class TestDepth:
def test_rand_cells(self) -> None: def test_rand_cells(self) -> None:
w_h = (10, 10) 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) rand_cells = DepthFirstSearch.random_cells(lst, (0, 1), w_h)
assert len(rand_cells) == 2 assert len(rand_cells) == 2
@@ -24,10 +25,11 @@ class TestDepth:
def test_reverse_path(self) -> None: def test_reverse_path(self) -> None:
assert DepthFirstSearch.reverse_path("N") == "S" 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: 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 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)