It's aliiiive!

This commit is contained in:
2026-03-24 16:10:57 +01:00
parent a79d4e5c3b
commit 6730ebcdb5
2 changed files with 29 additions and 18 deletions
+2 -2
View File
@@ -8,9 +8,9 @@ def main() -> None:
# try: # try:
maze = Maze(maze=None) maze = Maze(maze=None)
generator = Kruskal() generator = Kruskal()
for alg in generator.generator(20, 20): for alg in generator.generator(50, 50):
maze.set_maze(alg) maze.set_maze(alg)
# os.system("clear") os.system("clear")
maze.ascii_print() maze.ascii_print()
# solver = AStar((1, 1), (14, 18)) # solver = AStar((1, 1), (14, 18))
# print(solver.solve(maze)) # print(solver.solve(maze))
+27 -16
View File
@@ -18,6 +18,10 @@ class Kruskal(MazeGenerator):
def __init__(self, cells: list[int]) -> None: def __init__(self, cells: list[int]) -> None:
self.cells: list[int] = cells self.cells: list[int] = cells
class Sets:
def __init__(self, sets: list[Set]) -> None:
self.sets = sets
@staticmethod @staticmethod
def walls_to_maze( def walls_to_maze(
walls: np.ndarray, height: int, width: int walls: np.ndarray, height: int, width: int
@@ -47,9 +51,9 @@ class Kruskal(MazeGenerator):
return maze return maze
@staticmethod @staticmethod
def is_in_same_set(sets: np.ndarray, wall: tuple[int, int]) -> bool: def is_in_same_set(sets: Sets, wall: tuple[int, int]) -> bool:
a, b = wall a, b = wall
for set in sets: for set in sets.sets:
if a in set.cells and b in set.cells: if a in set.cells and b in set.cells:
return True return True
elif a in set.cells or b in set.cells: elif a in set.cells or b in set.cells:
@@ -57,22 +61,26 @@ class Kruskal(MazeGenerator):
return False return False
@staticmethod @staticmethod
def merge_sets(sets: np.ndarray, wall: tuple[int, int]) -> None: def merge_sets(sets: Sets, wall: tuple[int, int]) -> None:
a, b = wall a, b = wall
base_set = None base_set = None
for i in range(len(sets)): for i in range(len(sets.sets)):
if base_set is None and (a in sets[i].cells or b in sets[i].cells): if base_set is None and (
base_set = sets[i] a in sets.sets[i].cells or b in sets.sets[i].cells
elif base_set and (a in sets[i].cells or b in sets[i].cells): ):
base_set.cells += sets[i].cells base_set = sets.sets[i]
np.delete(sets, i) elif base_set and (
a in sets.sets[i].cells or b in sets.sets[i].cells
):
base_set.cells += sets.sets[i].cells
sets.sets.pop(i)
return return
raise Exception("two sets not found") raise Exception("two sets not found")
def generator( def generator(
self, height: int, width: int self, height: int, width: int
) -> Generator[np.ndarray, None, np.ndarray]: ) -> Generator[np.ndarray, None, np.ndarray]:
sets = np.array([self.Set([i]) for i in range(height * width)]) sets = self.Sets([self.Set([i]) for i in range(height * width)])
walls = [] walls = []
for h in range(height): for h in range(height):
for w in range(width - 1): for w in range(width - 1):
@@ -84,10 +92,13 @@ class Kruskal(MazeGenerator):
np.random.shuffle(walls) np.random.shuffle(walls)
yield self.walls_to_maze(walls, height, width) yield self.walls_to_maze(walls, height, width)
for wall in walls: while len(sets.sets) > 1:
if not self.is_in_same_set(sets, wall): for wall in walls:
self.merge_sets(sets, wall) if not self.is_in_same_set(sets, wall):
walls.remove(wall) self.merge_sets(sets, wall)
yield self.walls_to_maze(walls, height, width) walls.remove(wall)
print(f"nb sets: {len(sets)}") yield self.walls_to_maze(walls, height, width)
if len(sets.sets) == 1:
break
print(f"nb sets: {len(sets.sets)}")
return self.walls_to_maze(walls, height, width) return self.walls_to_maze(walls, height, width)