From: Lucas Flores Date: Fri, 27 Mar 2026 16:39:36 +0000 (+0100) Subject: Added output.py - format_output(maze) returns a str representation of maze, exits... X-Git-Url: https://git.uwuaxy.net/animations_scrolling.mp4?a=commitdiff_plain;h=bd6a6d22672e409f5fbed898c26d86d984b15e6e;p=axy%2Fft%2Fa-maze-ing.git Added output.py - format_output(maze) returns a str representation of maze, exits and path --- diff --git a/a_maze_ing.py b/a_maze_ing.py index 820f9a1..fcc26db 100644 --- a/a_maze_ing.py +++ b/a_maze_ing.py @@ -9,6 +9,7 @@ from mazegen.maze import ( make_perfect, ) from mazegen.config.config_parser import Config +from mazegen.maze.output import format_maze import random config = Config.parse(open("./example.conf").read()) diff --git a/mazegen/maze/output.py b/mazegen/maze/output.py new file mode 100644 index 0000000..266510f --- /dev/null +++ b/mazegen/maze/output.py @@ -0,0 +1,44 @@ +from .maze import Maze +from mazegen.utils import CellCoord, Cardinal +from mazegen.maze.path import pathfind_astar + + +def to_hex(cell: list[bool]): + val = ( + 1 + if cell[0] + else ( + 0 + 2 if cell[1] else 0 + 4 if cell[2] else 0 + 8 if cell[3] else 0 + ) + ) + return "0123456789ABCDEF"[val] + + +def format_maze(maze: Maze) -> str: + dims = maze.dims + maze_str = "" + for y in range(dims.y): + for x in range(dims.x): + cell = CellCoord(x, y) + n, e, s, w = map( + lambda card: maze.get_wall(cell.get_wall(card)), + [Cardinal.NORTH, Cardinal.EAST, Cardinal.SOUTH, Cardinal.WEST], + ) + maze_str += to_hex([n, e, s, w]) + maze_str += "\n" + return maze_str + + +def format_doors(maze: Maze) -> str: + entry = f"{maze.entry.x},{maze.entry.y}\n" + exit = f"{maze.exit.x},{maze.exit.y}\n" + return entry + exit + + +def format_path(maze: Maze) -> str: + path = pathfind_astar(maze) + return "".join(map(str, path)) + "\n" + + +def format_output(maze: Maze) -> str: + return format_maze(maze) + "\n" + format_doors(maze) + format_path(maze) diff --git a/mazegen/utils/coords.py b/mazegen/utils/coords.py index 1a042e6..1e484ff 100644 --- a/mazegen/utils/coords.py +++ b/mazegen/utils/coords.py @@ -44,6 +44,17 @@ class Cardinal(Enum): def right(self) -> "Cardinal": return self.left().opposite() + def __repr__(self) -> str: + match self: + case Cardinal.NORTH: + return "N" + case Cardinal.EAST: + return "E" + case Cardinal.SOUTH: + return "S" + case Cardinal.WEST: + return "W" + @staticmethod def all() -> list["Cardinal"]: return [Cardinal.NORTH, Cardinal.SOUTH, Cardinal.EAST, Cardinal.WEST]