7 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
Maoake Teriierooiterai 4055a8a7a2 finish to print the 42 2026-03-25 13:58:35 +01:00
Maoake TERIIEROOITERAI a39f348b1e set up the function draw ft 2026-03-24 22:11:54 +01:00
Maoake TERIIEROOITERAI 03c4d206d6 starting the branch parsing need to get a good start on this 2026-03-24 21:21:46 +01:00
3 changed files with 152 additions and 54 deletions
+3 -2
View File
@@ -7,10 +7,11 @@ import src.amaz_lib as g
def main(maze_gen: MazeGenerator) -> None:
# try:
maze = Maze(maze=None)
for alg in maze_gen.generator(30, 10):
for alg in maze_gen.generator(21, 21):
maze.set_maze(alg)
os.system("clear")
maze.ascii_print()
maze.ascii_print()
# solver = AStar((1, 1), (14, 18))
# print(solver.solve(maze))
@@ -20,4 +21,4 @@ def main(maze_gen: MazeGenerator) -> None:
if __name__ == "__main__":
main(g.DepthFirstSearch())
main(g.Kruskal())
+93 -35
View File
@@ -8,9 +8,33 @@ import math
class MazeGenerator(ABC):
@abstractmethod
def generator(
self, height: int, width: int
self, height: int, width: int, seed: int = None
) -> Generator[np.ndarray, None, np.ndarray]: ...
@staticmethod
def get_cell_ft(width: int, height: int) -> set:
forty_two = set()
y, x = (int(height / 2), int(width / 2))
forty_two.add((y, x - 1))
forty_two.add((y, x - 2))
forty_two.add((y, x - 3))
forty_two.add((y - 1, x - 3))
forty_two.add((y - 2, x - 3))
forty_two.add((y + 1, x - 1))
forty_two.add((y + 2, x - 1))
forty_two.add((y, x + 1))
forty_two.add((y, x + 2))
forty_two.add((y, x + 3))
forty_two.add((y - 1, x + 3))
forty_two.add((y - 2, x + 3))
forty_two.add((y - 2, x + 2))
forty_two.add((y - 2, x + 1))
forty_two.add((y + 1, x + 1))
forty_two.add((y + 2, x + 1))
forty_two.add((y + 2, x + 2))
forty_two.add((y + 2, x + 3))
return forty_two
class Kruskal(MazeGenerator):
class Set:
@@ -76,9 +100,27 @@ class Kruskal(MazeGenerator):
return
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(
self, height: int, width: int
self, height: int, width: int, seed: int = None
) -> 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:
np.random.seed(seed)
sets = self.Sets([self.Set([i]) for i in range(height * width)])
walls = []
for h in range(height):
@@ -91,13 +133,19 @@ class Kruskal(MazeGenerator):
np.random.shuffle(walls)
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:
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)
walls.remove(wall)
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
print(f"nb sets: {len(sets.sets)}")
return self.walls_to_maze(walls, height, width)
@@ -106,37 +154,43 @@ class Kruskal(MazeGenerator):
class DepthFirstSearch(MazeGenerator):
def generator(
self, height: int, width: int
self, height: int, width: int, seed: int = None
) -> Generator[np.ndarray, None, np.ndarray]:
maze = DepthFirstSearch.init_maze(width, height)
if seed is not None:
np.random.seed(seed)
maze = self.init_maze(width, height)
forty_two = self.get_cell_ft(width, height)
visited = np.zeros((height, width), dtype=bool)
visited = self.lock_cell_ft(visited, forty_two)
path = list()
w_h = (width, height)
coord = (0, 0)
x, y = coord
first = True
first_iteration = True
while path or first_iteration:
first_iteration = False
while path or first:
first = False
visited[y, x] = True
path = DepthFirstSearch.add_cell_visited(coord, path)
random_c = DepthFirstSearch.random_cells(visited, coord, w_h)
if len(random_c) == 0:
path = DepthFirstSearch.back_on_step(path, w_h, visited)
if path:
coord = path[-1]
random_c = DepthFirstSearch.random_cells(visited, coord, w_h)
x, y = coord
path = self.add_cell_visited(coord, path)
random_c = self.random_cells(visited, coord, w_h)
if not random_c:
path = self.back_on_step(path, w_h, visited)
if not path:
break
wall = DepthFirstSearch.next_step(random_c)
maze[y][x] = DepthFirstSearch.broken_wall(maze[y][x], wall)
coord = DepthFirstSearch.next_cell(x, y, wall)
wall_r = DepthFirstSearch.reverse_path(wall)
coord = path[-1]
random_c = self.random_cells(visited, coord, w_h)
x, y = coord
maze[y][x] = DepthFirstSearch.broken_wall(maze[y][x], wall_r)
wall = self.next_step(random_c)
maze[y][x] = self.broken_wall(maze[y][x], wall)
coord = self.next_cell(x, y, wall)
wall_r = self.reverse_path(wall)
x, y = coord
maze[y][x] = self.broken_wall(maze[y][x], wall_r)
yield maze
return maze
@@ -194,19 +248,23 @@ class DepthFirstSearch(MazeGenerator):
return (x + add_x, y + add_y)
@staticmethod
def reverse_path(next: str) -> str:
reverse = {"N": "S", "S": "N", "W": "E", "E": "W"}
return reverse[next]
def reverse_path(direction: str) -> str:
return {"N": "S", "S": "N", "W": "E", "E": "W"}[direction]
@staticmethod
def back_on_step(path: list, w_h: tuple, visited: np.array) -> list:
while path:
last = path[-1]
r_cells = DepthFirstSearch.random_cells(visited, last, w_h)
while len(path) > 0:
path.pop()
if path:
last = path[-1]
r_cells = DepthFirstSearch.random_cells(visited, last, w_h)
if r_cells:
if DepthFirstSearch.random_cells(visited, last, w_h):
break
path.pop()
return path
@staticmethod
def lock_cell_ft(
visited: np.ndarray, forty_two: set[tuple[int]]
) -> np.ndarray:
tab = [cell for cell in forty_two]
for cell in tab:
visited[cell] = True
return visited
+56 -17
View File
@@ -5,8 +5,8 @@ import numpy as np
class MazeSolver(ABC):
def __init__(self, start: tuple[int, int], end: tuple[int, int]) -> None:
self.start = (start[0] - 1, start[1] - 1)
self.end = (end[0] - 1, end[1] - 1)
self.start = (start[1] - 1, start[0] - 1)
self.end = (end[1] - 1, end[0] - 1)
@abstractmethod
def solve(self, maze: Maze) -> str: ...
@@ -48,35 +48,39 @@ class AStar(MazeSolver):
return 1000
def best_path(
self, maze: np.ndarray, actual: tuple[int, int]
) -> dict[str, int | None]:
print(actual)
self,
maze: np.ndarray,
actual: tuple[int, int],
last: str | None,
) -> dict[str, int]:
path = {
"N": (
self.f((actual[1] - 1, actual[0]))
if not maze[actual[1]][actual[0]].get_north() and actual[0] > 0
self.f((actual[0], actual[1] - 1))
if not maze[actual[1]][actual[0]].get_north() and actual[1] > 0
else None
),
"E": (
self.f((actual[1], actual[0] + 1))
self.f((actual[0] + 1, actual[1]))
if not maze[actual[1]][actual[0]].get_est()
and actual[1] < len(maze) - 1
and actual[0] < len(maze[0]) - 1
else None
),
"S": (
self.f((actual[1] + 1, actual[0]))
self.f((actual[0], actual[1] + 1))
if not maze[actual[1]][actual[0]].get_south()
and actual[0] < len(maze) - 1
and actual[1] < len(maze) - 1
else None
),
"W": (
self.f((actual[1], actual[0] - 1))
if not maze[actual[1]][actual[0]].get_west() and actual[1] > 0
self.f((actual[0] - 1, actual[1]))
if not maze[actual[1]][actual[0]].get_west() and actual[0] > 0
else None
),
}
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:
@@ -108,14 +112,49 @@ class AStar(MazeSolver):
return actual
def get_path(self, maze: np.ndarray) -> str | None:
actual = self.start
path = ""
path = [(self.start, self.best_path(maze, self.start, None))]
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
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:
print(maze)
res = self.get_path(self.start, maze.get_maze(), None)
res = self.get_path(maze.get_maze())
if res is None:
raise Exception("Path not found")
return res