mirror of
https://github.com/maoakeEnterprise/amazing.git
synced 2026-04-28 16:04:35 +02:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4055a8a7a2 | |||
| a39f348b1e | |||
| 03c4d206d6 |
+2
-2
@@ -7,10 +7,10 @@ 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(10, 10):
|
||||
maze.set_maze(alg)
|
||||
os.system("clear")
|
||||
maze.ascii_print()
|
||||
maze.ascii_print()
|
||||
# solver = AStar((1, 1), (14, 18))
|
||||
# print(solver.solve(maze))
|
||||
|
||||
|
||||
@@ -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:
|
||||
@@ -77,8 +101,10 @@ class Kruskal(MazeGenerator):
|
||||
raise Exception("two sets not found")
|
||||
|
||||
def generator(
|
||||
self, height: int, width: int
|
||||
self, height: int, width: int, seed: int = None
|
||||
) -> 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)])
|
||||
walls = []
|
||||
for h in range(height):
|
||||
@@ -106,37 +132,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
|
||||
coord = path[-1]
|
||||
random_c = self.random_cells(visited, coord, w_h)
|
||||
x, y = coord
|
||||
|
||||
wall = DepthFirstSearch.next_step(random_c)
|
||||
maze[y][x] = DepthFirstSearch.broken_wall(maze[y][x], wall)
|
||||
wall = self.next_step(random_c)
|
||||
maze[y][x] = self.broken_wall(maze[y][x], wall)
|
||||
|
||||
coord = DepthFirstSearch.next_cell(x, y, wall)
|
||||
wall_r = DepthFirstSearch.reverse_path(wall)
|
||||
coord = self.next_cell(x, y, wall)
|
||||
wall_r = self.reverse_path(wall)
|
||||
x, y = coord
|
||||
maze[y][x] = DepthFirstSearch.broken_wall(maze[y][x], wall_r)
|
||||
maze[y][x] = self.broken_wall(maze[y][x], wall_r)
|
||||
yield maze
|
||||
return maze
|
||||
|
||||
@@ -194,19 +226,22 @@ 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:
|
||||
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:
|
||||
while path:
|
||||
last = path[-1]
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user