This commit is contained in:
Kyler Olsen 2024-11-04 20:43:31 -07:00
parent ea0ec289a7
commit 025f00f6aa
2 changed files with 29 additions and 4 deletions

13
main.py
View File

@ -44,7 +44,13 @@ class MazeVisualizer:
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYUP:
generation_complete = not generation_complete
if event.key == pygame.K_SPACE:
generation_complete = not generation_complete
if event.key == pygame.K_o:
if isinstance(self.maze, maze.VectorWrapper):
self.maze = maze.VectorWrapper(maze.OriginShift.clone(self.maze.maze))
if event.key == pygame.K_s:
if generation_complete: self.maze.step()
# Only step through the algorithm if it's not finished
if not generation_complete:
@ -60,7 +66,10 @@ class MazeVisualizer:
# my_maze = maze.RecursiveBacktracker(63)
# my_maze = maze.VectorWrapper(maze.OriginShift(32))
my_maze = maze.VectorWrapper(maze.BinaryTree(32, bias=maze.VectorEnum.Up | maze.VectorEnum.Left))
# my_maze = maze.VectorWrapper(maze.BinaryTree(32, bias=maze.VectorEnum.Up | maze.VectorEnum.Left))
# my_maze = maze.VectorWrapper(maze.BinaryTree(32, bias=maze.VectorEnum.Up | maze.VectorEnum.Right))
# my_maze = maze.VectorWrapper(maze.BinaryTree(32, bias=maze.VectorEnum.Down | maze.VectorEnum.Left))
my_maze = maze.VectorWrapper(maze.BinaryTree(32, bias=maze.VectorEnum.Down | maze.VectorEnum.Right))
# for _ in range(512): my_maze.step()
# for _ in range(2048): my_maze.step()

20
maze.py
View File

@ -54,6 +54,7 @@ class Maze(abc.ABC):
class VectorEnum(enum.Enum):
Null = -1
Zero = 0
Up = 2
Down = 4
@ -83,6 +84,9 @@ class VectorMaze(Maze):
def __getitem__(self, index: tuple[int,int]) -> VectorEnum:
pass
def __list__(self) -> list[list[VectorEnum]]:
return [list([self[x,y] for x in range(self.width)]) for y in range(self.height)]
def _neighbors(self, index: tuple[int,int]) -> list[tuple[int,int]]:
neighbors: list[tuple[int,int]] = []
x, y = index
@ -122,7 +126,9 @@ class VectorWrapper(Maze):
def __getitem__(self, index: tuple[int,int]) -> bool:
x, y = index
if x % 2 and y % 2: return True
if x % 2 and y % 2:
if self.__maze[self.__to_vec(x), self.__to_vec(y)] != VectorEnum.Null:
return True
elif x % 2:
if self.__to_vec(y-1) >= 0:
if self.__maze[self.__to_vec(x), self.__to_vec(y-1)] == VectorEnum.Down:
@ -141,6 +147,10 @@ class VectorWrapper(Maze):
def step(self) -> bool: return self.__maze.step()
@property
def maze(self) -> VectorMaze:
return self.__maze
@staticmethod
def __from_vec(i: int) -> int:
return ((i * 2) + 1)
@ -246,6 +256,12 @@ class OriginShift(VectorMaze):
return True
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)]
return self
def __direction(self, start: tuple[int,int], end: tuple[int,int]) -> VectorEnum:
if start[0] - end[0] > 0: return VectorEnum.Left
if start[0] - end[0] < 0: return VectorEnum.Right
@ -280,7 +296,7 @@ class BinaryTree(VectorMaze):
self.__x = 0
self.__y = 0
self.__cells = [list([VectorEnum.Zero for x in range(self.width)]) for y in range(self.height)]
self.__cells = [list([VectorEnum.Null for x in range(self.width)]) for y in range(self.height)]
@property
def width(self) -> int: