From 518f9680881c5cec1cee4afe4d99386babc322de Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Sun, 18 Feb 2024 07:32:09 +0000 Subject: [PATCH] add ability to re-run the simulation --- .forgejo/workflows/workflow.yaml | 2 +- cell.py | 6 ++++++ graphics.py | 24 ++++++++++++++++++++---- maze.py | 8 +++++++- solver.py | 11 +++++++++++ 5 files changed, 45 insertions(+), 6 deletions(-) diff --git a/.forgejo/workflows/workflow.yaml b/.forgejo/workflows/workflow.yaml index 5e87cd2..10c55c8 100644 --- a/.forgejo/workflows/workflow.yaml +++ b/.forgejo/workflows/workflow.yaml @@ -17,6 +17,6 @@ jobs: - name: Install Python 3 uses: https://github.com/actions/setup-python@v5 with: - python-version: '3.12' + python-version: '3.12.2' - name: Test Code run: python tests.py diff --git a/cell.py b/cell.py index 09b05d9..3fe6651 100644 --- a/cell.py +++ b/cell.py @@ -161,6 +161,12 @@ class Cell: raise ValueError(f"This is an unknown visitor ({visitor})") self._visited[visitor] = True + def unmark_visited(self, visitor: str) -> None: + """ + unmarks the cell as visited by the specified visitor. + """ + self._visited[visitor] = False + def reset(self) -> None: for label in CellWallLabels: self._walls[label].configure(True) diff --git a/graphics.py b/graphics.py index 5883575..eeb0219 100644 --- a/graphics.py +++ b/graphics.py @@ -1,4 +1,4 @@ -from typing import Dict +from typing import Dict, Tuple from time import sleep from tkinter import Canvas from line import Line, Point @@ -13,6 +13,8 @@ class Graphics(Canvas): width=width, height=height, ) + self._path_tag = "path" + self._cell_wall_tag = "cell_wall" def _redraw(self) -> None: """ @@ -25,8 +27,9 @@ class Graphics(Canvas): def _draw_line( self, line: Line, + tags: Tuple[str], fill_colour: str = "black", - width: int = 2 + width: int = 2, ) -> None: """ draws a line onto the canvas. @@ -36,6 +39,7 @@ class Graphics(Canvas): line.point_b.x, line.point_b.y, fill=fill_colour, width=width, + tags=tags, ) def draw_cell_walls(self, walls: Dict[CellWallLabels, CellWall]) -> None: @@ -46,6 +50,7 @@ class Graphics(Canvas): self._draw_line( line=walls[label].get_line(), fill_colour=walls[label].get_line_colour(), + tags=(self._cell_wall_tag), ) self._redraw() @@ -63,11 +68,22 @@ class Graphics(Canvas): fill_colour = "red" if undo: fill_colour = "grey" - self._draw_line(line, fill_colour) + self._draw_line( + line=line, + fill_colour=fill_colour, + tags=(self._path_tag), + ) self._redraw() - def clear(self) -> None: + def clear_all(self) -> None: """ clears the canvas """ self.delete("all") + + def clear_paths(self) -> None: + """ + deletes all the lines that have the + path tag. + """ + self.delete(self._path_tag) diff --git a/maze.py b/maze.py index 81eb8f0..981b895 100644 --- a/maze.py +++ b/maze.py @@ -119,7 +119,7 @@ class Maze: self._cell_grid: List[List[Cell]] = None self._create_cell_grid() else: - self._graphics.clear() + self._graphics.clear_all() self._reset_cell_grid() self._draw_cell_grid() self._open_entrance_and_exit() @@ -352,3 +352,9 @@ class Maze: for i in range(self._height): for j in range(self._width): self._cell_grid[i][j].reset() + + def reset_solution(self, visitor: str) -> None: + self._graphics.clear_paths() + for i in range(self._height): + for j in range(self._width): + self._cell_grid[i][j].unmark_visited(visitor) diff --git a/solver.py b/solver.py index 88dc4ed..125ac45 100644 --- a/solver.py +++ b/solver.py @@ -23,6 +23,9 @@ class Solver: random.seed() + def _reset(self): + self._game.reset_solution(self._solver) + def solve( self, solve_method: Callable[[MazePosition, MazePosition, bool], bool], @@ -45,6 +48,14 @@ class Solver: last_j=self._game.get_last_j(), ) + # Clear the maze if there was a previous run + if self._game.cell_was_visited_by( + i=start_position.i, + j=start_position.j, + visitor=self._solver, + ): + self._game.reset_solution(self._solver) + return solve_method( start_position, end_position,