mirror of
https://github.com/maoakeEnterprise/amazing.git
synced 2026-11-01 07:42:03 +01:00
Compare commits
8
Commits
b2aa93e04d
...
40e25757c7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
40e25757c7 | ||
|
|
b1eda06fa5 | ||
|
|
769198c06b | ||
|
|
2c7b565137 | ||
|
|
d23959ce74 | ||
|
|
4cb678b5be | ||
|
|
b520210d58 | ||
|
|
bdb1056d69 |
No files matched your search
+66
-63
@@ -3,7 +3,6 @@ from src.AMazeIng import AMazeIng
|
|||||||
from src.parsing import Parsing
|
from src.parsing import Parsing
|
||||||
from mlx import Mlx
|
from mlx import Mlx
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import math
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
|
||||||
@@ -96,8 +95,9 @@ class MazeMLX:
|
|||||||
def random_color_ft() -> Any:
|
def random_color_ft() -> Any:
|
||||||
colors = [
|
colors = [
|
||||||
[0xFF, 0xBF, 0x00, 0xFF], # blue
|
[0xFF, 0xBF, 0x00, 0xFF], # blue
|
||||||
[0xFF, 0x00, 0x80, 0xFF], # purple
|
[0x00, 0xFF, 0x40, 0xFF], # green
|
||||||
[0xFF, 0x00, 0xFF, 0xFF], # rose
|
[0xFF, 0x00, 0xFF, 0xFF], # pink
|
||||||
|
[0x00, 0xFF, 0xFF, 0xFF], # yellow
|
||||||
]
|
]
|
||||||
while True:
|
while True:
|
||||||
for color in colors:
|
for color in colors:
|
||||||
@@ -106,30 +106,35 @@ class MazeMLX:
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def random_color() -> Any:
|
def random_color() -> Any:
|
||||||
colors = [
|
colors = [
|
||||||
[0x00, 0x00, 0xFF, 0xFF], # red
|
[0xFF, 0x00, 0xFF, 0xFF], # pink
|
||||||
[0x00, 0xFF, 0xFF, 0xFF], # yellow
|
[0x00, 0xFF, 0xFF, 0xFF], # yellow
|
||||||
[0x00, 0xFF, 0x40, 0xFF], # green
|
[0x00, 0xFF, 0x40, 0xFF], # green
|
||||||
[0xFF, 0xBF, 0x00, 0xFF], # blue
|
[0xFF, 0xBF, 0x00, 0xFF], # blue
|
||||||
[0xFF, 0x00, 0x80, 0xFF], # purple
|
[0xFF, 0x00, 0x80, 0xFF], # purple
|
||||||
[0xFF, 0x00, 0xFF, 0xFF], # pink
|
[0x00, 0x00, 0xFF, 0xFF], # red
|
||||||
]
|
]
|
||||||
while True:
|
while True:
|
||||||
for color in colors:
|
for color in colors:
|
||||||
yield color
|
yield color
|
||||||
|
|
||||||
def update_maze(self, maze: np.ndarray) -> None:
|
def get_margin_line_len(self, maze: np.ndarray) -> tuple[int, int, int]:
|
||||||
self.clear_image()
|
|
||||||
|
|
||||||
rows = len(maze)
|
rows = len(maze)
|
||||||
cols = len(maze[0])
|
cols = len(maze[0])
|
||||||
|
|
||||||
line_len = min(self.width // cols, self.height // rows)
|
line_len = min(self.width // cols, self.height // rows) - 1
|
||||||
|
|
||||||
maze_width = cols * line_len
|
maze_width = cols * line_len
|
||||||
maze_height = rows * line_len
|
maze_height = rows * line_len
|
||||||
|
|
||||||
margin_x = (self.width - maze_width) // 2
|
margin_x = ((self.width - maze_width) // 2) + 1
|
||||||
margin_y = (self.height - maze_height) // 2
|
margin_y = ((self.height - maze_height) // 2) + 1
|
||||||
|
|
||||||
|
return (line_len, margin_x, margin_y)
|
||||||
|
|
||||||
|
def update_maze(self, maze: np.ndarray) -> None:
|
||||||
|
self.clear_image()
|
||||||
|
|
||||||
|
line_len, margin_x, margin_y = self.get_margin_line_len(maze)
|
||||||
for y in range(len(maze)):
|
for y in range(len(maze)):
|
||||||
for x in range(len(maze[0])):
|
for x in range(len(maze[0])):
|
||||||
x0 = x * line_len + margin_x
|
x0 = x * line_len + margin_x
|
||||||
@@ -155,16 +160,7 @@ class MazeMLX:
|
|||||||
if maze is None:
|
if maze is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
rows = len(maze)
|
line_len, margin_x, margin_y = self.get_margin_line_len(maze)
|
||||||
cols = len(maze[0])
|
|
||||||
|
|
||||||
line_len = min(self.width // cols, self.height // rows)
|
|
||||||
|
|
||||||
maze_width = cols * line_len
|
|
||||||
maze_height = rows * line_len
|
|
||||||
|
|
||||||
margin_x = (self.width - maze_width) // 2
|
|
||||||
margin_y = (self.height - maze_height) // 2
|
|
||||||
|
|
||||||
for i in range(len(path)):
|
for i in range(len(path)):
|
||||||
ul = (
|
ul = (
|
||||||
@@ -212,16 +208,7 @@ class MazeMLX:
|
|||||||
if maze is None:
|
if maze is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
rows = len(maze)
|
line_len, margin_x, margin_y = self.get_margin_line_len(maze)
|
||||||
cols = len(maze[0])
|
|
||||||
|
|
||||||
line_len = min(self.width // cols, self.height // rows)
|
|
||||||
|
|
||||||
maze_width = cols * line_len
|
|
||||||
maze_height = rows * line_len
|
|
||||||
|
|
||||||
margin_x = (self.width - maze_width) // 2
|
|
||||||
margin_y = (self.height - maze_height) // 2
|
|
||||||
|
|
||||||
ul = (
|
ul = (
|
||||||
(entry[0] - 1) * line_len + margin_x + 3,
|
(entry[0] - 1) * line_len + margin_x + 3,
|
||||||
@@ -231,49 +218,42 @@ class MazeMLX:
|
|||||||
(entry[0] - 1) * line_len + line_len + margin_x - 3,
|
(entry[0] - 1) * line_len + line_len + margin_x - 3,
|
||||||
(entry[1] - 1) * line_len + line_len - 3 + margin_y,
|
(entry[1] - 1) * line_len + line_len - 3 + margin_y,
|
||||||
)
|
)
|
||||||
print(f"ul: {ul}; dr: {dr}")
|
self.put_block(ul, dr, [0xFF, 0xBF, 0x00, 0x9F])
|
||||||
self.put_block(ul, dr)
|
|
||||||
self.redraw_image()
|
ul = (
|
||||||
|
(exit[0] - 1) * line_len + margin_x + 3,
|
||||||
|
(exit[1] - 1) * line_len + 3 + margin_y,
|
||||||
|
)
|
||||||
|
dr = (
|
||||||
|
(exit[0] - 1) * line_len + line_len + margin_x - 3,
|
||||||
|
(exit[1] - 1) * line_len + line_len - 3 + margin_y,
|
||||||
|
)
|
||||||
|
self.put_block(ul, dr, [0x00, 0xFF, 0x40, 0x9F])
|
||||||
|
|
||||||
def draw_ft(self, maze: np.ndarray, color: list | None = None):
|
def draw_ft(self, maze: np.ndarray, color: list | None = None):
|
||||||
self.clear_image()
|
line_len, margin_x, margin_y = self.get_margin_line_len(maze)
|
||||||
margin = math.trunc(
|
|
||||||
math.sqrt(self.width if self.width > self.height else self.height)
|
|
||||||
// 2
|
|
||||||
)
|
|
||||||
line_len = math.trunc(
|
|
||||||
(
|
|
||||||
(self.height - margin) // len(maze)
|
|
||||||
if self.height > self.width
|
|
||||||
else (self.width - margin) // len(maze[0])
|
|
||||||
)
|
|
||||||
)
|
|
||||||
for y in range(len(maze)):
|
for y in range(len(maze)):
|
||||||
for x in range(len(maze[0])):
|
for x in range(len(maze[0])):
|
||||||
x0 = x * line_len + margin
|
|
||||||
y0 = y * line_len + margin
|
|
||||||
x1 = x * line_len + line_len + margin
|
|
||||||
y1 = y * line_len + line_len + margin
|
|
||||||
|
|
||||||
if maze[y][x].get_north():
|
|
||||||
self.put_line((x0, y0), (x1, y0))
|
|
||||||
if maze[y][x].get_est():
|
|
||||||
self.put_line((x1, y0), (x1, y1))
|
|
||||||
if maze[y][x].get_south():
|
|
||||||
self.put_line((x0, y1), (x1, y1))
|
|
||||||
if maze[y][x].get_west():
|
|
||||||
self.put_line((x0, y0), (x0, y1))
|
|
||||||
if maze[y][x].value == 15:
|
if maze[y][x].value == 15:
|
||||||
self.put_block((x0, y0), (x1, y1))
|
x0 = x * line_len + margin_x
|
||||||
|
y0 = y * line_len + margin_y
|
||||||
|
x1 = x * line_len + line_len + margin_x
|
||||||
|
y1 = y * line_len + line_len + margin_y
|
||||||
|
self.put_block((x0, y0), (x1, y1), color)
|
||||||
|
|
||||||
def draw_image(self, amazing: AMazeIng) -> None:
|
def draw_image(self, amazing: AMazeIng) -> None:
|
||||||
if self.render_maze(amazing):
|
if self.render_maze(amazing):
|
||||||
if self.path_printer and self.print_path:
|
if self.path_printer and self.print_path:
|
||||||
if self.render_path():
|
if self.render_path():
|
||||||
color = next(self.color_gen_ft)
|
color = next(self.color_gen_ft)
|
||||||
color
|
self.draw_ft(amazing.maze.get_maze(), color)
|
||||||
|
next(self.timer_gen)
|
||||||
else:
|
else:
|
||||||
|
self.time_gen()
|
||||||
|
self.update_maze(amazing.maze.get_maze())
|
||||||
self.draw_ft(amazing.maze.get_maze())
|
self.draw_ft(amazing.maze.get_maze())
|
||||||
|
self.put_start_end(amazing)
|
||||||
self.redraw_image()
|
self.redraw_image()
|
||||||
|
|
||||||
def shift_color(self):
|
def shift_color(self):
|
||||||
@@ -282,9 +262,18 @@ class MazeMLX:
|
|||||||
def shift_color_ft(self):
|
def shift_color_ft(self):
|
||||||
self.color_gen_ft = self.random_color_ft()
|
self.color_gen_ft = self.random_color_ft()
|
||||||
|
|
||||||
|
def time_gen(self):
|
||||||
|
self.timer_gen = self.time_generator()
|
||||||
|
|
||||||
def restart_maze(self, amazing: AMazeIng) -> None:
|
def restart_maze(self, amazing: AMazeIng) -> None:
|
||||||
self.generator = amazing.generate()
|
self.generator = amazing.generate()
|
||||||
|
|
||||||
|
def time_generator(self) -> Any:
|
||||||
|
yield
|
||||||
|
while True:
|
||||||
|
time.sleep(0.3)
|
||||||
|
yield
|
||||||
|
|
||||||
def restart_path(self, amazing: AMazeIng) -> None:
|
def restart_path(self, amazing: AMazeIng) -> None:
|
||||||
self.path_printer = self.put_path(amazing)
|
self.path_printer = self.put_path(amazing)
|
||||||
|
|
||||||
@@ -303,7 +292,6 @@ class MazeMLX:
|
|||||||
self.update_maze(amazing.maze.get_maze())
|
self.update_maze(amazing.maze.get_maze())
|
||||||
return False
|
return False
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
self.put_start_end(amazing)
|
|
||||||
pass
|
pass
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@@ -320,14 +308,29 @@ class MazeMLX:
|
|||||||
if keycode == 52:
|
if keycode == 52:
|
||||||
self.close_loop(None)
|
self.close_loop(None)
|
||||||
|
|
||||||
|
def handle_key_press_mteriier(self, keycode: int,
|
||||||
|
amazing: AMazeIng) -> None:
|
||||||
|
if keycode == 38:
|
||||||
|
self.restart_maze(amazing)
|
||||||
|
self.print_path = False
|
||||||
|
if keycode == 233:
|
||||||
|
self.restart_path(amazing)
|
||||||
|
self.print_path = True if self.print_path is False else False
|
||||||
|
if keycode == 34:
|
||||||
|
self.print_path = False
|
||||||
|
self.color = next(self.color_gen)
|
||||||
|
if keycode == 39:
|
||||||
|
self.close_loop(None)
|
||||||
|
|
||||||
def start(self, amazing: AMazeIng) -> None:
|
def start(self, amazing: AMazeIng) -> None:
|
||||||
self.restart_maze(amazing)
|
self.restart_maze(amazing)
|
||||||
self.shift_color()
|
self.shift_color()
|
||||||
self.shift_color_ft()
|
self.shift_color_ft()
|
||||||
|
self.time_gen()
|
||||||
self.mlx.mlx_loop_hook(self.mlx_ptr, self.draw_image, amazing)
|
self.mlx.mlx_loop_hook(self.mlx_ptr, self.draw_image, amazing)
|
||||||
self.mlx.mlx_hook(self.win_ptr, 33, 0, self.close_loop, None)
|
self.mlx.mlx_hook(self.win_ptr, 33, 0, self.close_loop, None)
|
||||||
self.mlx.mlx_hook(
|
self.mlx.mlx_hook(
|
||||||
self.win_ptr, 2, 1 << 0, self.handle_key_press, amazing
|
self.win_ptr, 2, 1 << 0, self.handle_key_press_mteriier, amazing
|
||||||
)
|
)
|
||||||
self.mlx.mlx_loop(self.mlx_ptr)
|
self.mlx.mlx_loop(self.mlx_ptr)
|
||||||
|
|
||||||
|
|||||||
+4
-4
@@ -1,8 +1,8 @@
|
|||||||
WIDTH=11
|
WIDTH=13
|
||||||
HEIGHT=11
|
HEIGHT=13
|
||||||
ENTRY=1,1
|
ENTRY=1,1
|
||||||
EXIT=11,11
|
EXIT=5,5
|
||||||
OUTPUT_FILE=maze.txt
|
OUTPUT_FILE=maze.txt
|
||||||
PERFECT=False
|
PERFECT=False
|
||||||
GENERATOR=DFS
|
GENERATOR=Kruskal
|
||||||
SOLVER=AStar
|
SOLVER=AStar
|
||||||
@@ -1,16 +1,16 @@
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
from numpy.typing import NDArray
|
||||||
import numpy
|
from typing import Optional, Any
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Maze:
|
class Maze:
|
||||||
maze: numpy.ndarray
|
maze: Optional[NDArray[Any]] = None
|
||||||
|
|
||||||
def get_maze(self) -> numpy.ndarray | None:
|
def get_maze(self) -> Optional[NDArray[Any]]:
|
||||||
return self.maze
|
return self.maze
|
||||||
|
|
||||||
def set_maze(self, new_maze: numpy.ndarray) -> None:
|
def set_maze(self, new_maze: NDArray[Any]) -> None:
|
||||||
self.maze = new_maze
|
self.maze = new_maze
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
@@ -24,6 +24,9 @@ class Maze:
|
|||||||
return res
|
return res
|
||||||
|
|
||||||
def ascii_print(self) -> None:
|
def ascii_print(self) -> None:
|
||||||
|
if self.maze is None:
|
||||||
|
print("None")
|
||||||
|
return
|
||||||
for cell in self.maze[0]:
|
for cell in self.maze[0]:
|
||||||
print("_", end="")
|
print("_", end="")
|
||||||
if cell.get_north():
|
if cell.get_north():
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import pytest
|
|
||||||
from amaz_lib.Cell import Cell
|
from amaz_lib.Cell import Cell
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
from amaz_lib.Cell import Cell
|
from amaz_lib.Cell import Cell
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from amaz_lib import AStar, Maze, MazeSolver
|
from amaz_lib import AStar, Maze
|
||||||
|
|
||||||
|
|
||||||
def test_solver() -> None:
|
def test_solver() -> None:
|
||||||
|
|||||||
Reference in new issue
Block a user