From 2c76beb7337480a735b663d188e06d864312d465 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Sun, 18 Feb 2024 05:48:07 +0000 Subject: [PATCH] checkpoint: add ability to regenerate the maze --- app.py | 1 + cell.py | 6 ++++++ graphics.py | 6 ++++++ main.py | 20 -------------------- maze.py | 17 ++++++++++++++--- solver.py | 7 +++++-- 6 files changed, 32 insertions(+), 25 deletions(-) diff --git a/app.py b/app.py index 65c8f35..d0b705a 100644 --- a/app.py +++ b/app.py @@ -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() diff --git a/cell.py b/cell.py index 669be14..09b05d9 100644 --- a/cell.py +++ b/cell.py @@ -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 diff --git a/graphics.py b/graphics.py index 8a44d75..5883575 100644 --- a/graphics.py +++ b/graphics.py @@ -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") diff --git a/main.py b/main.py index b616903..689cd94 100644 --- a/main.py +++ b/main.py @@ -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__": diff --git a/maze.py b/maze.py index 52172ec..81eb8f0 100644 --- a/maze.py +++ b/maze.py @@ -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() diff --git a/solver.py b/solver.py index 0238ff0..88dc4ed 100644 --- a/solver.py +++ b/solver.py @@ -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: