Rework and some fixes

This commit is contained in:
2026-03-18 14:10:05 +01:00
parent c8a13e9d5c
commit c14c043575
9 changed files with 134 additions and 109 deletions
+1 -1
View File
@@ -11,7 +11,7 @@ clean:
rm -rf __pycache__ .mypy_cache rm -rf __pycache__ .mypy_cache
lint: lint:
uv run flake8 . uv run flake8 . --exclude=.venv
uv run mypy . --warn-return-any --warn-unused-ignores --ignore-missing-imports --disallow-untyped-defs --check-untyped-defs uv run mypy . --warn-return-any --warn-unused-ignores --ignore-missing-imports --disallow-untyped-defs --check-untyped-defs
lint-strict: lint-strict:
+3 -4
View File
@@ -1,12 +1,11 @@
from numpy import ma from src.amaz_lib import MazeGenerator
from src.amaz_lib import kruskal
from src.amaz_lib import Maze from src.amaz_lib import Maze
def main() -> None: def main() -> None:
try: try:
maze = Maze(maze=None) maze = Maze(maze=None, start=(1, 1), end=(16, 15))
for alg in kruskal(10, 10): for alg in MazeGenerator.Kruskal.kruskal(20, 20):
maze.set_maze(alg) maze.set_maze(alg)
maze.export_maze("test.txt") maze.export_maze("test.txt")
except Exception as err: except Exception as err:
-4
View File
@@ -19,7 +19,3 @@ dev = [
[tool.mypy] [tool.mypy]
python_version = "3.10" python_version = "3.10"
exclude = [
".venv",
"venv",
]
+90
View File
@@ -0,0 +1,90 @@
from typing import Generator
import numpy as np
from .classes.Cell import Cell
import math
class MazeGenerator:
class Kruskal:
@staticmethod
def walls_to_maze(
walls: list[tuple[int, int]], height: int, width: int
) -> np.ndarray:
maze: np.ndarray = np.array(
[[Cell(value=0) for _ in range(width)] for _ in range(height)]
)
for wall in walls:
x, y = wall
match y - x:
case 1:
maze[math.trunc((x / width))][x % width].set_est(True)
maze[math.trunc((y / width))][y % width].set_west(True)
case 5:
maze[math.trunc((x / width))][x % width].set_south(
True
)
maze[math.trunc((y / width))][y % width].set_north(
True
)
return maze
@staticmethod
def is_in_same_set(
sets: list[list[int]], wall: tuple[int, int]
) -> bool:
a, b = wall
for set in sets:
if a in set and b in set:
return True
if a in set or b in set:
return False
return False
@staticmethod
def merge_sets(sets: list[list[int]], wall: tuple[int, int]) -> None:
a, b = wall
base_set = None
for set in sets:
if base_set is None and (a in set or b in set):
base_set = set
elif base_set and (a in set or b in set):
base_set += set
sets.remove(set)
@classmethod
def kruskal(
cls, height: int, width: int
) -> Generator[np.ndarray, None, np.ndarray]:
sets = [[i] for i in range(height * width)]
walls = []
for h in range(height):
for w in range(width - 1):
walls += [(w + (width * h), w + (width * h) + 1)]
for w in range(width):
for h in range(height - 1):
walls += [(w + (width * h), w + (width * h) + width)]
np.random.shuffle(walls)
yield cls.walls_to_maze(walls, height, width)
for wall in walls:
if not cls.is_in_same_set(sets, wall):
cls.merge_sets(sets, wall)
walls.remove(wall)
yield cls.walls_to_maze(walls, height, width)
return cls.walls_to_maze(walls, height, width)
def main():
try:
for alg in MazeGenerator.Kruskal.kruskal(10, 10):
maze = alg
# print(maze)
# print()
print(maze)
except GeneratorExit as maze:
print(maze)
if __name__ == "__main__":
main()
+2 -2
View File
@@ -1,7 +1,7 @@
from .classes.Cell import Cell from .classes.Cell import Cell
from .classes.Maze import Maze from .classes.Maze import Maze
from .generators.kruskal import kruskal from .MazeGenerator import MazeGenerator
__version__ = "1.0.0" __version__ = "1.0.0"
__author__ = "us" __author__ = "us"
__all__ = ["Cell", "Maze", "kruskal"] __all__ = ["Cell", "Maze", "MazeGenerator"]
+1 -1
View File
@@ -5,7 +5,7 @@ class Cell(BaseModel):
value: int = Field(ge=0, le=15) value: int = Field(ge=0, le=15)
def __str__(self) -> str: def __str__(self) -> str:
return hex(self.value).removeprefix("0x") return hex(self.value).removeprefix("0x").upper()
def set_value(self, value: int) -> None: def set_value(self, value: int) -> None:
self.value = value self.value = value
+14 -4
View File
@@ -1,15 +1,19 @@
from dataclasses import dataclass from dataclasses import dataclass
import numpy
from .Cell import Cell from .Cell import Cell
@dataclass @dataclass
class Maze: class Maze:
maze: list[list[Cell]] maze: numpy.ndarray
start: tuple[int, int]
end: tuple[int, int]
def get_maze(self) -> list[list[Cell]] | None: def get_maze(self) -> numpy.ndarray | None:
return self.maze return self.maze
def set_maze(self, new_maze: list[list[Cell]]) -> None: def set_maze(self, new_maze: numpy.ndarray) -> None:
self.maze = new_maze self.maze = new_maze
def __str__(self) -> str: def __str__(self) -> str:
@@ -20,8 +24,14 @@ class Maze:
for cell in line: for cell in line:
res += cell.__str__() res += cell.__str__()
res += "\n" res += "\n"
res += "\n"
res += f"{self.start[0]},{self.start[1]}\n"
res += f"{self.end[0]},{self.end[1]}\n"
return res return res
def export_maze(self, file_name: str): def export_maze(self, file_name: str) -> None:
with open(file_name, "w") as file: with open(file_name, "w") as file:
file.write(self.__str__()) file.write(self.__str__())
def solver(self) -> str:
pass
-83
View File
@@ -1,83 +0,0 @@
from typing import Generator
import numpy as np
from ..classes.Cell import Cell
import math
def walls_to_maze(
walls: list[tuple[int, int]],
height: int,
width: int,
) -> np.ndarray:
maze: np.ndarray = np.array(
[[Cell(value=0) for _ in range(width)] for _ in range(height)]
)
for wall in walls:
x, y = wall
match y - x:
case 1:
maze[math.trunc((x / width))][x % width].set_est(True)
maze[math.trunc((y / width))][y % width].set_west(True)
case 5:
maze[math.trunc((x / width))][x % width].set_south(True)
maze[math.trunc((y / width))][y % width].set_north(True)
return maze
def is_in_same_set(sets: list[list[int]], wall: tuple[int, int]) -> bool:
a, b = wall
for set in sets:
if a in set and b in set:
return True
if a in set or b in set:
return False
return False
def merge_sets(sets: list[list[int]], wall: tuple[int, int]) -> None:
a, b = wall
base_set = None
for set in sets:
if base_set is None and (a in set or b in set):
base_set = set
elif base_set and (a in set or b in set):
base_set += set
sets.remove(set)
def kruskal(
height: int, width: int
) -> Generator[np.ndarray, None, np.ndarray]:
sets = [[i] for i in range(height * width)]
walls = []
for h in range(height):
for w in range(width - 1):
walls += [(w + (width * h), w + (width * h) + 1)]
for w in range(width):
for h in range(height - 1):
walls += [(w + (width * h), w + (width * h) + width)]
np.random.shuffle(walls)
yield walls_to_maze(walls, height, width)
for wall in walls:
if not is_in_same_set(sets, wall):
merge_sets(sets, wall)
walls.remove(wall)
yield walls_to_maze(walls, height, width)
return walls_to_maze(walls, height, width)
def main():
try:
for alg in kruskal(10, 10):
maze = alg
# print(maze)
# print()
print(maze)
except GeneratorExit as maze:
print(maze)
if __name__ == "__main__":
main()
+23 -10
View File
@@ -1,10 +1,23 @@
2a80002a80 2AAA802AAAA8282AAAA8
2a80282800 000282A8280282A80028
282aaaa800 002A8280280028280000
2a82a802a8 2AA82A82AA82A82AA800
2802800280 02A8002A8028002A8280
002aaaaa80 282A8282AAA828002AA8
2a80280280 0028282A8282A82A82A8
00282aa828 2A8002AA80002A802A80
002a82aaa8 002802802A8280028280
2aa8000028 0282AA82A82AA80282A8
2802AAA8282AA8282AA8
000002A8028282AAA828
02AAA800002A82A80000
2AAAA82A82AA800282A8
282AA82A8282A82AA828
00002AAA82828282AAA8
02A82A80282802A80028
282A8000280028002AA8
02AA82A82A82AAAAAAA8
2A80000002AAAAA80000
1,1
16,15