mirror of
https://github.com/maoakeEnterprise/amazing.git
synced 2026-04-29 00:14:34 +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:
|
def main(maze_gen: MazeGenerator) -> None:
|
||||||
# try:
|
# try:
|
||||||
maze = Maze(maze=None)
|
maze = Maze(maze=None)
|
||||||
for alg in maze_gen.generator(30, 10):
|
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))
|
||||||
|
|
||||||
|
|||||||
@@ -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,37 +132,43 @@ 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]:
|
||||||
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 = 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 = True
|
first_iteration = True
|
||||||
|
|
||||||
|
while path or first_iteration:
|
||||||
|
first_iteration = False
|
||||||
|
|
||||||
while path or first:
|
|
||||||
first = False
|
|
||||||
visited[y, x] = True
|
visited[y, x] = True
|
||||||
path = DepthFirstSearch.add_cell_visited(coord, path)
|
path = self.add_cell_visited(coord, path)
|
||||||
random_c = DepthFirstSearch.random_cells(visited, coord, w_h)
|
|
||||||
if len(random_c) == 0:
|
random_c = self.random_cells(visited, coord, w_h)
|
||||||
path = DepthFirstSearch.back_on_step(path, w_h, visited)
|
|
||||||
if path:
|
if not random_c:
|
||||||
coord = path[-1]
|
path = self.back_on_step(path, w_h, visited)
|
||||||
random_c = DepthFirstSearch.random_cells(visited, coord, w_h)
|
|
||||||
x, y = coord
|
|
||||||
if not path:
|
if not path:
|
||||||
break
|
break
|
||||||
|
coord = path[-1]
|
||||||
|
random_c = self.random_cells(visited, coord, w_h)
|
||||||
|
x, y = coord
|
||||||
|
|
||||||
wall = DepthFirstSearch.next_step(random_c)
|
wall = self.next_step(random_c)
|
||||||
maze[y][x] = DepthFirstSearch.broken_wall(maze[y][x], wall)
|
maze[y][x] = self.broken_wall(maze[y][x], wall)
|
||||||
|
|
||||||
coord = DepthFirstSearch.next_cell(x, y, wall)
|
coord = self.next_cell(x, y, wall)
|
||||||
wall_r = DepthFirstSearch.reverse_path(wall)
|
wall_r = self.reverse_path(wall)
|
||||||
x, y = coord
|
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
|
yield maze
|
||||||
return maze
|
return maze
|
||||||
|
|
||||||
@@ -194,19 +226,22 @@ class DepthFirstSearch(MazeGenerator):
|
|||||||
return (x + add_x, y + add_y)
|
return (x + add_x, y + add_y)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def reverse_path(next: str) -> str:
|
def reverse_path(direction: str) -> str:
|
||||||
reverse = {"N": "S", "S": "N", "W": "E", "E": "W"}
|
return {"N": "S", "S": "N", "W": "E", "E": "W"}[direction]
|
||||||
return reverse[next]
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def back_on_step(path: list, w_h: tuple, visited: np.array) -> list:
|
def back_on_step(path: list, w_h: tuple, visited: np.array) -> list:
|
||||||
last = path[-1]
|
while path:
|
||||||
r_cells = DepthFirstSearch.random_cells(visited, last, w_h)
|
last = path[-1]
|
||||||
while len(path) > 0:
|
if DepthFirstSearch.random_cells(visited, last, w_h):
|
||||||
path.pop()
|
|
||||||
if path:
|
|
||||||
last = path[-1]
|
|
||||||
r_cells = DepthFirstSearch.random_cells(visited, last, w_h)
|
|
||||||
if r_cells:
|
|
||||||
break
|
break
|
||||||
|
path.pop()
|
||||||
return path
|
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