mirror of
https://github.com/maoakeEnterprise/amazing.git
synced 2026-04-28 16:04:35 +02:00
Compare commits
4 Commits
21e9aba95f
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 0bd43d21a5 | |||
| f5131f0e01 | |||
| 4d5662c248 | |||
| e137389216 |
+3
-1
@@ -1,3 +1,4 @@
|
||||
import os
|
||||
from typing import Any
|
||||
from numpy.typing import NDArray
|
||||
from AMazeIng import AMazeIng
|
||||
@@ -482,9 +483,10 @@ def main() -> None:
|
||||
"""Run the maze application."""
|
||||
mlx = None
|
||||
try:
|
||||
mlx = MazeMLX(1600, 2000)
|
||||
os.system("cls" if os.name == "nt" else "clear")
|
||||
config = Parsing.get_data_maze("config.txt")
|
||||
amazing = AMazeIng(**config)
|
||||
mlx = MazeMLX(1600, 2000)
|
||||
mlx.start(amazing)
|
||||
amazing.export_maze()
|
||||
except Exception as err:
|
||||
|
||||
+6
-6
@@ -1,8 +1,8 @@
|
||||
WIDTH=4
|
||||
HEIGHT=4
|
||||
ENTRY=1,1
|
||||
EXIT=1,2
|
||||
WIDTH=15
|
||||
HEIGHT=15
|
||||
ENTRY=2,1
|
||||
EXIT=3,5
|
||||
OUTPUT_FILE=con.txt
|
||||
PERFECT=False
|
||||
GENERATOR=Kruskal
|
||||
SOLVER=AStar
|
||||
GENERATOR=DFS
|
||||
Solver=AStar
|
||||
|
||||
+7
-3
@@ -12,8 +12,8 @@ class AMazeIng(BaseModel):
|
||||
|
||||
model_config = ConfigDict(arbitrary_types_allowed=True)
|
||||
|
||||
width: int = Field(ge=4, le=100)
|
||||
height: int = Field(ge=4, le=100)
|
||||
width: int = Field(ge=5, le=100)
|
||||
height: int = Field(ge=5, le=100)
|
||||
entry: tuple[int, int]
|
||||
exit: tuple[int, int]
|
||||
output_file: str = Field(min_length=3)
|
||||
@@ -39,7 +39,11 @@ class AMazeIng(BaseModel):
|
||||
raise ValueError("Exit coordinates exceed the maze size")
|
||||
if self.entry == self.exit:
|
||||
raise ValueError("Entry and Exit coordinates cant be the same")
|
||||
if self.width <= 10 or self.height <= 10:
|
||||
if self.exit[0] < 1 or self.exit[1] < 1:
|
||||
raise ValueError("Exit coordinates to low")
|
||||
if self.entry[0] < 1 or self.entry[1] < 1:
|
||||
raise ValueError("Entry coordinates to low")
|
||||
if self.width < 9 or self.height < 7:
|
||||
print("Height or width to low for disply forty two logo")
|
||||
return self
|
||||
|
||||
|
||||
@@ -105,10 +105,24 @@ class MazeGenerator(ABC):
|
||||
The modified maze.
|
||||
"""
|
||||
|
||||
def enough_wall(cell: Cell) -> bool:
|
||||
nb_wall = 0
|
||||
if cell.get_est():
|
||||
nb_wall += 1
|
||||
if cell.get_north():
|
||||
nb_wall += 1
|
||||
if cell.get_west():
|
||||
nb_wall += 1
|
||||
if cell.get_south():
|
||||
nb_wall += 1
|
||||
if nb_wall == 3:
|
||||
return True
|
||||
return False
|
||||
|
||||
directions = {"N": (0, -1), "S": (0, 1), "W": (-1, 0), "E": (1, 0)}
|
||||
|
||||
reverse = {"N": "S", "S": "N", "W": "E", "E": "W"}
|
||||
min_break = 2
|
||||
min_break = 1
|
||||
while True:
|
||||
count = 0
|
||||
for y in range(height):
|
||||
@@ -125,7 +139,9 @@ class MazeGenerator(ABC):
|
||||
continue
|
||||
if direc in ["S", "E"]:
|
||||
continue
|
||||
if np.random.random() < prob:
|
||||
if not enough_wall(maze[y][x]):
|
||||
continue
|
||||
else:
|
||||
count += 1
|
||||
cell = maze[y][x]
|
||||
cell_n = maze[ny][nx]
|
||||
@@ -137,7 +153,7 @@ class MazeGenerator(ABC):
|
||||
maze[y][x] = cell
|
||||
maze[ny][nx] = cell_n
|
||||
yield maze
|
||||
if count > min_break:
|
||||
if count >= min_break:
|
||||
break
|
||||
return maze
|
||||
|
||||
@@ -292,7 +308,7 @@ class Kruskal(MazeGenerator):
|
||||
The final generated maze.
|
||||
"""
|
||||
cells_ft = None
|
||||
if height > 10 and width > 10:
|
||||
if height >= 7 and width >= 9:
|
||||
cells_ft = self.get_cell_ft(width, height)
|
||||
if cells_ft and (self.start in cells_ft or self.end in cells_ft):
|
||||
print(
|
||||
@@ -374,7 +390,7 @@ class DepthFirstSearch(MazeGenerator):
|
||||
if seed is not None:
|
||||
random.seed(seed)
|
||||
maze = self.init_maze(width, height)
|
||||
if width > 10 and height > 10:
|
||||
if width >= 9 and height >= 7:
|
||||
self.forty_two = self.get_cell_ft(width, height)
|
||||
visited: NDArray[np.object_] = np.zeros((height, width), dtype=bool)
|
||||
if (
|
||||
|
||||
+26
-2
@@ -6,6 +6,22 @@ from typing import Any
|
||||
class DataMaze:
|
||||
"""Provide helper methods to load and validate maze configuration data."""
|
||||
|
||||
@staticmethod
|
||||
def test_output_file(name_file: str) -> None:
|
||||
try:
|
||||
with open(name_file, "r"):
|
||||
while True:
|
||||
res = input(
|
||||
f"{name_file} already exist. Data will"
|
||||
" be erased. Continue ? (y/n)"
|
||||
)
|
||||
if res == "y":
|
||||
break
|
||||
elif res == "n":
|
||||
raise Exception("")
|
||||
except FileNotFoundError:
|
||||
return
|
||||
|
||||
@staticmethod
|
||||
def get_file_data(name_file: str) -> str:
|
||||
"""Read and return the contents of a configuration file.
|
||||
@@ -38,8 +54,12 @@ class DataMaze:
|
||||
A dictionary mapping configuration keys to their string values.
|
||||
"""
|
||||
tmp = data.split("\n")
|
||||
tmp2 = [value.split("=", 1) for value in tmp if "=" in value]
|
||||
data_t = {value[0]: value[1] for value in tmp2}
|
||||
tmp2 = [
|
||||
value.split("=", 1)
|
||||
for value in tmp
|
||||
if not value.startswith("#") and "=" in value
|
||||
]
|
||||
data_t = {value[0].upper(): value[1] for value in tmp2}
|
||||
return data_t
|
||||
|
||||
@staticmethod
|
||||
@@ -64,6 +84,8 @@ class DataMaze:
|
||||
}
|
||||
i = 0
|
||||
for key in data:
|
||||
if key.upper() == "OUTPUT_FILE":
|
||||
DataMaze.test_output_file(data[key])
|
||||
if key in key_test:
|
||||
i += 1
|
||||
if len(key_test) != i:
|
||||
@@ -187,6 +209,8 @@ class DataMaze:
|
||||
def test_file_format(file: str) -> None:
|
||||
with open(file) as data_str:
|
||||
for line in data_str:
|
||||
if line.startswith("#"):
|
||||
continue
|
||||
if len(line.split("=", 1)) != 2:
|
||||
raise Exception(
|
||||
"config file format not respected. excpected format : "
|
||||
|
||||
Reference in New Issue
Block a user