mirror of
https://github.com/maoakeEnterprise/amazing.git
synced 2026-04-29 00:14:34 +02:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c478400640 | |||
| 993bcce857 | |||
| a85e342a0a | |||
| 4d151664ab | |||
| 8b4ef7afce | |||
| 030c6142ba | |||
| f8f0e31598 | |||
| e75e14110d |
@@ -18,5 +18,13 @@ lint-strict:
|
||||
uv run flake8 .
|
||||
uv run mypy . --strict
|
||||
|
||||
run_test_parsing:
|
||||
PYTHONPATH=src uv run pytest tests/test_parsing.py
|
||||
|
||||
run_test_dfs:
|
||||
PYTHONPATH=src uv run pytest tests/test_Depth.py
|
||||
|
||||
run_test_maze_gen:
|
||||
PYTHONPATH=src uv run pytest tests/test_MazeGenerator.py
|
||||
run_test:
|
||||
uv run pytest
|
||||
|
||||
+7
-10
@@ -1,24 +1,21 @@
|
||||
import os
|
||||
from numpy import ma
|
||||
from src.amaz_lib import MazeGenerator, Kruskal, AStar
|
||||
from src.amaz_lib import Maze
|
||||
from src.amaz_lib import MazeGenerator
|
||||
import src.amaz_lib as g
|
||||
|
||||
|
||||
def main() -> None:
|
||||
def main(maze_gen: MazeGenerator) -> None:
|
||||
# try:
|
||||
maze = Maze(maze=None)
|
||||
generator = Kruskal()
|
||||
for alg in generator.generator(20, 20):
|
||||
gen = maze_gen.generator(100, 100)
|
||||
for alg in gen:
|
||||
maze.set_maze(alg)
|
||||
# os.system("clear")
|
||||
os.system("clear")
|
||||
maze.ascii_print()
|
||||
# solver = AStar((1, 1), (14, 18))
|
||||
# print(solver.solve(maze))
|
||||
|
||||
|
||||
# except Exception as err:
|
||||
# print(err)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
main(g.DepthFirstSearch())
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
from pydantic import BaseModel, Field
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
class Cell(BaseModel):
|
||||
value: int = Field(ge=0, le=15)
|
||||
@dataclass
|
||||
class Cell:
|
||||
def __init__(self, value: int) -> None:
|
||||
self.value = value
|
||||
|
||||
def __str__(self) -> str:
|
||||
return hex(self.value).removeprefix("0x").upper()
|
||||
|
||||
@@ -26,14 +26,15 @@ 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="")
|
||||
|
||||
+135
-27
@@ -1,6 +1,5 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from dataclasses import dataclass
|
||||
from typing import Generator, Set
|
||||
from typing import Generator
|
||||
import numpy as np
|
||||
from .Cell import Cell
|
||||
import math
|
||||
@@ -14,13 +13,9 @@ class MazeGenerator(ABC):
|
||||
|
||||
|
||||
class Kruskal(MazeGenerator):
|
||||
class Set:
|
||||
def __init__(self, cells: list[int]) -> None:
|
||||
self.cells: list[int] = cells
|
||||
|
||||
@staticmethod
|
||||
def walls_to_maze(
|
||||
walls: np.ndarray, height: int, width: int
|
||||
walls: list[tuple[int, int]], height: int, width: int
|
||||
) -> np.ndarray:
|
||||
maze: np.ndarray = np.array(
|
||||
[[Cell(value=0) for _ in range(width)] for _ in range(height)]
|
||||
@@ -41,46 +36,43 @@ class Kruskal(MazeGenerator):
|
||||
if x == height - 1:
|
||||
maze[x][y].set_south(True)
|
||||
if y == 0:
|
||||
maze[x][y].set_west(True)
|
||||
if y == width - 1:
|
||||
maze[x][y].set_est(True)
|
||||
if y == width - 1:
|
||||
maze[x][y].set_west(True)
|
||||
return maze
|
||||
|
||||
@staticmethod
|
||||
def is_in_same_set(sets: np.ndarray, wall: tuple[int, int]) -> bool:
|
||||
def is_in_same_set(sets: list[list[int]], wall: tuple[int, int]) -> bool:
|
||||
a, b = wall
|
||||
for set in sets:
|
||||
if a in set.cells and b in set.cells:
|
||||
if a in set and b in set:
|
||||
return True
|
||||
elif a in set.cells or b in set.cells:
|
||||
if a in set or b in set:
|
||||
return False
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def merge_sets(sets: np.ndarray, wall: tuple[int, int]) -> None:
|
||||
def merge_sets(sets: list[list[int]], wall: tuple[int, int]) -> None:
|
||||
a, b = wall
|
||||
base_set = None
|
||||
for i in range(len(sets)):
|
||||
if base_set is None and (a in sets[i].cells or b in sets[i].cells):
|
||||
base_set = sets[i]
|
||||
elif base_set and (a in sets[i].cells or b in sets[i].cells):
|
||||
base_set.cells += sets[i].cells
|
||||
np.delete(sets, i)
|
||||
return
|
||||
raise Exception("two sets not found")
|
||||
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)
|
||||
|
||||
def generator(
|
||||
self, height: int, width: int
|
||||
) -> Generator[np.ndarray, None, np.ndarray]:
|
||||
sets = np.array([self.Set([i]) for i in range(height * width)])
|
||||
sets = [[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 h in range(height - 1):
|
||||
for w in range(width):
|
||||
walls += [(w + (width * h), w + (width * (h + 1)))]
|
||||
print(walls)
|
||||
for w in range(width):
|
||||
for h in range(height - 1):
|
||||
walls += [(w + (width * h), w + (width * h) + width)]
|
||||
np.random.shuffle(walls)
|
||||
|
||||
yield self.walls_to_maze(walls, height, width)
|
||||
@@ -89,5 +81,121 @@ class Kruskal(MazeGenerator):
|
||||
self.merge_sets(sets, wall)
|
||||
walls.remove(wall)
|
||||
yield self.walls_to_maze(walls, height, width)
|
||||
print(f"nb sets: {len(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]:
|
||||
maze = DepthFirstSearch.init_maze(width, height)
|
||||
visited = np.zeros((height, width), dtype=bool)
|
||||
path = list()
|
||||
w_h = (width, height)
|
||||
coord = (0, 0)
|
||||
x, y = coord
|
||||
first = True
|
||||
|
||||
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
|
||||
if not path:
|
||||
break
|
||||
|
||||
wall = DepthFirstSearch.next_step(random_c)
|
||||
maze[y][x] = DepthFirstSearch.broken_wall(maze[y][x], wall)
|
||||
|
||||
coord = DepthFirstSearch.next_cell(x, y, wall)
|
||||
wall_r = DepthFirstSearch.reverse_path(wall)
|
||||
x, y = coord
|
||||
maze[y][x] = DepthFirstSearch.broken_wall(maze[y][x], wall_r)
|
||||
yield maze
|
||||
return maze
|
||||
|
||||
@staticmethod
|
||||
def init_maze(width: int, height: int) -> np.ndarray:
|
||||
maze = np.array([[Cell(value=15) for _ in range(width)]
|
||||
for _ in range(height)])
|
||||
return maze
|
||||
|
||||
@staticmethod
|
||||
def add_cell_visited(coord: tuple, path: set) -> list:
|
||||
path.append(coord)
|
||||
return path
|
||||
|
||||
@staticmethod
|
||||
def random_cells(visited: np.array, coord: tuple, w_h: tuple) -> list:
|
||||
rand_cell = []
|
||||
x, y = coord
|
||||
width, height = w_h
|
||||
|
||||
if y - 1 >= 0 and not visited[y - 1][x]:
|
||||
rand_cell.append("N")
|
||||
|
||||
if y + 1 < height and not visited[y + 1][x]:
|
||||
rand_cell.append("S")
|
||||
|
||||
if x - 1 >= 0 and not visited[y][x - 1]:
|
||||
rand_cell.append("W")
|
||||
|
||||
if x + 1 < width and not visited[y][x + 1]:
|
||||
rand_cell.append("E")
|
||||
return rand_cell
|
||||
|
||||
@staticmethod
|
||||
def next_step(rand_cell: list) -> str:
|
||||
return np.random.choice(rand_cell)
|
||||
|
||||
@staticmethod
|
||||
def broken_wall(cell: Cell, wall: str) -> Cell:
|
||||
if wall == "N":
|
||||
cell.set_north(False)
|
||||
elif wall == "S":
|
||||
cell.set_south(False)
|
||||
elif wall == "W":
|
||||
cell.set_west(False)
|
||||
elif wall == "E":
|
||||
cell.set_est(False)
|
||||
return cell
|
||||
|
||||
@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)
|
||||
}
|
||||
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"
|
||||
}
|
||||
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)
|
||||
while len(path) > 0:
|
||||
path.pop()
|
||||
if path:
|
||||
last = path[-1]
|
||||
r_cells = DepthFirstSearch.random_cells(visited, last, w_h)
|
||||
if r_cells:
|
||||
break
|
||||
return path
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
from .Cell import Cell
|
||||
from .Maze import Maze
|
||||
from .MazeGenerator import MazeGenerator, Kruskal
|
||||
from .MazeGenerator import MazeGenerator, DepthFirstSearch
|
||||
from .MazeGenerator import Kruskal
|
||||
from .MazeSolver import MazeSolver, AStar
|
||||
|
||||
__version__ = "1.0.0"
|
||||
__author__ = "us"
|
||||
__all__ = ["Cell", "Maze", "MazeGenerator", "MazeSolver", "AStar", "Kruskal"]
|
||||
__all__ = ["Cell", "Maze", "MazeGenerator",
|
||||
"MazeSolver", "AStar", "Kruskal", "DepthFirstSearch"]
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
from amaz_lib.MazeGenerator import DepthFirstSearch
|
||||
from amaz_lib.Cell import Cell
|
||||
import numpy as np
|
||||
|
||||
|
||||
class TestDepth:
|
||||
|
||||
def test_init_maze(self) -> None:
|
||||
maze = DepthFirstSearch.init_maze(10, 10)
|
||||
cell = Cell(value=15)
|
||||
maze[1][1].set_est(False)
|
||||
assert maze[0][0].value == cell.value
|
||||
|
||||
def test_rand_cells(self) -> None:
|
||||
w_h = (10, 10)
|
||||
lst = np.zeros((10, 10), dtype=bool)
|
||||
lst[0, 0] = True
|
||||
rand_cells = DepthFirstSearch.random_cells(lst, (0, 1), w_h)
|
||||
assert len(rand_cells) == 2
|
||||
|
||||
def test_next_cell(self) -> None:
|
||||
coord = (5, 4)
|
||||
x, y = coord
|
||||
assert DepthFirstSearch.next_cell(x, y, "N") == (2, 3)
|
||||
|
||||
def test_reverse_path(self) -> None:
|
||||
assert DepthFirstSearch.reverse_path("N") == "S"
|
||||
+1
-1
@@ -15,7 +15,7 @@ def test_maze_setter_getter() -> None:
|
||||
)
|
||||
|
||||
maze.set_maze(test)
|
||||
assert numpy.array_equal(maze.get_maze(), test) == True
|
||||
assert numpy.array_equal(maze.get_maze(), test) is True
|
||||
|
||||
|
||||
def test_maze_str() -> None:
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
import numpy
|
||||
from amaz_lib.MazeGenerator import Kruskal
|
||||
from amaz_lib.MazeGenerator import DepthFirstSearch
|
||||
|
||||
|
||||
def test_kruskal_output_shape() -> None:
|
||||
generator = Kruskal()
|
||||
maze = numpy.array([])
|
||||
for output in generator.generator(10, 10):
|
||||
maze = output
|
||||
class TestMazeGenerator:
|
||||
|
||||
assert maze.shape == (10, 10)
|
||||
def test_generator(self) -> None:
|
||||
w_h = (300, 300)
|
||||
maze = numpy.array([])
|
||||
generator = DepthFirstSearch().generator(*w_h)
|
||||
for output in generator:
|
||||
maze = output
|
||||
|
||||
assert maze.shape == w_h
|
||||
|
||||
Reference in New Issue
Block a user