mirror of
https://github.com/maoakeEnterprise/amazing.git
synced 2026-04-28 16:04:35 +02:00
Compare commits
4 Commits
c478400640
...
8eb46f601f
| Author | SHA1 | Date | |
|---|---|---|---|
| 8eb46f601f | |||
| 991cdead51 | |||
| 6730ebcdb5 | |||
| a79d4e5c3b |
+4
-2
@@ -7,11 +7,13 @@ import src.amaz_lib as g
|
||||
def main(maze_gen: MazeGenerator) -> None:
|
||||
# try:
|
||||
maze = Maze(maze=None)
|
||||
gen = maze_gen.generator(100, 100)
|
||||
for alg in gen:
|
||||
for alg in maze_gen.generator(30, 10):
|
||||
maze.set_maze(alg)
|
||||
os.system("clear")
|
||||
maze.ascii_print()
|
||||
# solver = AStar((1, 1), (14, 18))
|
||||
# print(solver.solve(maze))
|
||||
|
||||
|
||||
# except Exception as err:
|
||||
# print(err)
|
||||
|
||||
@@ -26,15 +26,14 @@ class Maze:
|
||||
return res
|
||||
|
||||
def ascii_print(self) -> None:
|
||||
for cell in self.maze[0]:
|
||||
print("_", end="")
|
||||
if cell.get_north():
|
||||
print("__", end="")
|
||||
else:
|
||||
print(" ", end="")
|
||||
print("_")
|
||||
for line in self.maze:
|
||||
if line is self.maze[0]:
|
||||
for cell in line:
|
||||
print("_", end="")
|
||||
if cell.get_north():
|
||||
print("__", end="")
|
||||
else:
|
||||
print(" ", end="")
|
||||
print()
|
||||
for cell in line:
|
||||
if cell is line[0] and cell.get_west():
|
||||
print("|", end="")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Generator
|
||||
from typing import Generator, Set
|
||||
import numpy as np
|
||||
from .Cell import Cell
|
||||
import math
|
||||
@@ -13,9 +13,17 @@ class MazeGenerator(ABC):
|
||||
|
||||
|
||||
class Kruskal(MazeGenerator):
|
||||
class Set:
|
||||
def __init__(self, cells: list[int]) -> None:
|
||||
self.cells: list[int] = cells
|
||||
|
||||
class Sets:
|
||||
def __init__(self, sets: list[Set]) -> None:
|
||||
self.sets = sets
|
||||
|
||||
@staticmethod
|
||||
def walls_to_maze(
|
||||
walls: list[tuple[int, int]], height: int, width: int
|
||||
walls: np.ndarray, height: int, width: int
|
||||
) -> np.ndarray:
|
||||
maze: np.ndarray = np.array(
|
||||
[[Cell(value=0) for _ in range(width)] for _ in range(height)]
|
||||
@@ -36,58 +44,70 @@ class Kruskal(MazeGenerator):
|
||||
if x == height - 1:
|
||||
maze[x][y].set_south(True)
|
||||
if y == 0:
|
||||
maze[x][y].set_est(True)
|
||||
if y == width - 1:
|
||||
maze[x][y].set_west(True)
|
||||
if y == width - 1:
|
||||
maze[x][y].set_est(True)
|
||||
return maze
|
||||
|
||||
@staticmethod
|
||||
def is_in_same_set(sets: list[list[int]], wall: tuple[int, int]) -> bool:
|
||||
def is_in_same_set(sets: Sets, wall: tuple[int, int]) -> bool:
|
||||
a, b = wall
|
||||
for set in sets:
|
||||
if a in set and b in set:
|
||||
for set in sets.sets:
|
||||
if a in set.cells and b in set.cells:
|
||||
return True
|
||||
if a in set or b in set:
|
||||
elif a in set.cells or b in set.cells:
|
||||
return False
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def merge_sets(sets: list[list[int]], wall: tuple[int, int]) -> None:
|
||||
def merge_sets(sets: Sets, wall: tuple[int, int]) -> None:
|
||||
a, b = wall
|
||||
base_set = None
|
||||
for set in sets:
|
||||
if base_set is None and (a in set or b in set):
|
||||
base_set = set
|
||||
elif base_set and (a in set or b in set):
|
||||
base_set += set
|
||||
sets.remove(set)
|
||||
for i in range(len(sets.sets)):
|
||||
if base_set is None and (
|
||||
a in sets.sets[i].cells or b in sets.sets[i].cells
|
||||
):
|
||||
base_set = sets.sets[i]
|
||||
elif base_set and (
|
||||
a in sets.sets[i].cells or b in sets.sets[i].cells
|
||||
):
|
||||
base_set.cells += sets.sets[i].cells
|
||||
sets.sets.pop(i)
|
||||
return
|
||||
raise Exception("two sets not found")
|
||||
|
||||
def generator(
|
||||
self, height: int, width: int
|
||||
) -> Generator[np.ndarray, None, np.ndarray]:
|
||||
sets = [[i] for i in range(height * width)]
|
||||
sets = self.Sets([self.Set([i]) for i in range(height * width)])
|
||||
walls = []
|
||||
for h in range(height):
|
||||
for w in range(width - 1):
|
||||
walls += [(w + (width * h), w + (width * h) + 1)]
|
||||
for w in range(width):
|
||||
for h in range(height - 1):
|
||||
walls += [(w + (width * h), w + (width * h) + width)]
|
||||
for h in range(height - 1):
|
||||
for w in range(width):
|
||||
walls += [(w + (width * h), w + (width * (h + 1)))]
|
||||
print(walls)
|
||||
np.random.shuffle(walls)
|
||||
|
||||
yield self.walls_to_maze(walls, height, width)
|
||||
for wall in walls:
|
||||
if not self.is_in_same_set(sets, wall):
|
||||
self.merge_sets(sets, wall)
|
||||
walls.remove(wall)
|
||||
yield self.walls_to_maze(walls, height, width)
|
||||
while len(sets.sets) > 1:
|
||||
for wall in walls:
|
||||
if not self.is_in_same_set(sets, wall):
|
||||
self.merge_sets(sets, wall)
|
||||
walls.remove(wall)
|
||||
yield self.walls_to_maze(walls, height, width)
|
||||
if len(sets.sets) == 1:
|
||||
break
|
||||
print(f"nb sets: {len(sets.sets)}")
|
||||
return self.walls_to_maze(walls, height, width)
|
||||
|
||||
|
||||
class DepthFirstSearch(MazeGenerator):
|
||||
|
||||
def generator(self, width: int, height: int
|
||||
) -> Generator[np.ndarray, None, np.ndarray]:
|
||||
def generator(
|
||||
self, height: int, width: int
|
||||
) -> Generator[np.ndarray, None, np.ndarray]:
|
||||
maze = DepthFirstSearch.init_maze(width, height)
|
||||
visited = np.zeros((height, width), dtype=bool)
|
||||
path = list()
|
||||
@@ -122,8 +142,9 @@ class DepthFirstSearch(MazeGenerator):
|
||||
|
||||
@staticmethod
|
||||
def init_maze(width: int, height: int) -> np.ndarray:
|
||||
maze = np.array([[Cell(value=15) for _ in range(width)]
|
||||
for _ in range(height)])
|
||||
maze = np.array(
|
||||
[[Cell(value=15) for _ in range(width)] for _ in range(height)]
|
||||
)
|
||||
return maze
|
||||
|
||||
@staticmethod
|
||||
@@ -168,34 +189,24 @@ class DepthFirstSearch(MazeGenerator):
|
||||
|
||||
@staticmethod
|
||||
def next_cell(x: int, y: int, next: str) -> tuple:
|
||||
next_step = {
|
||||
"N": (0, -1),
|
||||
"S": (0, 1),
|
||||
"W": (-1, 0),
|
||||
"E": (1, 0)
|
||||
}
|
||||
next_step = {"N": (0, -1), "S": (0, 1), "W": (-1, 0), "E": (1, 0)}
|
||||
add_x, add_y = next_step[next]
|
||||
return (x + add_x, y + add_y)
|
||||
|
||||
@staticmethod
|
||||
def reverse_path(next: str) -> str:
|
||||
reverse = {
|
||||
"N": "S",
|
||||
"S": "N",
|
||||
"W": "E",
|
||||
"E": "W"
|
||||
}
|
||||
reverse = {"N": "S", "S": "N", "W": "E", "E": "W"}
|
||||
return reverse[next]
|
||||
|
||||
@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)
|
||||
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)
|
||||
r_cells = DepthFirstSearch.random_cells(visited, last, w_h)
|
||||
if r_cells:
|
||||
break
|
||||
return path
|
||||
|
||||
+14
-27
@@ -53,25 +53,25 @@ class AStar(MazeSolver):
|
||||
print(actual)
|
||||
path = {
|
||||
"N": (
|
||||
self.f((actual[0], actual[1] - 1))
|
||||
if not maze[actual[0]][actual[1]].get_north() and actual[1] > 0
|
||||
self.f((actual[1] - 1, actual[0]))
|
||||
if not maze[actual[1]][actual[0]].get_north() and actual[0] > 0
|
||||
else None
|
||||
),
|
||||
"E": (
|
||||
self.f((actual[0] + 1, actual[1]))
|
||||
if not maze[actual[0]][actual[1]].get_est()
|
||||
and actual[0] < len(maze) - 1
|
||||
self.f((actual[1], actual[0] + 1))
|
||||
if not maze[actual[1]][actual[0]].get_est()
|
||||
and actual[1] < len(maze) - 1
|
||||
else None
|
||||
),
|
||||
"S": (
|
||||
self.f((actual[0], actual[1] + 1))
|
||||
if not maze[actual[0]][actual[1]].get_south()
|
||||
and actual[1] < len(maze[0]) - 1
|
||||
self.f((actual[1] + 1, actual[0]))
|
||||
if not maze[actual[1]][actual[0]].get_south()
|
||||
and actual[0] < len(maze) - 1
|
||||
else None
|
||||
),
|
||||
"W": (
|
||||
self.f((actual[0] - 1, actual[1]))
|
||||
if not maze[actual[0]][actual[1]].get_west() and actual[0] > 0
|
||||
self.f((actual[1], actual[0] - 1))
|
||||
if not maze[actual[1]][actual[0]].get_west() and actual[1] > 0
|
||||
else None
|
||||
),
|
||||
}
|
||||
@@ -107,23 +107,10 @@ class AStar(MazeSolver):
|
||||
case _:
|
||||
return actual
|
||||
|
||||
def get_path(
|
||||
self, actual: tuple[int, int], maze: np.ndarray, pre: str | None
|
||||
) -> str | None:
|
||||
if actual == self.end:
|
||||
return ""
|
||||
paths = self.best_path(maze, actual)
|
||||
for path in paths:
|
||||
if paths[path] is None:
|
||||
continue
|
||||
if path != pre:
|
||||
temp = self.get_path(
|
||||
self.get_next_pos(path, actual),
|
||||
maze,
|
||||
self.get_opposit(path),
|
||||
)
|
||||
if not temp is None:
|
||||
return path + temp
|
||||
def get_path(self, maze: np.ndarray) -> str | None:
|
||||
actual = self.start
|
||||
path = ""
|
||||
|
||||
return None
|
||||
|
||||
def solve(self, maze: Maze) -> str:
|
||||
|
||||
Reference in New Issue
Block a user