checkpoint: add ability to regenerate the maze
Some checks failed
test / test (pull_request) Failing after 1m0s

This commit is contained in:
Dan Anglin 2024-02-18 05:48:07 +00:00
parent 8b55d28060
commit 2c76beb733
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
6 changed files with 32 additions and 25 deletions

1
app.py
View file

@ -18,6 +18,7 @@ class App(Tk):
centre_x = int(screen_width/2 - width/2)
centre_y = int(screen_height/2 - height/2)
self.geometry(f"{width}x{height}+{centre_x}+{centre_y}")
self.resizable(False, False)
# Styling
self.style = ttk.Style()

View file

@ -161,5 +161,11 @@ class Cell:
raise ValueError(f"This is an unknown visitor ({visitor})")
self._visited[visitor] = True
def reset(self) -> None:
for label in CellWallLabels:
self._walls[label].configure(True)
for k in self._visited:
self._visited[k] = False
def get_walls(self) -> Dict[CellWallLabels, CellWall]:
return self._walls

View file

@ -65,3 +65,9 @@ class Graphics(Canvas):
fill_colour = "grey"
self._draw_line(line, fill_colour)
self._redraw()
def clear(self) -> None:
"""
clears the canvas
"""
self.delete("all")

20
main.py
View file

@ -4,26 +4,6 @@ from app import App
def main():
app = App()
app.mainloop()
#window = Window(800, 800)
#game = Maze(
# x_position=10,
# y_position=10,
# height=19,
# width=19,
# cell_height=40,
# cell_width=40,
# window=window
#)
#solver = Solver(game)
#if solver.solve(solver.solve_with_bfs_r, True):
# print("Maze solved successfully :)")
#else:
# print("I'm unable to solve the maze :(")
#window.mainloop()
if __name__ == "__main__":

17
maze.py
View file

@ -108,14 +108,19 @@ class Maze:
random.seed(seed)
# Create the Maze's cells
self._cell_grid: List[List[Cell]] = []
self._create_cell_grid()
self._cell_grid: List[List[Cell]] = None
def generate(self):
"""
randomly generates a new maze.
"""
if self._cell_grid is None:
self._cell_grid: List[List[Cell]] = None
self._create_cell_grid()
else:
self._graphics.clear()
self._reset_cell_grid()
self._draw_cell_grid()
self._open_entrance_and_exit()
self._break_walls_r(MazePosition(
@ -288,7 +293,8 @@ class Maze:
draws a path between two cells in an animated way.
"""
self._graphics.draw_path(current_cell.centre(), next_cell.centre(), undo)
self._graphics.draw_path(
current_cell.centre(), next_cell.centre(), undo)
def mark_cell_as_visited(self, i: int, j: int, visitor: str) -> None:
"""
@ -341,3 +347,8 @@ class Maze:
left=left,
right=right,
)
def _reset_cell_grid(self) -> None:
for i in range(self._height):
for j in range(self._width):
self._cell_grid[i][j].reset()

View file

@ -45,7 +45,11 @@ class Solver:
last_j=self._game.get_last_j(),
)
return solve_method(start_position, end_position, enable_random_direction)
return solve_method(
start_position,
end_position,
enable_random_direction,
)
def solve_with_dfs_r(
self,
@ -62,7 +66,6 @@ class Solver:
visitor=self._solver,
)
while True:
possible_directions: List[MazeDirection] = []
for direction in MazeDirection: