mirror of
https://github.com/maoakeEnterprise/amazing.git
synced 2026-04-29 00:14:34 +02:00
Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e717bf52e9 | |||
| 3fa0d3204e | |||
| cc6f2eb147 | |||
| c6242eeec0 | |||
| 4055a8a7a2 | |||
| a39f348b1e | |||
| 03c4d206d6 | |||
| 8eb46f601f | |||
| 991cdead51 | |||
| 6730ebcdb5 | |||
| c478400640 | |||
| 993bcce857 | |||
| a85e342a0a | |||
| 4d151664ab | |||
| 8b4ef7afce | |||
| 030c6142ba | |||
| f8f0e31598 | |||
| e75e14110d |
@@ -18,5 +18,13 @@ lint-strict:
|
|||||||
uv run flake8 .
|
uv run flake8 .
|
||||||
uv run mypy . --strict
|
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:
|
run_test:
|
||||||
uv run pytest
|
uv run pytest
|
||||||
|
|||||||
+7
-7
@@ -1,17 +1,17 @@
|
|||||||
import os
|
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 Maze
|
||||||
|
from src.amaz_lib import MazeGenerator
|
||||||
|
import src.amaz_lib as g
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main(maze_gen: MazeGenerator) -> None:
|
||||||
# try:
|
# try:
|
||||||
maze = Maze(maze=None)
|
maze = Maze(maze=None)
|
||||||
generator = Kruskal()
|
for alg in maze_gen.generator(21, 21):
|
||||||
for alg in generator.generator(20, 20):
|
|
||||||
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))
|
||||||
|
|
||||||
@@ -21,4 +21,4 @@ def main() -> None:
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main(g.Kruskal())
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
from pydantic import BaseModel, Field
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
|
||||||
class Cell(BaseModel):
|
@dataclass
|
||||||
value: int = Field(ge=0, le=15)
|
class Cell:
|
||||||
|
def __init__(self, value: int) -> None:
|
||||||
|
self.value = value
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return hex(self.value).removeprefix("0x").upper()
|
return hex(self.value).removeprefix("0x").upper()
|
||||||
|
|||||||
+196
-19
@@ -1,5 +1,4 @@
|
|||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from dataclasses import dataclass
|
|
||||||
from typing import Generator, Set
|
from typing import Generator, Set
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from .Cell import Cell
|
from .Cell import Cell
|
||||||
@@ -9,15 +8,43 @@ 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:
|
||||||
def __init__(self, cells: list[int]) -> None:
|
def __init__(self, cells: list[int]) -> None:
|
||||||
self.cells: list[int] = cells
|
self.cells: list[int] = cells
|
||||||
|
|
||||||
|
class Sets:
|
||||||
|
def __init__(self, sets: list[Set]) -> None:
|
||||||
|
self.sets = sets
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def walls_to_maze(
|
def walls_to_maze(
|
||||||
walls: np.ndarray, height: int, width: int
|
walls: np.ndarray, height: int, width: int
|
||||||
@@ -47,9 +74,9 @@ class Kruskal(MazeGenerator):
|
|||||||
return maze
|
return maze
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def is_in_same_set(sets: np.ndarray, wall: tuple[int, int]) -> bool:
|
def is_in_same_set(sets: Sets, wall: tuple[int, int]) -> bool:
|
||||||
a, b = wall
|
a, b = wall
|
||||||
for set in sets:
|
for set in sets.sets:
|
||||||
if a in set.cells and b in set.cells:
|
if a in set.cells and b in set.cells:
|
||||||
return True
|
return True
|
||||||
elif a in set.cells or b in set.cells:
|
elif a in set.cells or b in set.cells:
|
||||||
@@ -57,22 +84,44 @@ class Kruskal(MazeGenerator):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def merge_sets(sets: np.ndarray, wall: tuple[int, int]) -> None:
|
def merge_sets(sets: Sets, wall: tuple[int, int]) -> None:
|
||||||
a, b = wall
|
a, b = wall
|
||||||
base_set = None
|
base_set = None
|
||||||
for i in range(len(sets)):
|
for i in range(len(sets.sets)):
|
||||||
if base_set is None and (a in sets[i].cells or b in sets[i].cells):
|
if base_set is None and (
|
||||||
base_set = sets[i]
|
a in sets.sets[i].cells or b in sets.sets[i].cells
|
||||||
elif base_set and (a in sets[i].cells or b in sets[i].cells):
|
):
|
||||||
base_set.cells += sets[i].cells
|
base_set = sets.sets[i]
|
||||||
np.delete(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
|
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]:
|
||||||
sets = np.array([self.Set([i]) for i in range(height * width)])
|
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)])
|
||||||
walls = []
|
walls = []
|
||||||
for h in range(height):
|
for h in range(height):
|
||||||
for w in range(width - 1):
|
for w in range(width - 1):
|
||||||
@@ -84,10 +133,138 @@ 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)
|
||||||
for wall in walls:
|
while (len(sets.sets) != 1 and cells_ft is None) or (
|
||||||
if not self.is_in_same_set(sets, wall):
|
len(sets.sets) != 19 and cells_ft is not None
|
||||||
self.merge_sets(sets, wall)
|
):
|
||||||
walls.remove(wall)
|
for wall in walls:
|
||||||
yield self.walls_to_maze(walls, height, width)
|
if not self.is_in_same_set(sets, wall) and not self.touch_ft(
|
||||||
print(f"nb sets: {len(sets)}")
|
width, wall, cells_ft
|
||||||
|
):
|
||||||
|
self.merge_sets(sets, wall)
|
||||||
|
walls.remove(wall)
|
||||||
|
yield self.walls_to_maze(walls, height, width)
|
||||||
|
if (len(sets.sets) == 1 and cells_ft is None) or (
|
||||||
|
len(sets.sets) == 19 and cells_ft is not None
|
||||||
|
):
|
||||||
|
break
|
||||||
|
print(f"nb sets: {len(sets.sets)}")
|
||||||
return self.walls_to_maze(walls, height, width)
|
return self.walls_to_maze(walls, height, width)
|
||||||
|
|
||||||
|
|
||||||
|
class DepthFirstSearch(MazeGenerator):
|
||||||
|
|
||||||
|
def generator(
|
||||||
|
self, height: int, width: int, seed: int = None
|
||||||
|
) -> Generator[np.ndarray, None, np.ndarray]:
|
||||||
|
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_iteration = True
|
||||||
|
|
||||||
|
while path or first_iteration:
|
||||||
|
first_iteration = False
|
||||||
|
|
||||||
|
visited[y, x] = True
|
||||||
|
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 = self.next_step(random_c)
|
||||||
|
maze[y][x] = self.broken_wall(maze[y][x], wall)
|
||||||
|
|
||||||
|
coord = self.next_cell(x, y, wall)
|
||||||
|
wall_r = self.reverse_path(wall)
|
||||||
|
x, y = coord
|
||||||
|
maze[y][x] = self.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(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:
|
||||||
|
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
|
||||||
|
|||||||
+55
-29
@@ -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[0], actual[1] - 1))
|
self.f((actual[0], actual[1] - 1))
|
||||||
if not maze[actual[0]][actual[1]].get_north() and actual[1] > 0
|
if not maze[actual[1]][actual[0]].get_north() and actual[1] > 0
|
||||||
else None
|
else None
|
||||||
),
|
),
|
||||||
"E": (
|
"E": (
|
||||||
self.f((actual[0] + 1, actual[1]))
|
self.f((actual[0] + 1, actual[1]))
|
||||||
if not maze[actual[0]][actual[1]].get_est()
|
if not maze[actual[1]][actual[0]].get_est()
|
||||||
and actual[0] < len(maze) - 1
|
and actual[0] < len(maze[0]) - 1
|
||||||
else None
|
else None
|
||||||
),
|
),
|
||||||
"S": (
|
"S": (
|
||||||
self.f((actual[0], actual[1] + 1))
|
self.f((actual[0], actual[1] + 1))
|
||||||
if not maze[actual[0]][actual[1]].get_south()
|
if not maze[actual[1]][actual[0]].get_south()
|
||||||
and actual[1] < len(maze[0]) - 1
|
and actual[1] < len(maze) - 1
|
||||||
else None
|
else None
|
||||||
),
|
),
|
||||||
"W": (
|
"W": (
|
||||||
self.f((actual[0] - 1, actual[1]))
|
self.f((actual[0] - 1, actual[1]))
|
||||||
if not maze[actual[0]][actual[1]].get_west() and actual[0] > 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:
|
||||||
@@ -107,28 +111,50 @@ class AStar(MazeSolver):
|
|||||||
case _:
|
case _:
|
||||||
return actual
|
return actual
|
||||||
|
|
||||||
def get_path(
|
def get_path(self, maze: np.ndarray) -> str | None:
|
||||||
self, actual: tuple[int, int], maze: np.ndarray, pre: str | None
|
path = [(self.start, self.best_path(maze, self.start, None))]
|
||||||
) -> str | None:
|
visited = [self.start]
|
||||||
if actual == self.end:
|
while len(path) > 0 and path[-1][0] != self.end:
|
||||||
return ""
|
print(path[-1])
|
||||||
paths = self.best_path(maze, actual)
|
if len(path[-1][1]) == 0:
|
||||||
for path in paths:
|
path.pop(-1)
|
||||||
if paths[path] is None:
|
if len(path) == 0:
|
||||||
|
break
|
||||||
|
k = next(iter(path[-1][1]))
|
||||||
|
path[-1][1].pop(k)
|
||||||
continue
|
continue
|
||||||
if path != pre:
|
|
||||||
temp = self.get_path(
|
while len(path[-1][1]) > 0:
|
||||||
self.get_next_pos(path, actual),
|
next_pos = self.get_next_pos(
|
||||||
maze,
|
list(path[-1][1].keys())[0], path[-1][0]
|
||||||
self.get_opposit(path),
|
|
||||||
)
|
)
|
||||||
if not temp is None:
|
if next_pos in visited:
|
||||||
return path + temp
|
k = next(iter(path[-1][1]))
|
||||||
return None
|
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
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
from .Cell import Cell
|
from .Cell import Cell
|
||||||
from .Maze import Maze
|
from .Maze import Maze
|
||||||
from .MazeGenerator import MazeGenerator, Kruskal
|
from .MazeGenerator import MazeGenerator, DepthFirstSearch
|
||||||
|
from .MazeGenerator import Kruskal
|
||||||
from .MazeSolver import MazeSolver, AStar
|
from .MazeSolver import MazeSolver, AStar
|
||||||
|
|
||||||
__version__ = "1.0.0"
|
__version__ = "1.0.0"
|
||||||
__author__ = "us"
|
__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)
|
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:
|
def test_maze_str() -> None:
|
||||||
|
|||||||
@@ -1,11 +1,14 @@
|
|||||||
import numpy
|
import numpy
|
||||||
from amaz_lib.MazeGenerator import Kruskal
|
from amaz_lib.MazeGenerator import DepthFirstSearch
|
||||||
|
|
||||||
|
|
||||||
def test_kruskal_output_shape() -> None:
|
class TestMazeGenerator:
|
||||||
generator = Kruskal()
|
|
||||||
maze = numpy.array([])
|
|
||||||
for output in generator.generator(10, 10):
|
|
||||||
maze = output
|
|
||||||
|
|
||||||
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