refactor: add a draw method in CellWall

Add a draw() method in the CellWall class to reduce repeated code in the
Cell class.

Updated test case.
This commit is contained in:
Dan Anglin 2024-02-13 21:40:56 +00:00
parent 808e1cee53
commit d87df48df2
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
2 changed files with 34 additions and 53 deletions

79
cell.py
View file

@ -8,9 +8,17 @@ class CellWall:
a Cell's wall.
"""
def __init__(self, line: Line) -> None:
def __init__(self, line: Line, window: Window) -> None:
self.exists = True
self.line = line
self._window = window
def draw(self):
fill_colour = self._window.cell_grid_colour
if not self.exists:
fill_colour = self._window.background_colour
self._window.draw_line(self.line, fill_colour=fill_colour)
class Cell:
@ -46,10 +54,10 @@ class Cell:
right_wall = Line(Point(x2, y1), Point(x2, y2))
self._walls: Dict[int, CellWall] = {
Cell.TOP_WALL: CellWall(top_wall),
Cell.BOTTOM_WALL: CellWall(bottom_wall),
Cell.LEFT_WALL: CellWall(left_wall),
Cell.RIGHT_WALL: CellWall(right_wall),
Cell.TOP_WALL: CellWall(top_wall, window),
Cell.BOTTOM_WALL: CellWall(bottom_wall, window),
Cell.LEFT_WALL: CellWall(left_wall, window),
Cell.RIGHT_WALL: CellWall(right_wall, window),
}
# Calculate the cell's central point
@ -85,7 +93,7 @@ class Cell:
def wall_exists(self, wall: int) -> bool:
if wall not in self._walls:
raise Exception("received invalid cell wall")
raise CellInvalidWallError(wall)
return self._walls[wall].exists
# def break_walls_r(self, i: int, j: int) -> None:
@ -102,49 +110,8 @@ class Cell:
if not self._window:
return
if self.wall_exists(Cell.TOP_WALL):
self._window.draw_line(
self._walls[Cell.TOP_WALL].line,
fill_colour=self._window.cell_grid_colour,
)
else:
self._window.draw_line(
self._walls[Cell.TOP_WALL].line,
fill_colour=self._window.background_colour,
)
if self.wall_exists(Cell.BOTTOM_WALL):
self._window.draw_line(
self._walls[Cell.BOTTOM_WALL].line,
fill_colour=self._window.cell_grid_colour,
)
else:
self._window.draw_line(
self._walls[Cell.BOTTOM_WALL].line,
fill_colour=self._window.background_colour,
)
if self.wall_exists(Cell.LEFT_WALL):
self._window.draw_line(
self._walls[Cell.LEFT_WALL].line,
fill_colour=self._window.cell_grid_colour,
)
else:
self._window.draw_line(
self._walls[Cell.LEFT_WALL].line,
fill_colour=self._window.background_colour,
)
if self.wall_exists(Cell.RIGHT_WALL):
self._window.draw_line(
self._walls[Cell.RIGHT_WALL].line,
fill_colour=self._window.cell_grid_colour,
)
else:
self._window.draw_line(
self._walls[Cell.RIGHT_WALL].line,
fill_colour=self._window.background_colour,
)
for _, wall in self._walls.items():
wall.draw()
def draw_move(self, to_cell: 'Cell', undo: bool = False) -> None:
"""
@ -161,6 +128,20 @@ class Cell:
self._window.draw_line(line, fill_colour)
class CellInvalidWallError(Exception):
"""
CellInvalidWallError is raised when the program tries to specify a Cell's
Wall that does not exist.
"""
def __init__(self, wall: int, *args):
super().__init__(args)
self.wall = wall
def __str__(self):
return f"Invalid Cell Wall (wall int: {self.wall}) specified."
class CellInvalidError(Exception):
"""
CellInvalidError is raised when the program tries to create a Cell whose

View file

@ -1,5 +1,6 @@
import unittest
from maze import Maze
from cell import Cell
class Tests(unittest.TestCase):
@ -55,9 +56,8 @@ class Tests(unittest.TestCase):
2,
2,
)
self.assertFalse(maze._cells[0][0]._top_wall.exists)
self.assertFalse(
maze._cells[number_of_cell_rows - 1][number_of_cells_per_row - 1]._bottom_wall.exists)
self.assertFalse(maze._cells[0][0].wall_exists(Cell.TOP_WALL))
self.assertFalse(maze._cells[number_of_cell_rows - 1][number_of_cells_per_row - 1].wall_exists(Cell.BOTTOM_WALL))
if __name__ == "__main__":