mirror of
https://github.com/maoakeEnterprise/amazing.git
synced 2026-04-29 00:14:34 +02:00
Compare commits
7 Commits
8eb46f601f
...
e717bf52e9
| Author | SHA1 | Date | |
|---|---|---|---|
| e717bf52e9 | |||
| 3fa0d3204e | |||
| cc6f2eb147 | |||
| c6242eeec0 | |||
| 4055a8a7a2 | |||
| a39f348b1e | |||
| 03c4d206d6 |
+3
-2
@@ -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(30, 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())
|
||||||
|
|||||||
@@ -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:
|
||||||
@@ -76,9 +100,27 @@ 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
|
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:
|
||||||
|
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):
|
||||||
@@ -91,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)
|
||||||
@@ -106,37 +154,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 +248,23 @@ 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
|
||||||
|
|||||||
+57
-18
@@ -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
|
||||||
|
|
||||||
return None
|
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:
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user