From: Axy Date: Sun, 29 Mar 2026 23:06:28 +0000 (+0200) Subject: Simple module for the subject X-Git-Url: https://git.uwuaxy.net/flexible_layout.mp4?a=commitdiff_plain;h=8854267906d1032566831c6ce71cbe7f905da08f;p=axy%2Fft%2Fa-maze-ing.git Simple module for the subject --- diff --git a/a_maze_ing.py b/a_maze_ing.py index 14544c9..40da036 100644 --- a/a_maze_ing.py +++ b/a_maze_ing.py @@ -63,7 +63,7 @@ pattern = Pattern(config.maze_pattern).centered_for(maze.dims, excluded) def maze_main() -> None: - pattern.fill(maze) + pattern.write_to_maze(maze) maze.outline() walls_const = set(maze.walls_full()) diff --git a/mazegen/__init__.py b/mazegen/__init__.py index 0024978..3d57222 100644 --- a/mazegen/__init__.py +++ b/mazegen/__init__.py @@ -1,2 +1,56 @@ __version__ = "1.0.0" __author__ = "luflores & agilliar" + + +class MazeGenerator: + """ + A very simple but not very practical maze generator + The options have the same effect as in the config file + """ + + def __init__( + self, + dims: tuple[int, int], + entry: tuple[int, int], + exit: tuple[int, int], + perfect: bool = True, + seed: int | None = None, + ) -> None: + from mazegen.maze import ( + Maze, + Pattern, + make_perfect, + make_pacman, + NetworkTracker, + PacmanTracker, + ) + from mazegen.utils import IVec2 + import random + + prev_rand = random.getstate() + random.seed(seed) + + maze = Maze(IVec2(*dims), IVec2(*entry), IVec2(*exit)) + maze.outline() + Pattern(Pattern.FT_PATTERN).centered_for( + maze.dims, {maze.entry, maze.exit} + ).write_to_maze(maze) + walls_const = set(maze.walls_full()) + + make_perfect(maze, NetworkTracker(maze)) + if not perfect: + make_pacman(maze, walls_const, PacmanTracker(maze)) + + random.setstate(prev_rand) + self.__maze = maze + + def get_output(self) -> str: + """ + Returns the output as formatted for the output file + """ + from mazegen.maze.output import format_output + + return format_output(self.__maze) + + +__all__ = ["MazeGenerator"] diff --git a/mazegen/maze/maze.py b/mazegen/maze/maze.py index 3ac95e1..a9f0ea8 100644 --- a/mazegen/maze/maze.py +++ b/mazegen/maze/maze.py @@ -1,4 +1,4 @@ -from typing import Callable, Generator, Iterable +from typing import Callable, Generator, Iterable, overload from mazegen.config.config_parser import Config from mazegen.utils import ( CellCoord, @@ -16,19 +16,30 @@ class Maze: Its observers are called whenever the status of a wall changes """ - def __init__(self, config: Config) -> None: - self.dims = IVec2(config.width, config.height) - self.observers: set[MazeObserver] = set() - self.entry: CellCoord = ( - CellCoord(0, 0) - if config.entry is None - else CellCoord(config.entry) - ) - self.exit: CellCoord = ( - CellCoord(self.dims - IVec2.splat(1)) - if config.exit is None - else CellCoord(config.exit) + @overload + def __init__(self, config: Config) -> None: ... + @overload + def __init__(self, dims: IVec2, entry: IVec2, exit: IVec2, /) -> None: ... + + def __init__( + self, + config: Config | IVec2, + entry: IVec2 = IVec2.splat(0), + exit: IVec2 = IVec2.splat(0), + ) -> None: + self.dims = ( + IVec2(config.width, config.height) + if isinstance(config, Config) + else config ) + self.observers: set[MazeObserver] = set() + self.entry: CellCoord = CellCoord(entry) + self.exit: CellCoord = CellCoord(exit) + if isinstance(config, Config): + if config.entry is not None: + self.entry = CellCoord(config.entry) + if config.exit is not None: + self.exit = CellCoord(config.exit) self.__walls_full: dict[WallCoord, None] = {} def get_wall(self, coord: WallCoord) -> bool: diff --git a/mazegen/maze/pattern.py b/mazegen/maze/pattern.py index 29df958..b26c944 100644 --- a/mazegen/maze/pattern.py +++ b/mazegen/maze/pattern.py @@ -140,9 +140,9 @@ class Pattern: return normalized.offset(pos) return Pattern([]) - def fill(self, maze: "Maze") -> None: + def write_to_maze(self, maze: "Maze") -> None: """ - Fills the pattern into the maze by filling the walls of each pattern + Writes the pattern into the maze by filling the walls of each pattern cell """ for cell in self.__cells: