This commit is contained in:
parent
2c76beb733
commit
518f968088
5 changed files with 45 additions and 6 deletions
|
@ -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
|
||||
|
|
6
cell.py
6
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)
|
||||
|
|
24
graphics.py
24
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)
|
||||
|
|
8
maze.py
8
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)
|
||||
|
|
11
solver.py
11
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,
|
||||
|
|
Loading…
Reference in a new issue