From e46ce462f74f8261a3921a2719e4127fbe301590 Mon Sep 17 00:00:00 2001 From: Axy Date: Fri, 6 Feb 2026 00:17:57 +0100 Subject: [PATCH] Acyclic generation --- __main__.py | 15 +++++++++------ amazeing/maze.py | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/__main__.py b/__main__.py index 6989c13..4ccbd6f 100644 --- a/__main__.py +++ b/__main__.py @@ -2,21 +2,24 @@ from amazeing import Maze, TTYBackend, Pattern import random -maze = Maze(50, 20) +dims = (30, 30) + +maze = Maze(*dims) maze.outline() -pattern = Pattern(Pattern.FT_PATTERN).centered_for((50, 20)) +pattern = Pattern(Pattern.FT_PATTERN).centered_for(dims) pattern.fill(maze) empty = list(maze.walls_empty()) random.shuffle(empty) for wall in empty: - if not maze.wall_bisects(wall): - maze.fill_wall(wall) + if maze.wall_bisects(wall): # or maze.wall_cuts_cycle(wall): + continue + maze.fill_wall(wall) -backend = TTYBackend(50, 20) -backend.set_style("#") +backend = TTYBackend(*dims, style="\x1b[48;5;240m \x1b[0m") +backend.set_style("\x1b[48;5;248m \x1b[0m") for wall in maze.walls_full(): for pixel in wall.pixel_coords(): backend.draw_pixel(pixel) diff --git a/amazeing/maze.py b/amazeing/maze.py index 65b6956..9777e4b 100644 --- a/amazeing/maze.py +++ b/amazeing/maze.py @@ -64,6 +64,11 @@ class WallCoord: ) return (PixelCoord(x, y) for x in x_iter for y in y_iter) + def neighbour_cells(self) -> list["CellCoord"]: + if self.orientation == Orientation.HORIZONTAL: + return [CellCoord(self.b, self.a), CellCoord(self.b, self.a - 1)] + return [CellCoord(self.a, self.b), CellCoord(self.a - 1, self.b)] + class CellCoord: def __init__(self, x: int, y: int) -> None: @@ -278,3 +283,16 @@ class Maze: if neighbour.is_full() } return len(a & b) != 0 + + def wall_cuts_cycle(self, wall: WallCoord) -> bool: + return any( + len( + [ + () + for wall in self.get_walls_checked(list(cell.walls())) + if wall.is_full() + ] + ) + >= 2 + for cell in wall.neighbour_cells() + ) -- 2.52.0