finish to print the 42

This commit is contained in:
Maoake Teriierooiterai
2026-03-25 13:58:35 +01:00
parent a39f348b1e
commit 4055a8a7a2
2 changed files with 40 additions and 19 deletions
+2 -2
View File
@@ -7,10 +7,10 @@ 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(30, 30): for alg in maze_gen.generator(10, 10):
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))
+38 -17
View File
@@ -8,9 +8,33 @@ import math
class MazeGenerator(ABC): class MazeGenerator(ABC):
@abstractmethod @abstractmethod
def generator( def generator(
self, height: int, width: int self, height: int, width: int, seed: int = None
) -> Generator[np.ndarray, None, np.ndarray]: ... ) -> 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 Kruskal(MazeGenerator):
class Set: class Set:
@@ -77,8 +101,10 @@ class Kruskal(MazeGenerator):
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, seed: int = None
) -> Generator[np.ndarray, None, np.ndarray]: ) -> Generator[np.ndarray, None, np.ndarray]:
if seed is not None:
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)])
walls = [] walls = []
for h in range(height): for h in range(height):
@@ -106,16 +132,19 @@ class Kruskal(MazeGenerator):
class DepthFirstSearch(MazeGenerator): class DepthFirstSearch(MazeGenerator):
def generator( def generator(
self, height: int, width: int self, height: int, width: int, seed: int = None
) -> Generator[np.ndarray, None, np.ndarray]: ) -> Generator[np.ndarray, None, np.ndarray]:
if seed is not None:
np.random.seed(seed)
maze = self.init_maze(width, height) maze = self.init_maze(width, height)
forty_two = self.get_cell_ft(width, height)
visited = np.zeros((height, width), dtype=bool) visited = np.zeros((height, width), dtype=bool)
visited = self.lock_cell_ft(visited, forty_two)
path = list() path = list()
w_h = (width, height) w_h = (width, height)
coord = (0, 0) coord = (0, 0)
x, y = coord x, y = coord
first_iteration = True first_iteration = True
visited = self.lock_cell_ft(visited, (10, 10))
while path or first_iteration: while path or first_iteration:
first_iteration = False first_iteration = False
@@ -210,17 +239,9 @@ class DepthFirstSearch(MazeGenerator):
return path return path
@staticmethod @staticmethod
def lock_cell_ft(visited: np.ndarray, coord: tuple) -> np.ndarray: def lock_cell_ft(visited: np.ndarray, forty_two: set[tuple[int]]
x, y = coord ) -> np.ndarray:
for dy in range(y, y + 7): tab = [cell for cell in forty_two]
for dx in range(x, x + 11): for cell in tab:
visited[dy, dx] = True visited[cell] = True
return visited return visited
@staticmethod
def draw_ft(maze: np.ndarray, coord: tuple):
x, y = coord
for dy in range(y, y + 7):
for dx in range(x, x + 11):
maze[dy][dx] = Cell(value=0)
return maze