add output to Maze class

This commit is contained in:
2026-03-18 12:14:10 +01:00
parent 272ccefb52
commit c8a13e9d5c
7 changed files with 73 additions and 13 deletions
+12 -1
View File
@@ -1,5 +1,16 @@
from numpy import ma
from src.amaz_lib import kruskal
from src.amaz_lib import Maze
def main() -> None:
print("A-Maze-ing !!!")
try:
maze = Maze(maze=None)
for alg in kruskal(10, 10):
maze.set_maze(alg)
maze.export_maze("test.txt")
except Exception as err:
print(err)
if __name__ == "__main__":
+25
View File
@@ -0,0 +1,25 @@
# This script does not check for errors or malformed files.
# It only validates that neighbooring cells sharing a wall have
# both the correct encoding.
# Usage: python3 output_validator.py output_maze.txt
import sys
if len(sys.argv) != 2:
print(f"Usage: python3 {sys.argv[0]} <output_file>")
sys.exit(1)
g = []
for line in open(sys.argv[1]):
if line.strip() == '':
break
g.append([int(c, 16) for c in line.strip(' \t\n\r')])
for r in range(len(g)):
for c in range(len(g[0])):
v = g[r][c]
if not all([(r < 1 or v & 1 == (g[r-1][c] >> 2) & 1),
(c >= len(g[0])-1 or (v >> 1) & 1 == (g[r][c+1] >> 3) & 1),
(r >= len(g)-1 or (v >> 2) & 1 == g[r+1][c] & 1),
(c < 1 or (v >> 3) & 1 == (g[r][c-1] >> 1) & 1)]):
print(f'Wrong encoding for ({c},{r})')
+3 -1
View File
@@ -1,5 +1,7 @@
from .classes.Cell import Cell
from .classes.Maze import Maze
from .generators.kruskal import kruskal
__version__ = "1.0.0"
__author__ = "us"
__all__ = ["Cell"]
__all__ = ["Cell", "Maze", "kruskal"]
+1 -1
View File
@@ -5,7 +5,7 @@ class Cell(BaseModel):
value: int = Field(ge=0, le=15)
def __str__(self) -> str:
return hex(self.value)
return hex(self.value).removeprefix("0x")
def set_value(self, value: int) -> None:
self.value = value
+20 -8
View File
@@ -1,15 +1,27 @@
from sys import stdout
import numpy as np
from pydantic import BaseModel
from dataclasses import dataclass
from .Cell import Cell
class Maze(BaseModel):
maze: np.ndarray
@dataclass
class Maze:
maze: list[list[Cell]]
def get_maze(self) -> list[list[Cell]] | None:
return self.maze
def set_maze(self, new_maze: list[list[Cell]]) -> None:
self.maze = new_maze
def __str__(self) -> str:
if self.maze is None:
return "None"
res = ""
for _ in self.maze:
for cell in self.maze:
res += cell
for line in self.maze:
for cell in line:
res += cell.__str__()
res += "\n"
return res
def export_maze(self, file_name: str):
with open(file_name, "w") as file:
file.write(self.__str__())
+2 -2
View File
@@ -45,7 +45,7 @@ def merge_sets(sets: list[list[int]], wall: tuple[int, int]) -> None:
sets.remove(set)
def kraskal(
def kruskal(
height: int, width: int
) -> Generator[np.ndarray, None, np.ndarray]:
sets = [[i] for i in range(height * width)]
@@ -69,7 +69,7 @@ def kraskal(
def main():
try:
for alg in kraskal(10, 10):
for alg in kruskal(10, 10):
maze = alg
# print(maze)
# print()
+10
View File
@@ -0,0 +1,10 @@
2a80002a80
2a80282800
282aaaa800
2a82a802a8
2802800280
002aaaaa80
2a80280280
00282aa828
002a82aaa8
2aa8000028