Added secondary highlighting

This commit is contained in:
Kyler Olsen 2024-11-07 00:02:29 -07:00
parent f051ab58e3
commit dffc6ba4ca
2 changed files with 36 additions and 14 deletions

15
main.py
View File

@ -8,9 +8,10 @@ pygame.init()
# Constants
# CELL_SIZE = 10
# CLOCK_TICK = 20
HIGHLIGHT_COLOR = (0, 0, 255) # Blue for highlighted cells
WALL_COLOR = (0, 0, 0) # Black for walls
PATH_COLOR = (255, 255, 255) # White for paths
HIGHLIGHT_COLOR = (0, 255, 255) # Cyan for highlighted cells
SECONDARY_COLOR = (0, 0, 255) # Blue for secondary highlighted cells
WALL_COLOR = (0, 0, 0) # Black for walls
PATH_COLOR = (255, 255, 255) # White for paths
class MazeVisualizer:
@ -25,11 +26,13 @@ class MazeVisualizer:
self.screen.fill(PATH_COLOR)
for y in range(self.maze.height):
for x in range(self.maze.width):
if not self.maze[x, y]: # Wall
if not self.maze[x, y]:
pygame.draw.rect(self.screen, WALL_COLOR,
pygame.Rect(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE))
if self.maze.secondary((x, y)):
pygame.draw.rect(self.screen, SECONDARY_COLOR,
pygame.Rect(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE))
# Highlight the current cell
if self.maze.highlighted:
hx, hy = self.maze.highlighted
pygame.draw.rect(self.screen, HIGHLIGHT_COLOR,
@ -71,7 +74,7 @@ class MazeVisualizer:
# Draw the maze regardless of generation state
self.draw_maze()
pygame.display.flip()
clock.tick(CLOCK_TICK) # Adjust speed as needed
clock.tick(CLOCK_TICK)
pygame.quit()
sys.exit()

35
maze.py
View File

@ -25,6 +25,9 @@ class Maze(abc.ABC):
def step(self) -> bool: # returns False when algorithm is done
pass
def secondary(self, index: tuple[int,int]) -> bool:
return False
def __str__(self) -> str:
s = ""
for y in range(self.height):
@ -83,6 +86,9 @@ class VectorMaze(Maze):
def __getitem__(self, index: tuple[int,int]) -> VectorEnum:
pass
def secondary(self, index: tuple[int,int]) -> VectorEnum:
return VectorEnum.Null
def __list__(self) -> list[list[VectorEnum]]:
return [list([self[x,y] for x in range(self.width)]) for y in range(self.height)]
@ -124,23 +130,29 @@ class VectorWrapper(Maze):
else: return None
def __getitem__(self, index: tuple[int,int]) -> bool:
return self.__value(index, lambda o: self.__maze[o])
def secondary(self, index: tuple[int,int]) -> bool:
return self.__value(index, lambda o: self.__maze.secondary(o))
def __value(self, index: tuple[int,int], key) -> bool:
x, y = index
if x % 2 and y % 2:
if self.__maze[self.__to_vec(x), self.__to_vec(y)] != VectorEnum.Null:
if key((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:
if key((self.__to_vec(x), self.__to_vec(y-1))) == VectorEnum.Down:
return True
if self.__to_vec(y+1) < self.__maze.height:
if self.__maze[self.__to_vec(x), self.__to_vec(y+1)] == VectorEnum.Up:
if key((self.__to_vec(x), self.__to_vec(y+1))) == VectorEnum.Up:
return True
elif y % 2:
if self.__to_vec(x-1) >= 0:
if self.__maze[self.__to_vec(x-1), self.__to_vec(y)] == VectorEnum.Right:
if key((self.__to_vec(x-1), self.__to_vec(y))) == VectorEnum.Right:
return True
if self.__to_vec(x+1) < self.__maze.width:
if self.__maze[self.__to_vec(x+1), self.__to_vec(y)] == VectorEnum.Left:
if key((self.__to_vec(x+1), self.__to_vec(y))) == VectorEnum.Left:
return True
return False
@ -797,8 +809,16 @@ class Wilson(VectorMaze):
x, y = index
return self.__cells[y][x]
def secondary(self, index: tuple[int,int]) -> VectorEnum:
if self.__start is not None:
cell: tuple[int,int] = self.__start
while self.__path.get(cell, VectorEnum.Zero) != VectorEnum.Zero:
if cell == index: return self.__path[cell]
cell = self.__next(cell, self.__path[cell])
return VectorEnum.Null
def step(self) -> bool:
if self.highlighted:
if self.highlighted is not None and self.__start is not None:
highlighted = self.highlighted
neighbors = self._neighbors(highlighted)
cell = neighbors[random.randint(0, len(neighbors)-1)]
@ -806,8 +826,7 @@ class Wilson(VectorMaze):
if self.__cells[cell[1]][cell[0]] == VectorEnum.Null:
self.__path[cell] = VectorEnum.Zero
else:
# if self.highlighted is not None, self.__start is not None
cell: tuple[int,int] = self.__start # type: ignore
cell: tuple[int,int] = self.__start
while self.__path.get(cell, VectorEnum.Zero) != VectorEnum.Zero:
self.__cells[cell[1]][cell[0]] = self.__path[cell]
cell = self.__next(cell, self.__path[cell])