mirror of
https://github.com/maoakeEnterprise/amazing.git
synced 2026-04-28 16:04:35 +02:00
adding my maze need to be tested
This commit is contained in:
+106
-11
@@ -3,6 +3,7 @@ from typing import Generator
|
||||
import numpy as np
|
||||
from .Cell import Cell
|
||||
import math
|
||||
import random
|
||||
|
||||
|
||||
class MazeGenerator(ABC):
|
||||
@@ -84,17 +85,111 @@ class Kruskal(MazeGenerator):
|
||||
return self.walls_to_maze(walls, height, width)
|
||||
|
||||
|
||||
def main():
|
||||
try:
|
||||
for alg in MazeGenerator.Kruskal.kruskal(10, 10):
|
||||
maze = alg
|
||||
# print(maze)
|
||||
# print()
|
||||
print(maze)
|
||||
class DepthFirstSearch:
|
||||
|
||||
except GeneratorExit as maze:
|
||||
print(maze)
|
||||
@staticmethod
|
||||
def generator(width: int, height: int) -> np.ndarray:
|
||||
maze = DepthFirstSearch.init_maze(width, height)
|
||||
visited = []
|
||||
path = []
|
||||
w_h = (width, height)
|
||||
coord = (0, 0)
|
||||
while len(visited) < width * height:
|
||||
x, y = coord
|
||||
rand_steps = DepthFirstSearch.random_cells(visited, coord, w_h)
|
||||
if len(rand_steps) == 0:
|
||||
path = DepthFirstSearch.back_on_step(path, w_h)
|
||||
coord = DepthFirstSearch.last(path)
|
||||
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)
|
||||
coord = DepthFirstSearch.next_cell(x, y, wall)
|
||||
x, y = coord
|
||||
maze[y][x] = DepthFirstSearch.broken_wall(maze[y][x], wall_r)
|
||||
return maze
|
||||
|
||||
@staticmethod
|
||||
def init_maze(width: int, height: int) -> np.ndarray:
|
||||
maze = np.array([[Cell(value=15) for _ in range(width)]
|
||||
for _ in range(height)])
|
||||
return maze
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@staticmethod
|
||||
def add_cell_visited(coord: tuple, visited: list = []) -> list:
|
||||
visited.append(coord)
|
||||
return visited
|
||||
|
||||
@staticmethod
|
||||
def random_cells(visited: list, coord: tuple, w_h: tuple) -> list:
|
||||
rand_cell = []
|
||||
x, y = coord
|
||||
width, height = w_h
|
||||
# NORTH
|
||||
if y - 1 >= 0 and coord not in visited:
|
||||
rand_cell.append("N")
|
||||
|
||||
# SOUTH
|
||||
if y + 1 < height and coord not in visited:
|
||||
rand_cell.append("S")
|
||||
|
||||
# WEST
|
||||
if x - 1 >= 0 and coord not in visited:
|
||||
rand_cell.append("W")
|
||||
|
||||
# EAST
|
||||
if x + 1 < width and coord not in visited:
|
||||
rand_cell.append("E")
|
||||
return rand_cell
|
||||
|
||||
@staticmethod
|
||||
def next_step(rand_cell: list) -> str:
|
||||
return random.choice(rand_cell)
|
||||
|
||||
@staticmethod
|
||||
def broken_wall(cell: Cell, wall: str) -> Cell:
|
||||
if wall == "N":
|
||||
cell.set_north(False)
|
||||
elif wall == "S":
|
||||
cell.set_south(False)
|
||||
elif wall == "W":
|
||||
cell.set_west(False)
|
||||
elif wall == "E":
|
||||
cell.set_est(False)
|
||||
return cell
|
||||
|
||||
@staticmethod
|
||||
def next_cell(x: int, y: int, next: str) -> tuple:
|
||||
next_step = {
|
||||
"N": (0, -1),
|
||||
"S": (0, 1),
|
||||
"W": (-1, 0),
|
||||
"E": (1, 0)
|
||||
}
|
||||
add_x, add_y = next_step[next]
|
||||
return (x + add_x, y + add_y)
|
||||
|
||||
def reverse_path(next: str) -> str:
|
||||
reverse = {
|
||||
"N": "S",
|
||||
"S": "N",
|
||||
"W": "E",
|
||||
"E": "W"
|
||||
}
|
||||
return reverse[next]
|
||||
|
||||
@staticmethod
|
||||
def last(path: list):
|
||||
return path(len(path) - 1)
|
||||
|
||||
def back_on_step(path: list, w_h: tuple) -> list:
|
||||
last = DepthFirstSearch.last(path)
|
||||
r_cells = DepthFirstSearch.random_cells(path, last, w_h)
|
||||
while len(r_cells == 0):
|
||||
path.pop(len(path) - 1)
|
||||
last = DepthFirstSearch.last(path)
|
||||
r_cells = DepthFirstSearch.random_cells(path, last, w_h)
|
||||
return path
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
from .Cell import Cell
|
||||
from .Maze import Maze
|
||||
from .MazeGenerator import MazeGenerator
|
||||
from .MazeGenerator import MazeGenerator, DepthFirstSearch
|
||||
from .MazeSolver import MazeSolver
|
||||
|
||||
__version__ = "1.0.0"
|
||||
__author__ = "us"
|
||||
__all__ = ["Cell", "Maze", "MazeGenerator", "MazeSolver"]
|
||||
__all__ = ["Cell", "Maze", "MazeGenerator", "MazeSolver", "DepthFirstSearch"]
|
||||
|
||||
Reference in New Issue
Block a user