mirror of
https://github.com/maoakeEnterprise/amazing.git
synced 2026-04-29 08:24:35 +02:00
Compare commits
21 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a79d4e5c3b | |||
| 8dc00e238a | |||
| 0f19d24736 | |||
| 22c44333c1 | |||
| b00ddc7d29 | |||
| e4e8ebfc13 | |||
| 0c20d2d063 | |||
| 96b39fbeea | |||
| 97b35fe3eb | |||
| ac13df160f | |||
| d2d477d1b5 | |||
| 721a7d00b9 | |||
| 6d849a8121 | |||
| a4d55d5692 | |||
| c6c7e6e47e | |||
| 84c4b63a6c | |||
| c14c043575 | |||
| c8a13e9d5c | |||
| 272ccefb52 | |||
| b44cffec2c | |||
| ce584d2ae3 |
@@ -11,7 +11,7 @@ clean:
|
|||||||
rm -rf __pycache__ .mypy_cache .venv
|
rm -rf __pycache__ .mypy_cache .venv
|
||||||
|
|
||||||
lint:
|
lint:
|
||||||
uv run flake8 .
|
uv run flake8 . --exclude=.venv
|
||||||
uv run mypy . --warn-return-any --warn-unused-ignores --ignore-missing-imports --disallow-untyped-defs --check-untyped-defs
|
uv run mypy . --warn-return-any --warn-unused-ignores --ignore-missing-imports --disallow-untyped-defs --check-untyped-defs
|
||||||
|
|
||||||
lint-strict:
|
lint-strict:
|
||||||
@@ -19,4 +19,4 @@ lint-strict:
|
|||||||
uv run mypy . --strict
|
uv run mypy . --strict
|
||||||
|
|
||||||
run_test:
|
run_test:
|
||||||
PYTHONPATH=src uv run python3 test/test_parsing.py
|
uv run pytest
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
The Randomized Kruskal's Algorithm
|
||||||
|
|
||||||
|
The Randomized Prim's Algorithm
|
||||||
|
|||||||
+19
-1
@@ -1,5 +1,23 @@
|
|||||||
|
import os
|
||||||
|
from numpy import ma
|
||||||
|
from src.amaz_lib import MazeGenerator, Kruskal, AStar
|
||||||
|
from src.amaz_lib import Maze
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
print("A-Maze-ing !!!")
|
# try:
|
||||||
|
maze = Maze(maze=None)
|
||||||
|
generator = Kruskal()
|
||||||
|
for alg in generator.generator(20, 20):
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
# This script does not check for errors or malformed files.
|
||||||
|
# It only validates that neighbooring cells sharing a wall have
|
||||||
|
# both the correct encoding.
|
||||||
|
# Usage: python3 output_validator.py output_maze.txt
|
||||||
|
|
||||||
|
import sys
|
||||||
|
|
||||||
|
if len(sys.argv) != 2:
|
||||||
|
print(f"Usage: python3 {sys.argv[0]} <output_file>")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
g = []
|
||||||
|
for line in open(sys.argv[1]):
|
||||||
|
if line.strip() == '':
|
||||||
|
break
|
||||||
|
g.append([int(c, 16) for c in line.strip(' \t\n\r')])
|
||||||
|
|
||||||
|
for r in range(len(g)):
|
||||||
|
for c in range(len(g[0])):
|
||||||
|
v = g[r][c]
|
||||||
|
if not all([(r < 1 or v & 1 == (g[r-1][c] >> 2) & 1),
|
||||||
|
(c >= len(g[0])-1 or (v >> 1) & 1 == (g[r][c+1] >> 3) & 1),
|
||||||
|
(r >= len(g)-1 or (v >> 2) & 1 == g[r+1][c] & 1),
|
||||||
|
(c < 1 or (v >> 3) & 1 == (g[r][c-1] >> 1) & 1)]):
|
||||||
|
print(f'Wrong encoding for ({c},{r})')
|
||||||
+4
-5
@@ -7,7 +7,6 @@ requires-python = ">=3.10"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"numpy>=2.2.6",
|
"numpy>=2.2.6",
|
||||||
"pydantic>=2.12.5",
|
"pydantic>=2.12.5",
|
||||||
"pytest>=9.0.2",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@@ -15,12 +14,12 @@ dependencies = [
|
|||||||
dev = [
|
dev = [
|
||||||
"mypy>=1.19.1",
|
"mypy>=1.19.1",
|
||||||
"flake8>=7.3.0",
|
"flake8>=7.3.0",
|
||||||
|
"pytest>=9.0.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
[tool.mypy]
|
[tool.mypy]
|
||||||
python_version = "3.10"
|
python_version = "3.10"
|
||||||
exclude = [
|
|
||||||
".venv",
|
[tool.pytest.ini_options]
|
||||||
"venv",
|
pythonpath = ["src"]
|
||||||
]
|
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
from dataclasses import field
|
||||||
|
from os import eventfd_read
|
||||||
|
from typing import Generator
|
||||||
|
import numpy
|
||||||
|
from typing_extensions import Self
|
||||||
|
from pydantic import AfterValidator, BaseModel, Field, model_validator
|
||||||
|
|
||||||
|
from amaz_lib import Maze, MazeGenerator, MazeSolver
|
||||||
|
from amaz_lib.Cell import Cell
|
||||||
|
|
||||||
|
|
||||||
|
class AMazeIng(BaseModel):
|
||||||
|
width: int = Field(ge=3)
|
||||||
|
height: int = Field(ge=3)
|
||||||
|
entry: tuple[int, int]
|
||||||
|
exit: tuple[int, int]
|
||||||
|
output_file: str = Field(min_length=3)
|
||||||
|
perfect: bool = Field(default=True)
|
||||||
|
maze: Maze = Field(default=Maze(maze=numpy.array([])))
|
||||||
|
generator: MazeGenerator
|
||||||
|
solver: MazeSolver
|
||||||
|
|
||||||
|
@model_validator(mode="after")
|
||||||
|
def check_entry_exit(self) -> Self:
|
||||||
|
if self.entry[0] >= self.width or self.entry[1] >= self.height:
|
||||||
|
raise ValueError("Entry coordinates exceed the maze size")
|
||||||
|
if self.exit[0] >= self.width or self.exit[1] >= self.height:
|
||||||
|
raise ValueError("Exit coordinates exceed the maze size")
|
||||||
|
return self
|
||||||
|
|
||||||
|
def generate(self) -> Generator[Maze, None, None]:
|
||||||
|
for array in self.generator.generator(self.height, self.width):
|
||||||
|
self.maze.set_maze(array)
|
||||||
|
yield self.maze
|
||||||
|
|
||||||
|
def solve_path(self) -> str:
|
||||||
|
return self.solver.solve(self.maze)
|
||||||
|
|
||||||
|
def __str__(self) -> str:
|
||||||
|
res = self.maze.__str__()
|
||||||
|
res += "\n"
|
||||||
|
res += f"{self.entry[0]},{self.entry[1]}\n"
|
||||||
|
res += f"{self.exit[0]},{self.exit[1]}\n"
|
||||||
|
res += self.solve_path()
|
||||||
|
res += "\n"
|
||||||
|
return res
|
||||||
@@ -5,11 +5,17 @@ class Cell(BaseModel):
|
|||||||
value: int = Field(ge=0, le=15)
|
value: int = Field(ge=0, le=15)
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return hex(self.value)
|
return hex(self.value).removeprefix("0x").upper()
|
||||||
|
|
||||||
|
def set_value(self, value: int) -> None:
|
||||||
|
self.value = value
|
||||||
|
|
||||||
|
def get_value(self) -> int:
|
||||||
|
return self.value
|
||||||
|
|
||||||
def set_north(self, is_wall: bool) -> None:
|
def set_north(self, is_wall: bool) -> None:
|
||||||
if (is_wall and self.value | 14 == 15) or (
|
if (not is_wall and self.value | 14 == 15) or (
|
||||||
not is_wall and self.value | 14 != 15
|
is_wall and self.value | 14 != 15
|
||||||
):
|
):
|
||||||
self.value = self.value ^ (1)
|
self.value = self.value ^ (1)
|
||||||
|
|
||||||
@@ -17,8 +23,8 @@ class Cell(BaseModel):
|
|||||||
return self.value & 1 == 1
|
return self.value & 1 == 1
|
||||||
|
|
||||||
def set_est(self, is_wall: bool) -> None:
|
def set_est(self, is_wall: bool) -> None:
|
||||||
if (is_wall and self.value | 13 == 15) or (
|
if (not is_wall and self.value | 13 == 15) or (
|
||||||
not is_wall and self.value | 13 != 15
|
is_wall and self.value | 13 != 15
|
||||||
):
|
):
|
||||||
self.value = self.value ^ (2)
|
self.value = self.value ^ (2)
|
||||||
|
|
||||||
@@ -26,8 +32,8 @@ class Cell(BaseModel):
|
|||||||
return self.value & 2 == 2
|
return self.value & 2 == 2
|
||||||
|
|
||||||
def set_south(self, is_wall: bool) -> None:
|
def set_south(self, is_wall: bool) -> None:
|
||||||
if (is_wall and self.value | 11 == 15) or (
|
if (not is_wall and self.value | 11 == 15) or (
|
||||||
not is_wall and self.value | 11 != 15
|
is_wall and self.value | 11 != 15
|
||||||
):
|
):
|
||||||
self.value = self.value ^ (4)
|
self.value = self.value ^ (4)
|
||||||
|
|
||||||
@@ -35,25 +41,10 @@ class Cell(BaseModel):
|
|||||||
return self.value & 4 == 4
|
return self.value & 4 == 4
|
||||||
|
|
||||||
def set_west(self, is_wall: bool) -> None:
|
def set_west(self, is_wall: bool) -> None:
|
||||||
if (is_wall and self.value | 8 == 15) or (
|
if (not is_wall and self.value | 7 == 15) or (
|
||||||
not is_wall and self.value | 8 != 15
|
is_wall and self.value | 7 != 15
|
||||||
):
|
):
|
||||||
self.value = self.value ^ (8)
|
self.value = self.value ^ (8)
|
||||||
|
|
||||||
def get_west(self) -> bool:
|
def get_west(self) -> bool:
|
||||||
return self.value & 8 == 8
|
return self.value & 8 == 8
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
|
||||||
c = Cell(value=1)
|
|
||||||
print(c.get_north())
|
|
||||||
c.set_north(True)
|
|
||||||
print(c.get_north())
|
|
||||||
c.set_north(True)
|
|
||||||
print(c.get_north())
|
|
||||||
c.set_north(False)
|
|
||||||
print(c.get_north())
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
import numpy
|
||||||
|
from .Cell import Cell
|
||||||
|
from .MazeGenerator import MazeGenerator
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Maze:
|
||||||
|
maze: numpy.ndarray
|
||||||
|
|
||||||
|
def get_maze(self) -> numpy.ndarray | None:
|
||||||
|
return self.maze
|
||||||
|
|
||||||
|
def set_maze(self, new_maze: numpy.ndarray) -> None:
|
||||||
|
self.maze = new_maze
|
||||||
|
|
||||||
|
def __str__(self) -> str:
|
||||||
|
if self.maze is None:
|
||||||
|
return "None"
|
||||||
|
res = ""
|
||||||
|
for line in self.maze:
|
||||||
|
for cell in line:
|
||||||
|
res += cell.__str__()
|
||||||
|
res += "\n"
|
||||||
|
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:
|
||||||
|
for cell in line:
|
||||||
|
if cell is line[0] and cell.get_west():
|
||||||
|
print("|", end="")
|
||||||
|
if cell.get_south() is True:
|
||||||
|
print("__", end="")
|
||||||
|
else:
|
||||||
|
print(" ", end="")
|
||||||
|
if cell.get_est() is True:
|
||||||
|
print("|", end="")
|
||||||
|
else:
|
||||||
|
print("_", end="")
|
||||||
|
print()
|
||||||
@@ -0,0 +1,93 @@
|
|||||||
|
from abc import ABC, abstractmethod
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Generator, Set
|
||||||
|
import numpy as np
|
||||||
|
from .Cell import Cell
|
||||||
|
import math
|
||||||
|
|
||||||
|
|
||||||
|
class MazeGenerator(ABC):
|
||||||
|
@abstractmethod
|
||||||
|
def generator(
|
||||||
|
self, height: int, width: int
|
||||||
|
) -> Generator[np.ndarray, None, np.ndarray]: ...
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
) -> np.ndarray:
|
||||||
|
maze: np.ndarray = np.array(
|
||||||
|
[[Cell(value=0) for _ in range(width)] for _ in range(height)]
|
||||||
|
)
|
||||||
|
for wall in walls:
|
||||||
|
x, y = wall
|
||||||
|
match y - x:
|
||||||
|
case 1:
|
||||||
|
maze[math.trunc((x / width))][x % width].set_est(True)
|
||||||
|
maze[math.trunc((y / width))][y % width].set_west(True)
|
||||||
|
case width:
|
||||||
|
maze[math.trunc((x / width))][x % width].set_south(True)
|
||||||
|
maze[math.trunc((y / width))][y % width].set_north(True)
|
||||||
|
for x in range(height):
|
||||||
|
for y in range(width):
|
||||||
|
if x == 0:
|
||||||
|
maze[x][y].set_north(True)
|
||||||
|
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)
|
||||||
|
return maze
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def is_in_same_set(sets: np.ndarray, wall: tuple[int, int]) -> bool:
|
||||||
|
a, b = wall
|
||||||
|
for set in sets:
|
||||||
|
if a in set.cells and b in set.cells:
|
||||||
|
return True
|
||||||
|
elif a in set.cells or b in set.cells:
|
||||||
|
return False
|
||||||
|
return False
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def merge_sets(sets: np.ndarray, 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")
|
||||||
|
|
||||||
|
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)])
|
||||||
|
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)
|
||||||
|
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)
|
||||||
|
print(f"nb sets: {len(sets)}")
|
||||||
|
return self.walls_to_maze(walls, height, width)
|
||||||
@@ -0,0 +1,134 @@
|
|||||||
|
from abc import ABC, abstractmethod
|
||||||
|
from .Maze import Maze
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
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
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
from .Cell import Cell
|
||||||
|
from .Maze import Maze
|
||||||
|
from .MazeGenerator import MazeGenerator, Kruskal
|
||||||
|
from .MazeSolver import MazeSolver, AStar
|
||||||
|
|
||||||
|
__version__ = "1.0.0"
|
||||||
|
__author__ = "us"
|
||||||
|
__all__ = ["Cell", "Maze", "MazeGenerator", "MazeSolver", "AStar", "Kruskal"]
|
||||||
+89
-10
@@ -1,18 +1,97 @@
|
|||||||
class DataMaze:
|
class DataMaze:
|
||||||
def __init__(self) -> None:
|
|
||||||
pass
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_data(name_file: str) -> str:
|
def get_file_data(name_file: str) -> str:
|
||||||
data = ""
|
with open(name_file, "r") as file:
|
||||||
|
data = file.read()
|
||||||
|
if data == "":
|
||||||
|
raise ValueError("The file is empty")
|
||||||
|
return data
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def transform_data(data: str) -> dict:
|
||||||
|
tmp = data.split("\n")
|
||||||
|
tmp2 = [
|
||||||
|
value.split("=", 1) for value in tmp
|
||||||
|
]
|
||||||
|
data_t = {
|
||||||
|
value[0]: value[1] for value in tmp2
|
||||||
|
}
|
||||||
|
return data_t
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def verif_key_data(data: dict) -> None:
|
||||||
|
key_test = {
|
||||||
|
"WIDTH", "HEIGHT", "ENTRY", "EXIT", "OUTPUT_FILE", "PERFECT"
|
||||||
|
}
|
||||||
|
set_key = {
|
||||||
|
key for key in data.keys()
|
||||||
|
}
|
||||||
|
if len(set_key) != len(key_test):
|
||||||
|
raise KeyError("Missing some data the len do not correspond")
|
||||||
|
res_key = {key for key in set_key if key not in key_test}
|
||||||
|
if len(res_key) != 0:
|
||||||
|
raise KeyError("Some Key "
|
||||||
|
f"do not correspond the keys: {res_key}")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def convert_values(data: dict):
|
||||||
|
key_int = {"WIDTH", "HEIGHT"}
|
||||||
|
key_tuple = {"ENTRY", "EXIT"}
|
||||||
|
key_bool = {"PERFECT"}
|
||||||
|
res: dict = {}
|
||||||
|
for key in key_int:
|
||||||
|
res.update({key: int(data[key])})
|
||||||
|
for key in key_tuple:
|
||||||
|
res.update({key: DataMaze.convert_tuple(data[key])})
|
||||||
|
for key in key_bool:
|
||||||
|
res.update({key: DataMaze.convert_bool(data[key])})
|
||||||
|
res.update({"OUTPUT_FILE": data["OUTPUT_FILE"]})
|
||||||
|
return res
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_data_maze(name_file: str) -> dict:
|
||||||
try:
|
try:
|
||||||
with open(name_file, "r") as file:
|
data_str = DataMaze.get_file_data(name_file)
|
||||||
data = file.read()
|
data_dict = DataMaze.transform_data(data_str)
|
||||||
|
DataMaze.verif_key_data(data_dict)
|
||||||
|
data_maze = DataMaze.convert_values(data_dict)
|
||||||
|
return data_maze
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
print("The file do not exist")
|
print("The file do not exist")
|
||||||
finally:
|
exit()
|
||||||
return data
|
except PermissionError:
|
||||||
|
print("We dont have the Permission")
|
||||||
|
exit()
|
||||||
|
except ValueError as e:
|
||||||
|
print(f"Error during the convert or the file is empty: {e}")
|
||||||
|
exit()
|
||||||
|
except KeyError as e:
|
||||||
|
print(f"Error on the key in the file: {e}")
|
||||||
|
exit()
|
||||||
|
except IndexError as e:
|
||||||
|
print("In the function transform Data some data cannot "
|
||||||
|
f"be splited by '=' because '=' was not present: {e}")
|
||||||
|
exit()
|
||||||
|
except AttributeError as e:
|
||||||
|
print("Error on the "
|
||||||
|
f"funciton get_data_maze : {e}")
|
||||||
|
exit()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def transform_data(data: str):
|
def convert_tuple(data: str) -> tuple:
|
||||||
pass
|
data_t = data.split(",")
|
||||||
|
if len(data_t) != 2:
|
||||||
|
raise ValueError("There is too much "
|
||||||
|
"argument in the coordinate given")
|
||||||
|
x, y = data_t
|
||||||
|
tup = (int(x), int(y))
|
||||||
|
return tup
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def convert_bool(data: str) -> bool:
|
||||||
|
if data != "True" and data != "False":
|
||||||
|
raise ValueError("This is not True or False")
|
||||||
|
if data == "True":
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
7D53BFD3D57951517D1D
|
||||||
|
3D12C3903BD03AD4178D
|
||||||
|
2BAEBEEEAA92EED547C9
|
||||||
|
2287ED17AAAC5393FFF0
|
||||||
|
6C6951292A87D2AEBD30
|
||||||
|
37D43E8686E93AABAB8C
|
||||||
|
21516D2D47FEE8284049
|
||||||
|
6C7857C3FB9116C696D8
|
||||||
|
751453D6D2AAC57BE970
|
||||||
|
3BA952D17EA83BD05470
|
||||||
|
22AAD2907BAE86967B74
|
||||||
|
2AA83C2EFC69696FBC35
|
||||||
|
686EE96FD7D4783FAD21
|
||||||
|
7ED17ED3D57D3EC52FA0
|
||||||
|
7B943D16FB7BABD3AFC8
|
||||||
|
7407C5297EB82EB84174
|
||||||
|
392D53C6912EE9447E9D
|
||||||
|
62A952BBAAC13EFD7B89
|
||||||
|
3AAC3EC6EABAAD557824
|
||||||
|
66C7C7D7D6C6C7D556CD
|
||||||
|
|
||||||
|
1,1
|
||||||
|
16,15
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
from parsing.Parsing import DataMaze
|
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
|
||||||
print("Unit Testing for parsing:")
|
|
||||||
DataMaze.get_data("config.txt")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
import pytest
|
||||||
|
from amaz_lib.Cell import Cell
|
||||||
|
|
||||||
|
|
||||||
|
def test_cell_setter_getter() -> None:
|
||||||
|
cell = Cell(value=0)
|
||||||
|
|
||||||
|
cell.set_north(True)
|
||||||
|
assert cell.get_north() is True
|
||||||
|
cell.set_north(False)
|
||||||
|
assert cell.get_north() is False
|
||||||
|
|
||||||
|
cell.set_est(True)
|
||||||
|
assert cell.get_est() is True
|
||||||
|
cell.set_est(False)
|
||||||
|
assert cell.get_est() is False
|
||||||
|
|
||||||
|
cell.set_south(True)
|
||||||
|
assert cell.get_south() is True
|
||||||
|
cell.set_south(False)
|
||||||
|
assert cell.get_south() is False
|
||||||
|
|
||||||
|
cell.set_west(True)
|
||||||
|
assert cell.get_west() is True
|
||||||
|
cell.set_west(False)
|
||||||
|
assert cell.get_west() is False
|
||||||
|
|
||||||
|
cell.set_value(8)
|
||||||
|
assert cell.get_value() == 8
|
||||||
|
cell.set_value(0)
|
||||||
|
assert cell.get_value() == 0
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
import numpy
|
||||||
|
from amaz_lib.Cell import Cell
|
||||||
|
from amaz_lib.Maze import Maze
|
||||||
|
|
||||||
|
|
||||||
|
def test_maze_setter_getter() -> None:
|
||||||
|
maze = Maze(numpy.array([]))
|
||||||
|
|
||||||
|
test = numpy.array(
|
||||||
|
[
|
||||||
|
[Cell(value=6), Cell(value=8), Cell(value=11)],
|
||||||
|
[Cell(value=6), Cell(value=8), Cell(value=11)],
|
||||||
|
[Cell(value=6), Cell(value=8), Cell(value=11)],
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
maze.set_maze(test)
|
||||||
|
assert numpy.array_equal(maze.get_maze(), test) == True
|
||||||
|
|
||||||
|
|
||||||
|
def test_maze_str() -> None:
|
||||||
|
test = numpy.array(
|
||||||
|
[
|
||||||
|
[Cell(value=6), Cell(value=8), Cell(value=11)],
|
||||||
|
[Cell(value=6), Cell(value=8), Cell(value=11)],
|
||||||
|
[Cell(value=6), Cell(value=8), Cell(value=11)],
|
||||||
|
]
|
||||||
|
)
|
||||||
|
maze = Maze(test)
|
||||||
|
|
||||||
|
assert maze.__str__() == "68B\n68B\n68B\n"
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
import numpy
|
||||||
|
from amaz_lib.MazeGenerator import Kruskal
|
||||||
|
|
||||||
|
|
||||||
|
def test_kruskal_output_shape() -> None:
|
||||||
|
generator = Kruskal()
|
||||||
|
maze = numpy.array([])
|
||||||
|
for output in generator.generator(10, 10):
|
||||||
|
maze = output
|
||||||
|
|
||||||
|
assert maze.shape == (10, 10)
|
||||||
@@ -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"
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
from parsing.Parsing import DataMaze
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
class TestParsing:
|
||||||
|
|
||||||
|
def test_get_data_valid(self):
|
||||||
|
data = DataMaze.get_file_data("tests/test_txt/config_1.txt")
|
||||||
|
assert isinstance(data, str) is True
|
||||||
|
|
||||||
|
def test_file_error(self):
|
||||||
|
with pytest.raises(FileNotFoundError):
|
||||||
|
DataMaze.get_file_data("tete")
|
||||||
|
|
||||||
|
# def test_permission_error(self):
|
||||||
|
# with pytest.raises(PermissionError):
|
||||||
|
# DataMaze.get_file_data("tests/test_txt/error_1.txt")
|
||||||
|
|
||||||
|
def test_empty_file_error(self):
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
DataMaze.get_file_data("tests/test_txt/error_6.txt")
|
||||||
|
|
||||||
|
def test_transform_data_valid(self):
|
||||||
|
data = DataMaze.get_file_data("tests/test_txt/config_1.txt")
|
||||||
|
data_2 = DataMaze.transform_data(data)
|
||||||
|
assert isinstance(data_2, dict)
|
||||||
|
|
||||||
|
def test_transform__index_error(self):
|
||||||
|
with pytest.raises(IndexError):
|
||||||
|
DataMaze.transform_data("asdasdasdasdasdasda\nasdasdas=asdasd")
|
||||||
|
|
||||||
|
def test_key_data_error(self):
|
||||||
|
with pytest.raises(KeyError):
|
||||||
|
data = DataMaze.get_file_data("tests/test_txt/error_8.txt")
|
||||||
|
data2 = DataMaze.transform_data(data)
|
||||||
|
DataMaze.verif_key_data(data2)
|
||||||
|
|
||||||
|
def test_key_data_error_2(self):
|
||||||
|
with pytest.raises(KeyError):
|
||||||
|
data = DataMaze.get_file_data("tests/test_txt/error_9.txt")
|
||||||
|
data2 = DataMaze.transform_data(data)
|
||||||
|
DataMaze.verif_key_data(data2)
|
||||||
|
|
||||||
|
def test_convert_int(self):
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
data = DataMaze.get_file_data("tests/test_txt/error_2.txt")
|
||||||
|
data2 = DataMaze.transform_data(data)
|
||||||
|
DataMaze.convert_values(data2)
|
||||||
|
|
||||||
|
def test_tuple_error(self):
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
DataMaze.convert_tuple("0,3,5,5")
|
||||||
|
|
||||||
|
def test_tuple_error1(self):
|
||||||
|
with pytest.raises(AttributeError):
|
||||||
|
DataMaze.convert_tuple(None)
|
||||||
|
|
||||||
|
def test_bool_error(self):
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
DataMaze.convert_bool("Trueeee")
|
||||||
|
|
||||||
|
def test_valid_tuple(self):
|
||||||
|
assert DataMaze.convert_tuple("7534564654, 78") == (7534564654, 78)
|
||||||
|
|
||||||
|
def test_valid_bool(self):
|
||||||
|
assert DataMaze.convert_bool("False") is False
|
||||||
|
|
||||||
|
def test_valid_bool1(self):
|
||||||
|
assert DataMaze.convert_bool("True") is True
|
||||||
|
|
||||||
|
def test_data_maze(self):
|
||||||
|
data = DataMaze.get_data_maze("tests/test_txt/config_1.txt")
|
||||||
|
assert data["WIDTH"] == 200
|
||||||
|
assert data["HEIGHT"] == 100
|
||||||
|
assert data["ENTRY"] == (0, 0)
|
||||||
|
assert data["EXIT"] == (19, 14)
|
||||||
|
assert data["OUTPUT_FILE"] == "maze.txt"
|
||||||
|
assert data["PERFECT"] is True
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
WIDTH=200
|
||||||
|
HEIGHT=100
|
||||||
|
ENTRY=0,0
|
||||||
|
EXIT=19,14
|
||||||
|
OUTPUT_FILE=maze.txt
|
||||||
|
PERFECT=True
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
WIDTH=200
|
||||||
|
HEIGHT=100
|
||||||
|
ENTRY=0,0
|
||||||
|
EXIT=19,14
|
||||||
|
OUTPUT_FILE=maze.txt
|
||||||
|
PERFECT=True
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
WIDTH=caca
|
||||||
|
HEIGHT=100
|
||||||
|
ENTRY=0,0
|
||||||
|
EXIT=19,14
|
||||||
|
OUTPUT_FILE=maze.txt
|
||||||
|
PERFECT=True
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
WIDTH=200
|
||||||
|
HEIGHT=100
|
||||||
|
ENTRY=0,0
|
||||||
|
EXIT=19,14
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
WIDTH=200
|
||||||
|
HEIGHT=100
|
||||||
|
ENTRY=0,0
|
||||||
|
EXIT=19,14
|
||||||
|
OUTPUT_FILE=maze.txt
|
||||||
|
PERFECT=True
|
||||||
|
PIPI=tut
|
||||||
@@ -14,26 +14,26 @@ dependencies = [
|
|||||||
{ name = "numpy", version = "2.2.6", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.11'" },
|
{ name = "numpy", version = "2.2.6", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.11'" },
|
||||||
{ name = "numpy", version = "2.4.3", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.11'" },
|
{ name = "numpy", version = "2.4.3", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.11'" },
|
||||||
{ name = "pydantic" },
|
{ name = "pydantic" },
|
||||||
{ name = "pytest" },
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[package.dev-dependencies]
|
[package.dev-dependencies]
|
||||||
dev = [
|
dev = [
|
||||||
{ name = "flake8" },
|
{ name = "flake8" },
|
||||||
{ name = "mypy" },
|
{ name = "mypy" },
|
||||||
|
{ name = "pytest" },
|
||||||
]
|
]
|
||||||
|
|
||||||
[package.metadata]
|
[package.metadata]
|
||||||
requires-dist = [
|
requires-dist = [
|
||||||
{ name = "numpy", specifier = ">=2.2.6" },
|
{ name = "numpy", specifier = ">=2.2.6" },
|
||||||
{ name = "pydantic", specifier = ">=2.12.5" },
|
{ name = "pydantic", specifier = ">=2.12.5" },
|
||||||
{ name = "pytest", specifier = ">=9.0.2" },
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[package.metadata.requires-dev]
|
[package.metadata.requires-dev]
|
||||||
dev = [
|
dev = [
|
||||||
{ name = "flake8", specifier = ">=7.3.0" },
|
{ name = "flake8", specifier = ">=7.3.0" },
|
||||||
{ name = "mypy", specifier = ">=1.19.1" },
|
{ name = "mypy", specifier = ">=1.19.1" },
|
||||||
|
{ name = "pytest", specifier = ">=9.0.2" },
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
Reference in New Issue
Block a user