4 Commits

Author SHA1 Message Date
da7e e717bf52e9 fix 42 logo adapt with size 2026-03-25 15:50:08 +01:00
da7e 3fa0d3204e add ft logo to maze 2026-03-25 15:27:39 +01:00
da7e cc6f2eb147 Merge branch 'fix_aster' 2026-03-25 14:52:10 +01:00
da7e c6242eeec0 fix astar algorithm work 2026-03-25 14:51:12 +01:00
3 changed files with 88 additions and 25 deletions
+3 -2
View File
@@ -7,10 +7,11 @@ import src.amaz_lib as g
def main(maze_gen: MazeGenerator) -> None: def main(maze_gen: MazeGenerator) -> None:
# try: # try:
maze = Maze(maze=None) maze = Maze(maze=None)
for alg in maze_gen.generator(10, 10): for alg in maze_gen.generator(21, 21):
maze.set_maze(alg) maze.set_maze(alg)
os.system("clear") os.system("clear")
maze.ascii_print() 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))
@@ -20,4 +21,4 @@ def main(maze_gen: MazeGenerator) -> None:
if __name__ == "__main__": if __name__ == "__main__":
main(g.DepthFirstSearch()) main(g.Kruskal())
+27 -4
View File
@@ -100,9 +100,25 @@ class Kruskal(MazeGenerator):
return return
raise Exception("two sets not found") raise Exception("two sets not found")
@staticmethod
def touch_ft(
width: int,
wall: tuple[int, int],
cells_ft: None | set[tuple[int, int]],
) -> bool:
if cells_ft is None:
return False
s1 = (math.trunc(wall[0] / width), wall[0] % width)
s2 = (math.trunc(wall[1] / width), wall[1] % width)
return s1 in cells_ft or s2 in cells_ft
def generator( def generator(
self, height: int, width: int, seed: int = None self, height: int, width: int, seed: int = None
) -> Generator[np.ndarray, None, np.ndarray]: ) -> Generator[np.ndarray, None, np.ndarray]:
cells_ft = None
if height > 10 and width > 10:
cells_ft = self.get_cell_ft(width, height)
if seed is not None: if seed is not None:
np.random.seed(seed) np.random.seed(seed)
sets = self.Sets([self.Set([i]) for i in range(height * width)]) sets = self.Sets([self.Set([i]) for i in range(height * width)])
@@ -117,13 +133,19 @@ 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)
while len(sets.sets) > 1: while (len(sets.sets) != 1 and cells_ft is None) or (
len(sets.sets) != 19 and cells_ft is not None
):
for wall in walls: for wall in walls:
if not self.is_in_same_set(sets, wall): if not self.is_in_same_set(sets, wall) and not self.touch_ft(
width, wall, cells_ft
):
self.merge_sets(sets, wall) self.merge_sets(sets, wall)
walls.remove(wall) walls.remove(wall)
yield self.walls_to_maze(walls, height, width) yield self.walls_to_maze(walls, height, width)
if len(sets.sets) == 1: if (len(sets.sets) == 1 and cells_ft is None) or (
len(sets.sets) == 19 and cells_ft is not None
):
break break
print(f"nb sets: {len(sets.sets)}") print(f"nb sets: {len(sets.sets)}")
return self.walls_to_maze(walls, height, width) return self.walls_to_maze(walls, height, width)
@@ -239,7 +261,8 @@ class DepthFirstSearch(MazeGenerator):
return path return path
@staticmethod @staticmethod
def lock_cell_ft(visited: np.ndarray, forty_two: set[tuple[int]] def lock_cell_ft(
visited: np.ndarray, forty_two: set[tuple[int]]
) -> np.ndarray: ) -> np.ndarray:
tab = [cell for cell in forty_two] tab = [cell for cell in forty_two]
for cell in tab: for cell in tab:
+56 -17
View File
@@ -5,8 +5,8 @@ import numpy as np
class MazeSolver(ABC): class MazeSolver(ABC):
def __init__(self, start: tuple[int, int], end: tuple[int, int]) -> None: def __init__(self, start: tuple[int, int], end: tuple[int, int]) -> None:
self.start = (start[0] - 1, start[1] - 1) self.start = (start[1] - 1, start[0] - 1)
self.end = (end[0] - 1, end[1] - 1) self.end = (end[1] - 1, end[0] - 1)
@abstractmethod @abstractmethod
def solve(self, maze: Maze) -> str: ... def solve(self, maze: Maze) -> str: ...
@@ -48,35 +48,39 @@ class AStar(MazeSolver):
return 1000 return 1000
def best_path( def best_path(
self, maze: np.ndarray, actual: tuple[int, int] self,
) -> dict[str, int | None]: maze: np.ndarray,
print(actual) actual: tuple[int, int],
last: str | None,
) -> dict[str, int]:
path = { path = {
"N": ( "N": (
self.f((actual[1] - 1, actual[0])) self.f((actual[0], actual[1] - 1))
if not maze[actual[1]][actual[0]].get_north() and actual[0] > 0 if not maze[actual[1]][actual[0]].get_north() and actual[1] > 0
else None else None
), ),
"E": ( "E": (
self.f((actual[1], actual[0] + 1)) self.f((actual[0] + 1, actual[1]))
if not maze[actual[1]][actual[0]].get_est() if not maze[actual[1]][actual[0]].get_est()
and actual[1] < len(maze) - 1 and actual[0] < len(maze[0]) - 1
else None else None
), ),
"S": ( "S": (
self.f((actual[1] + 1, actual[0])) self.f((actual[0], actual[1] + 1))
if not maze[actual[1]][actual[0]].get_south() if not maze[actual[1]][actual[0]].get_south()
and actual[0] < len(maze) - 1 and actual[1] < len(maze) - 1
else None else None
), ),
"W": ( "W": (
self.f((actual[1], actual[0] - 1)) self.f((actual[0] - 1, actual[1]))
if not maze[actual[1]][actual[0]].get_west() and actual[1] > 0 if not maze[actual[1]][actual[0]].get_west() and actual[0] > 0
else None else None
), ),
} }
return { return {
k: v for k, v in sorted(path.items(), key=lambda item: item[0]) k: v
for k, v in sorted(path.items(), key=lambda item: item[0])
if v is not None and k != last
} }
def get_opposit(self, dir: str) -> str: def get_opposit(self, dir: str) -> str:
@@ -108,14 +112,49 @@ class AStar(MazeSolver):
return actual return actual
def get_path(self, maze: np.ndarray) -> str | None: def get_path(self, maze: np.ndarray) -> str | None:
actual = self.start path = [(self.start, self.best_path(maze, self.start, None))]
path = "" visited = [self.start]
while len(path) > 0 and path[-1][0] != self.end:
print(path[-1])
if len(path[-1][1]) == 0:
path.pop(-1)
if len(path) == 0:
break
k = next(iter(path[-1][1]))
path[-1][1].pop(k)
continue
while len(path[-1][1]) > 0:
next_pos = self.get_next_pos(
list(path[-1][1].keys())[0], path[-1][0]
)
if next_pos in visited:
k = next(iter(path[-1][1]))
path[-1][1].pop(k)
else:
break
if len(path[-1][1]) == 0:
path.pop(-1)
continue
pre = self.get_opposit(list(path[-1][1].keys())[0])
path.append(
(
next_pos,
self.best_path(maze, next_pos, pre),
)
)
visited += [next_pos]
if len(path) == 0:
return None return None
path[-1] = (self.end, {})
return "".join(
str(list(c[1].keys())[0]) for c in path if len(c[1]) > 0
)
def solve(self, maze: Maze) -> str: def solve(self, maze: Maze) -> str:
print(maze) print(maze)
res = self.get_path(self.start, maze.get_maze(), None) res = self.get_path(maze.get_maze())
if res is None: if res is None:
raise Exception("Path not found") raise Exception("Path not found")
return res return res