Added a type conversion
This commit is contained in:
parent
8ac4b71665
commit
b9ecf665a8
2
main.py
2
main.py
|
@ -61,6 +61,8 @@ class MazeVisualizer:
|
|||
if event.key == pygame.K_m:
|
||||
for _ in range(10000):
|
||||
if not self.maze.step(): break
|
||||
if event.key == pygame.K_o:
|
||||
self.maze = maze.VectorWrapper.convert(self.maze)
|
||||
|
||||
# Only step through the algorithm if it's not finished
|
||||
if not generation_complete:
|
||||
|
|
37
maze.py
37
maze.py
|
@ -158,6 +158,33 @@ class VectorWrapper(Maze):
|
|||
def __to_vec(i: int) -> int:
|
||||
return ((i - 1) // 2)
|
||||
|
||||
@classmethod
|
||||
def convert(cls, maze: Maze) -> "VectorWrapper":
|
||||
width, height = cls.__to_vec(maze.width), cls.__to_vec(maze.height)
|
||||
|
||||
stack = [(random.randint(0,width-1),random.randint(0,height-1))]
|
||||
|
||||
cells = [list([VectorEnum.Null for _ in range(width)]) for _ in range(height)]
|
||||
|
||||
cells[stack[-1][1]][stack[-1][0]] = VectorEnum.Zero
|
||||
|
||||
while stack:
|
||||
x, y = stack.pop()
|
||||
if x - 1 >= 0 and cells[y][x - 1] == VectorEnum.Null and maze[cls.__from_vec(x) - 1, cls.__from_vec(y)]:
|
||||
stack.append((x - 1, y))
|
||||
cells[y][x - 1] = VectorEnum.Right
|
||||
if x + 1 < width and cells[y][x + 1] == VectorEnum.Null and maze[cls.__from_vec(x) + 1, cls.__from_vec(y)]:
|
||||
stack.append((x + 1, y))
|
||||
cells[y][x + 1] = VectorEnum.Left
|
||||
if y - 1 >= 0 and cells[y - 1][x] == VectorEnum.Null and maze[cls.__from_vec(x), cls.__from_vec(y) - 1]:
|
||||
stack.append((x, y - 1))
|
||||
cells[y - 1][x] = VectorEnum.Down
|
||||
if y + 1 < height and cells[y + 1][x] == VectorEnum.Null and maze[cls.__from_vec(x), cls.__from_vec(y) + 1]:
|
||||
stack.append((x, y + 1))
|
||||
cells[y + 1][x] = VectorEnum.Up
|
||||
|
||||
return cls(OriginShift.clone(cells))
|
||||
|
||||
|
||||
class RecursiveBacktracker(Maze):
|
||||
|
||||
|
@ -256,9 +283,13 @@ class OriginShift(VectorMaze):
|
|||
else: return False
|
||||
|
||||
@classmethod
|
||||
def clone(cls, other: VectorMaze) -> "OriginShift":
|
||||
self = cls(other.width, other.height)
|
||||
self.__cells = [list([other[x,y] for x in range(other.width)]) for y in range(other.height)]
|
||||
def clone(cls, other: VectorMaze | list[list[VectorEnum]]) -> "OriginShift":
|
||||
if isinstance(other, VectorMaze):
|
||||
self = cls(other.width, other.height)
|
||||
self.__cells = [list([other[x,y] for x in range(other.width)]) for y in range(other.height)]
|
||||
else:
|
||||
self = cls(len(other[0]), len(other))
|
||||
self.__cells = [list([other[y][x] for x in range(len(other[0]))]) for y in range(len(other))]
|
||||
return self
|
||||
|
||||
def __direction(self, start: tuple[int,int], end: tuple[int,int]) -> VectorEnum:
|
||||
|
|
Loading…
Reference in New Issue