3 files changed, +22, -32
12b62e6refactor(mazegen): rename exit to exit_, remove _reserve_42_pattern() etc
+2, -2
@@ -17,7 +17,7 @@ def write_hex_output(gen: MazeGenerator, path: str) -> None:
1717 OSError: If the file cannot be opened or written.
1818 """
1919 ex, ey = gen.entry
20 - xx, xy = gen.exit
20+ xx, xy = gen.exit_
2121 directions = "".join(gen.path)
2222 with open(path, "w") as f:
2323 for line in gen.to_hex_lines():
@@ -53,7 +53,7 @@ def main(argv: list[str]) -> int:
5353 width=cfg.width,
5454 height=cfg.height,
5555 entry=cfg.entry,
56 - exit=cfg.exit,
56+ exit_=cfg.exit_,
5757 perfect=cfg.perfect,
5858 seed=cfg.seed,
5959 )
+3, -3
@@ -25,7 +25,7 @@ class Config:
2525 width: Maze width in cells, >= 2.
2626 height: Maze height in cells, >= 2.
2727 entry: Entry cell (x, y), in bounds.
28 - exit: Exit cell (x, y), in bounds and != entry.
28+ exit_: Exit cell (x, y), in bounds and != entry.
2929 output_file: Destination path for hex output.
3030 perfect: True for spanning tree, False to allow loops.
3131 seed: RNG seed, or None for random.
@@ -34,7 +34,7 @@ class Config:
3434 width: int
3535 height: int
3636 entry: tuple[int, int]
37 - exit: tuple[int, int]
37+ exit_: tuple[int, int]
3838 output_file: str
3939 perfect: bool
4040 seed: int | None
@@ -85,7 +85,7 @@ def parse_config(path: str) -> Config:
8585 width=width,
8686 height=height,
8787 entry=entry,
88 - exit=exit_,
88+ exit_=exit_,
8989 output_file=output_file,
9090 perfect=perfect,
9191 seed=seed,
+17, -27
@@ -13,7 +13,7 @@ SOLID_CELL = N | E | S | W # 15
13 13 OPPOSITE = {N: S, E: W, S: N, W: E}
14 14 DX = {N: 0, E: 1, S: 0, W: -1}
15 15 DY = {N: -1, E: 0, S: 1, W: 0}
16 -DIR_NAME = {N: "N", E: "E", S: "S", W: "W"}
16+DELTA_DIR = {(0, -1): "N", (1, 0): "E", (0, 1): "S", (-1, 0): "W"}
17 17
18 18 # "4" 5x3 + 1-col gap + "2" 5x3 = 5 rows x 7 cols.
19 19 PATTERN_42_OFFSETS: tuple[Position, ...] = (
@@ -50,7 +50,7 @@ class MazeGenerator:
50 50 width: int,
51 51 height: int,
52 52 entry: Position,
53 - exit: Position,
53+ exit_: Position,
54 54 perfect: bool,
55 55 seed: int | None = None,
56 56 ) -> None:
@@ -60,14 +60,14 @@ class MazeGenerator:
60 60 width: Grid width in cells, >= 9 (to fit the '42' glyph).
61 61 height: Grid height in cells, >= 7.
62 62 entry: Entry cell (x, y).
63 - exit: Exit cell (x, y).
63+ exit_: Exit cell (x, y).
64 64 perfect: True for spanning tree, False to allow loops.
65 65 seed: RNG seed, or None for non-deterministic output.
66 66 """
67 67 self._width = width
68 68 self._height = height
69 69 self._entry = entry
70 - self._exit = exit
70+ self._exit = exit_
71 71 self._perfect = perfect
72 72 self._rng = random.Random(seed)
73 73 self._grid: Grid = []
@@ -90,7 +90,7 @@ class MazeGenerator:
90 90 return self._entry
91 91
92 92 @property
93 - def exit(self) -> Position:
93+ def exit_(self) -> Position:
94 94 """Exit cell (x, y)."""
95 95 return self._exit
96 96
@@ -128,7 +128,9 @@ class MazeGenerator:
128128 The shared grid object after each mutation.
129129 """
130130 self._init_grid()
131 - self._reserve_42_pattern()
131+ self._pattern_cells = self.pattern_cells_for(
132+ self._width, self._height
133+ )
132134 yield self._grid
133135 yield from self._carve_dfs()
134136
@@ -141,7 +143,6 @@ class MazeGenerator:
141143 Raises:
142144 ValueError: If exit is unreachable from entry.
143145 """
144 - self._path = []
145146 yield from self._solve_bfs()
146147
147148 def to_hex_lines(self) -> Iterator[str]:
@@ -167,10 +168,7 @@ class MazeGenerator:
167168 Raises:
168169 ValueError: If grid is too small for the glyph plus margin.
169170 """
170 - if (
171 - width < PATTERN_42_COLS + 2
172 - or height < PATTERN_42_ROWS + 2
173 - ):
171+ if width < PATTERN_42_COLS + 2 or height < PATTERN_42_ROWS + 2:
174172 raise ValueError("maze too small for '42' pattern")
175173 sx = (width - PATTERN_42_COLS) // 2
176174 sy = (height - PATTERN_42_ROWS) // 2
@@ -181,13 +179,12 @@ class MazeGenerator:
181179
182180 Naturally produces perfect mazes with no 3x3 on its own.
183181 """
184 - # We can't path through the pattern, so add it to the visited set
185 - visited: set[Position] = set(self._pattern_cells)
186 - visited.add(self._entry)
182+ # We can't path through the pattern or entry, so add it to the visited
183+ visited: set[Position] = self._pattern_cells | {self._entry}
187184 stack: list[Position] = [self._entry]
188185 while stack:
189186 x, y = stack[-1]
190 - unvisited: list[tuple[int, int, int]] = []
187+ unvisited = []
191188 for d in (N, E, S, W):
192189 nx, ny = x + DX[d], y + DY[d]
193190 if self._in_bounds(nx, ny) and (nx, ny) not in visited:
@@ -205,8 +202,7 @@ class MazeGenerator:
205202 """BFS shortest path entry -> exit; yields each visited cell."""
206203 entry = self._entry
207204 exit_ = self._exit
208 - predecessor: dict[Position, Position | None] = {entry: None}
209 - came_from: dict[Position, int] = {}
205+ predecessor: dict[Position, Position] = {entry: entry}
210206 queue: deque[Position] = deque([entry])
211207 yield entry
212208 while queue:
@@ -221,15 +217,15 @@ class MazeGenerator:
221217 if nxt in predecessor:
222218 continue
223219 predecessor[nxt] = (x, y)
224 - came_from[nxt] = d
225220 queue.append(nxt)
226221 yield nxt
227222 if exit_ not in predecessor:
228223 raise ValueError("no path from entry to exit")
229224 path: list[str] = []
230 - cur: Position = exit_
231 - while (prev := predecessor[cur]) is not None:
232 - path.append(DIR_NAME[came_from[cur]])
225+ cur = exit_
226+ while cur != entry:
227+ prev = predecessor[cur]
228+ path.append(DELTA_DIR[(cur[0] - prev[0], cur[1] - prev[1])])
233229 cur = prev
234230 path.reverse()
235231 self._path = path
@@ -250,9 +246,3 @@ class MazeGenerator:
250246 self._grid[y][x] &= ~direction
251247 nx, ny = x + DX[direction], y + DY[direction]
252248 self._grid[ny][nx] &= ~OPPOSITE[direction]
253 -
254 - def _reserve_42_pattern(self) -> None:
255 - """Populate self._pattern_cells; cells stay SOLID_CELL in the grid."""
256 - self._pattern_cells = self.pattern_cells_for(
257 - self._width, self._height
258 - )