mirror of
https://github.com/maoakeEnterprise/amazing.git
synced 2026-04-28 16:04:35 +02:00
Merge branch 'main' into parsing
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Generator
|
||||
import numpy as np
|
||||
from .classes.Cell import Cell
|
||||
import math
|
||||
|
||||
|
||||
class MazeGenerator(ABC):
|
||||
@abstractmethod
|
||||
@classmethod
|
||||
def generator(
|
||||
cls, height: int, width: int
|
||||
) -> Generator[np.ndarray, None, np.ndarray]: ...
|
||||
|
||||
|
||||
class Kruskal(MazeGenerator):
|
||||
@staticmethod
|
||||
def walls_to_maze(
|
||||
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)]
|
||||
)
|
||||
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_est(True)
|
||||
if y == width - 1:
|
||||
maze[x][y].set_west(True)
|
||||
return maze
|
||||
|
||||
@staticmethod
|
||||
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 and b in set:
|
||||
return True
|
||||
if a in set or b in set:
|
||||
return False
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def merge_sets(sets: list[list[int]], 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)
|
||||
|
||||
@classmethod
|
||||
def generator(
|
||||
cls, height: int, width: int
|
||||
) -> Generator[np.ndarray, None, np.ndarray]:
|
||||
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 w in range(width):
|
||||
for h in range(height - 1):
|
||||
walls += [(w + (width * h), w + (width * h) + width)]
|
||||
np.random.shuffle(walls)
|
||||
|
||||
yield cls.walls_to_maze(walls, height, width)
|
||||
for wall in walls:
|
||||
if not cls.is_in_same_set(sets, wall):
|
||||
cls.merge_sets(sets, wall)
|
||||
walls.remove(wall)
|
||||
yield cls.walls_to_maze(walls, height, width)
|
||||
return cls.walls_to_maze(walls, height, width)
|
||||
|
||||
|
||||
def main():
|
||||
try:
|
||||
for alg in MazeGenerator.Kruskal.kruskal(10, 10):
|
||||
maze = alg
|
||||
# print(maze)
|
||||
# print()
|
||||
print(maze)
|
||||
|
||||
except GeneratorExit as maze:
|
||||
print(maze)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,7 @@
|
||||
from .classes.Cell import Cell
|
||||
from .classes.Maze import Maze
|
||||
from .MazeGenerator import MazeGenerator
|
||||
|
||||
__version__ = "1.0.0"
|
||||
__author__ = "us"
|
||||
__all__ = ["Cell", "Maze", "MazeGenerator"]
|
||||
@@ -5,11 +5,17 @@ class Cell(BaseModel):
|
||||
value: int = Field(ge=0, le=15)
|
||||
|
||||
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:
|
||||
if (is_wall and self.value | 14 == 15) or (
|
||||
not is_wall and self.value | 14 != 15
|
||||
if (not is_wall and self.value | 14 == 15) or (
|
||||
is_wall and self.value | 14 != 15
|
||||
):
|
||||
self.value = self.value ^ (1)
|
||||
|
||||
@@ -17,8 +23,8 @@ class Cell(BaseModel):
|
||||
return self.value & 1 == 1
|
||||
|
||||
def set_est(self, is_wall: bool) -> None:
|
||||
if (is_wall and self.value | 13 == 15) or (
|
||||
not is_wall and self.value | 13 != 15
|
||||
if (not is_wall and self.value | 13 == 15) or (
|
||||
is_wall and self.value | 13 != 15
|
||||
):
|
||||
self.value = self.value ^ (2)
|
||||
|
||||
@@ -26,8 +32,8 @@ class Cell(BaseModel):
|
||||
return self.value & 2 == 2
|
||||
|
||||
def set_south(self, is_wall: bool) -> None:
|
||||
if (is_wall and self.value | 11 == 15) or (
|
||||
not is_wall and self.value | 11 != 15
|
||||
if (not is_wall and self.value | 11 == 15) or (
|
||||
is_wall and self.value | 11 != 15
|
||||
):
|
||||
self.value = self.value ^ (4)
|
||||
|
||||
@@ -35,8 +41,8 @@ class Cell(BaseModel):
|
||||
return self.value & 4 == 4
|
||||
|
||||
def set_west(self, is_wall: bool) -> None:
|
||||
if (is_wall and self.value | 8 == 15) or (
|
||||
not is_wall and self.value | 8 != 15
|
||||
if (not is_wall and self.value | 8 == 15) or (
|
||||
is_wall and self.value | 8 != 15
|
||||
):
|
||||
self.value = self.value ^ (8)
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
from dataclasses import dataclass
|
||||
|
||||
import numpy
|
||||
from .Cell import Cell
|
||||
from ..MazeGenerator import MazeGenerator
|
||||
|
||||
|
||||
@dataclass
|
||||
class Maze:
|
||||
maze: numpy.ndarray
|
||||
start: tuple[int, int]
|
||||
end: tuple[int, int]
|
||||
|
||||
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"
|
||||
res += "\n"
|
||||
res += f"{self.start[0]},{self.start[1]}\n"
|
||||
res += f"{self.end[0]},{self.end[1]}\n"
|
||||
return res
|
||||
|
||||
def export_maze(self, file_name: str) -> None:
|
||||
with open(file_name, "w") as file:
|
||||
file.write(self.__str__())
|
||||
|
||||
def solver(self) -> str:
|
||||
pass
|
||||
|
||||
def ascii_print(self) -> None:
|
||||
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="")
|
||||
if cell.get_south() is True:
|
||||
print("__", end="")
|
||||
else:
|
||||
print(" ", end="")
|
||||
if cell.get_est() is True:
|
||||
print("|", end="")
|
||||
else:
|
||||
print("_", end="")
|
||||
print()
|
||||
Reference in New Issue
Block a user