]> Untitled Git - axy/ft/a-maze-ing.git/commitdiff
Added output.py - format_output(maze) returns a str representation of maze, exits...
authorLucas Flores <luflores@k1r1p3.42mulhouse.fr>
Fri, 27 Mar 2026 16:39:36 +0000 (17:39 +0100)
committerLucas Flores <luflores@k1r1p3.42mulhouse.fr>
Fri, 27 Mar 2026 16:39:36 +0000 (17:39 +0100)
a_maze_ing.py
mazegen/maze/output.py [new file with mode: 0644]
mazegen/utils/coords.py

index 820f9a18abe1d22b109ede4dc5a2ab1a230bd893..fcc26dbc655055c4696ca32e15ba9fcbbf998d77 100644 (file)
@@ -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 (file)
index 0000000..266510f
--- /dev/null
@@ -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)
index 1a042e6771519c9ff84c26ee30bf6f89f21cce9c..1e484ff826773473a53635ab6dc25ddcb6a6bb62 100644 (file)
@@ -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]