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 | |||
| 8dc00e238a | |||
| 0f19d24736 | |||
| 8b4ef7afce | |||
| 030c6142ba |
@@ -23,3 +23,8 @@ run_test_parsing:
|
|||||||
|
|
||||||
run_test_dfs:
|
run_test_dfs:
|
||||||
PYTHONPATH=src uv run pytest tests/test_Depth.py
|
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
-8
@@ -1,22 +1,21 @@
|
|||||||
import os
|
import os
|
||||||
from numpy import ma
|
|
||||||
from src.amaz_lib import MazeGenerator
|
|
||||||
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, start=(1, 1), end=(16, 15))
|
maze = Maze(maze=None)
|
||||||
for alg in MazeGenerator.Kruskal.kruskal(20, 20):
|
gen = maze_gen.generator(100, 100)
|
||||||
|
for alg in gen:
|
||||||
maze.set_maze(alg)
|
maze.set_maze(alg)
|
||||||
os.system("clear")
|
os.system("clear")
|
||||||
maze.ascii_print()
|
maze.ascii_print()
|
||||||
maze.export_maze("test.txt")
|
|
||||||
|
|
||||||
|
|
||||||
# except Exception as err:
|
# except Exception as err:
|
||||||
# print(err)
|
# print(err)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main(g.DepthFirstSearch())
|
||||||
|
|||||||
@@ -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()
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ from typing import Generator
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
from .Cell import Cell
|
from .Cell import Cell
|
||||||
import math
|
import math
|
||||||
import random
|
|
||||||
|
|
||||||
|
|
||||||
class MazeGenerator(ABC):
|
class MazeGenerator(ABC):
|
||||||
@@ -85,31 +84,40 @@ class Kruskal(MazeGenerator):
|
|||||||
return self.walls_to_maze(walls, height, width)
|
return self.walls_to_maze(walls, height, width)
|
||||||
|
|
||||||
|
|
||||||
class DepthFirstSearch:
|
class DepthFirstSearch(MazeGenerator):
|
||||||
|
|
||||||
@staticmethod
|
def generator(self, width: int, height: int
|
||||||
def generator(width: int, height: int) -> np.ndarray:
|
) -> Generator[np.ndarray, None, np.ndarray]:
|
||||||
maze = DepthFirstSearch.init_maze(width, height)
|
maze = DepthFirstSearch.init_maze(width, height)
|
||||||
visited = []
|
visited = np.zeros((height, width), dtype=bool)
|
||||||
path = []
|
path = list()
|
||||||
w_h = (width, height)
|
w_h = (width, height)
|
||||||
coord = (0, 0)
|
coord = (0, 0)
|
||||||
while len(visited) < width * height:
|
x, y = coord
|
||||||
x, y = coord
|
first = True
|
||||||
rand_steps = DepthFirstSearch.random_cells(visited, coord, w_h)
|
|
||||||
if len(rand_steps) == 0:
|
while path or first:
|
||||||
path = DepthFirstSearch.back_on_step(path, w_h)
|
first = False
|
||||||
coord = DepthFirstSearch.last(path)
|
visited[y, x] = True
|
||||||
rand_steps = DepthFirstSearch.random_cells(path, coord, w_h)
|
|
||||||
x, y = coord
|
|
||||||
wall = DepthFirstSearch.next_step(rand_steps)
|
|
||||||
wall_r = DepthFirstSearch.reverse_path(wall)
|
|
||||||
maze[y][x] = DepthFirstSearch.broken_wall(maze[y][x], wall)
|
|
||||||
visited = DepthFirstSearch.add_cell_visited(coord, visited)
|
|
||||||
path = DepthFirstSearch.add_cell_visited(coord, path)
|
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)
|
coord = DepthFirstSearch.next_cell(x, y, wall)
|
||||||
|
wall_r = DepthFirstSearch.reverse_path(wall)
|
||||||
x, y = coord
|
x, y = coord
|
||||||
maze[y][x] = DepthFirstSearch.broken_wall(maze[y][x], wall_r)
|
maze[y][x] = DepthFirstSearch.broken_wall(maze[y][x], wall_r)
|
||||||
|
yield maze
|
||||||
return maze
|
return maze
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@@ -119,35 +127,32 @@ class DepthFirstSearch:
|
|||||||
return maze
|
return maze
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def add_cell_visited(coord: tuple, visited: list = []) -> list:
|
def add_cell_visited(coord: tuple, path: set) -> list:
|
||||||
visited.append(coord)
|
path.append(coord)
|
||||||
return visited
|
return path
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def random_cells(visited: list, coord: tuple, w_h: tuple) -> list:
|
def random_cells(visited: np.array, coord: tuple, w_h: tuple) -> list:
|
||||||
rand_cell = []
|
rand_cell = []
|
||||||
x, y = coord
|
x, y = coord
|
||||||
width, height = w_h
|
width, height = w_h
|
||||||
# NORTH
|
|
||||||
if y - 1 >= 0 and (x, y - 1) not in visited:
|
if y - 1 >= 0 and not visited[y - 1][x]:
|
||||||
rand_cell.append("N")
|
rand_cell.append("N")
|
||||||
|
|
||||||
# SOUTH
|
if y + 1 < height and not visited[y + 1][x]:
|
||||||
if y + 1 < height and (x, y + 1) not in visited:
|
|
||||||
rand_cell.append("S")
|
rand_cell.append("S")
|
||||||
|
|
||||||
# WEST
|
if x - 1 >= 0 and not visited[y][x - 1]:
|
||||||
if x - 1 >= 0 and (x - 1, y) not in visited:
|
|
||||||
rand_cell.append("W")
|
rand_cell.append("W")
|
||||||
|
|
||||||
# EAST
|
if x + 1 < width and not visited[y][x + 1]:
|
||||||
if x + 1 < width and (x + 1, y) not in visited:
|
|
||||||
rand_cell.append("E")
|
rand_cell.append("E")
|
||||||
return rand_cell
|
return rand_cell
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def next_step(rand_cell: list) -> str:
|
def next_step(rand_cell: list) -> str:
|
||||||
return random.choice(rand_cell)
|
return np.random.choice(rand_cell)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def broken_wall(cell: Cell, wall: str) -> Cell:
|
def broken_wall(cell: Cell, wall: str) -> Cell:
|
||||||
@@ -172,6 +177,7 @@ class DepthFirstSearch:
|
|||||||
add_x, add_y = next_step[next]
|
add_x, add_y = next_step[next]
|
||||||
return (x + add_x, y + add_y)
|
return (x + add_x, y + add_y)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
def reverse_path(next: str) -> str:
|
def reverse_path(next: str) -> str:
|
||||||
reverse = {
|
reverse = {
|
||||||
"N": "S",
|
"N": "S",
|
||||||
@@ -182,14 +188,14 @@ class DepthFirstSearch:
|
|||||||
return reverse[next]
|
return reverse[next]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def last(path: list):
|
def back_on_step(path: list, w_h: tuple, visited: np.array) -> list:
|
||||||
return path[len(path) - 1]
|
last = path[-1]
|
||||||
|
r_cells = DepthFirstSearch.random_cells(visited, last, w_h)
|
||||||
def back_on_step(path: list, w_h: tuple) -> list:
|
while len(path) > 0:
|
||||||
last = DepthFirstSearch.last(path)
|
path.pop()
|
||||||
r_cells = DepthFirstSearch.random_cells(path, last, w_h)
|
if path:
|
||||||
while len(r_cells == 0):
|
last = path[-1]
|
||||||
path.pop(len(path) - 1)
|
r_cells = DepthFirstSearch.random_cells(visited, last, w_h)
|
||||||
last = DepthFirstSearch.last(path)
|
if r_cells:
|
||||||
r_cells = DepthFirstSearch.random_cells(path, last, w_h)
|
break
|
||||||
return path
|
return path
|
||||||
|
|||||||
@@ -1,7 +1,134 @@
|
|||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from .Maze import Maze
|
from .Maze import Maze
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
class MazeSolver(ABC):
|
class MazeSolver(ABC):
|
||||||
|
def __init__(self, start: tuple[int, int], end: tuple[int, int]) -> None:
|
||||||
|
self.start = (start[0] - 1, start[1] - 1)
|
||||||
|
self.end = (end[0] - 1, end[1] - 1)
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def solve(self, maze: Maze) -> str: ...
|
def solve(self, maze: Maze) -> str: ...
|
||||||
|
|
||||||
|
|
||||||
|
class AStar(MazeSolver):
|
||||||
|
|
||||||
|
def __init__(self, start: tuple[int, int], end: tuple[int, int]) -> None:
|
||||||
|
super().__init__(start, end)
|
||||||
|
|
||||||
|
def f(self, n):
|
||||||
|
def g(n: tuple[int, int]) -> int:
|
||||||
|
res = 0
|
||||||
|
if n[0] < self.start[0]:
|
||||||
|
res += self.start[0] - n[0]
|
||||||
|
else:
|
||||||
|
res += n[0] - self.start[0]
|
||||||
|
if n[1] < self.start[1]:
|
||||||
|
res += self.start[1] - n[1]
|
||||||
|
else:
|
||||||
|
res += n[1] - self.start[1]
|
||||||
|
return res
|
||||||
|
|
||||||
|
def h(n: tuple[int, int]) -> int:
|
||||||
|
res = 0
|
||||||
|
if n[0] < self.end[0]:
|
||||||
|
res += self.end[0] - n[0]
|
||||||
|
else:
|
||||||
|
res += n[0] - self.end[0]
|
||||||
|
if n[1] < self.end[1]:
|
||||||
|
res += self.end[1] - n[1]
|
||||||
|
else:
|
||||||
|
res += n[1] - self.end[1]
|
||||||
|
return res
|
||||||
|
|
||||||
|
try:
|
||||||
|
return g(n) + h(n)
|
||||||
|
except Exception:
|
||||||
|
return 1000
|
||||||
|
|
||||||
|
def best_path(
|
||||||
|
self, maze: np.ndarray, actual: tuple[int, int]
|
||||||
|
) -> dict[str, int | None]:
|
||||||
|
print(actual)
|
||||||
|
path = {
|
||||||
|
"N": (
|
||||||
|
self.f((actual[0], actual[1] - 1))
|
||||||
|
if not maze[actual[0]][actual[1]].get_north() and actual[1] > 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
|
||||||
|
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
|
||||||
|
else None
|
||||||
|
),
|
||||||
|
"W": (
|
||||||
|
self.f((actual[0] - 1, actual[1]))
|
||||||
|
if not maze[actual[0]][actual[1]].get_west() and actual[0] > 0
|
||||||
|
else None
|
||||||
|
),
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
k: v for k, v in sorted(path.items(), key=lambda item: item[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
def get_opposit(self, dir: str) -> str:
|
||||||
|
match dir:
|
||||||
|
case "N":
|
||||||
|
return "S"
|
||||||
|
case "E":
|
||||||
|
return "W"
|
||||||
|
case "S":
|
||||||
|
return "N"
|
||||||
|
case "W":
|
||||||
|
return "E"
|
||||||
|
case _:
|
||||||
|
return ""
|
||||||
|
|
||||||
|
def get_next_pos(
|
||||||
|
self, dir: str, actual: tuple[int, int]
|
||||||
|
) -> tuple[int, int]:
|
||||||
|
match dir:
|
||||||
|
case "N":
|
||||||
|
return (actual[0], actual[1] - 1)
|
||||||
|
case "E":
|
||||||
|
return (actual[0] + 1, actual[1])
|
||||||
|
case "S":
|
||||||
|
return (actual[0], actual[1] + 1)
|
||||||
|
case "W":
|
||||||
|
return (actual[0] - 1, actual[1])
|
||||||
|
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
|
||||||
|
return None
|
||||||
|
|
||||||
|
def solve(self, maze: Maze) -> str:
|
||||||
|
print(maze)
|
||||||
|
res = self.get_path(self.start, maze.get_maze(), None)
|
||||||
|
if res is None:
|
||||||
|
raise Exception("Path not found")
|
||||||
|
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, DepthFirstSearch
|
from .MazeGenerator import MazeGenerator, DepthFirstSearch
|
||||||
from .MazeSolver import MazeSolver
|
from .MazeGenerator import Kruskal
|
||||||
|
from .MazeSolver import MazeSolver, AStar
|
||||||
|
|
||||||
__version__ = "1.0.0"
|
__version__ = "1.0.0"
|
||||||
__author__ = "us"
|
__author__ = "us"
|
||||||
__all__ = ["Cell", "Maze", "MazeGenerator", "MazeSolver", "DepthFirstSearch"]
|
__all__ = ["Cell", "Maze", "MazeGenerator",
|
||||||
|
"MazeSolver", "AStar", "Kruskal", "DepthFirstSearch"]
|
||||||
|
|||||||
+4
-10
@@ -1,5 +1,6 @@
|
|||||||
from amaz_lib.MazeGenerator import DepthFirstSearch
|
from amaz_lib.MazeGenerator import DepthFirstSearch
|
||||||
from amaz_lib.Cell import Cell
|
from amaz_lib.Cell import Cell
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
class TestDepth:
|
class TestDepth:
|
||||||
@@ -12,22 +13,15 @@ class TestDepth:
|
|||||||
|
|
||||||
def test_rand_cells(self) -> None:
|
def test_rand_cells(self) -> None:
|
||||||
w_h = (10, 10)
|
w_h = (10, 10)
|
||||||
lst = DepthFirstSearch.add_cell_visited((0, 0))
|
lst = np.zeros((10, 10), dtype=bool)
|
||||||
|
lst[0, 0] = True
|
||||||
rand_cells = DepthFirstSearch.random_cells(lst, (0, 1), w_h)
|
rand_cells = DepthFirstSearch.random_cells(lst, (0, 1), w_h)
|
||||||
assert len(rand_cells) == 2
|
assert len(rand_cells) == 2
|
||||||
|
|
||||||
def test_next_cell(self) -> None:
|
def test_next_cell(self) -> None:
|
||||||
coord = (5, 4)
|
coord = (5, 4)
|
||||||
x, y = coord
|
x, y = coord
|
||||||
assert DepthFirstSearch.next_cell(x, y, "N") == (5, 3)
|
assert DepthFirstSearch.next_cell(x, y, "N") == (2, 3)
|
||||||
|
|
||||||
def test_reverse_path(self) -> None:
|
def test_reverse_path(self) -> None:
|
||||||
assert DepthFirstSearch.reverse_path("N") == "S"
|
assert DepthFirstSearch.reverse_path("N") == "S"
|
||||||
|
|
||||||
def test_last(self) -> None:
|
|
||||||
lst = [(0, 0), (1, 1)]
|
|
||||||
assert DepthFirstSearch.last(lst) == (1, 1)
|
|
||||||
|
|
||||||
def test_BOS(self) -> None:
|
|
||||||
path = [(0, 0), (0, 2), (1, 1)]
|
|
||||||
assert len(DepthFirstSearch.random_cells(path, (0, 1), (10, 10))) == 0
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
from amaz_lib.Cell import Cell
|
||||||
|
import numpy as np
|
||||||
|
from amaz_lib import AStar, Maze, MazeSolver
|
||||||
|
|
||||||
|
|
||||||
|
def test_solver() -> None:
|
||||||
|
maze = Maze(
|
||||||
|
np.array(
|
||||||
|
[
|
||||||
|
[Cell(value=13), Cell(value=3), Cell(value=11)],
|
||||||
|
[Cell(value=9), Cell(value=4), Cell(value=6)],
|
||||||
|
[Cell(value=12), Cell(value=5), Cell(value=7)],
|
||||||
|
]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
print(maze)
|
||||||
|
solver = AStar((1, 1), (3, 3))
|
||||||
|
res = solver.solve(maze)
|
||||||
|
assert res == "ESWSEE"
|
||||||
Reference in New Issue
Block a user