diff --git a/cell.py b/cell.py index 80f4ecc..a5d4c87 100644 --- a/cell.py +++ b/cell.py @@ -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,10 +93,10 @@ 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: + # def break_walls_r(self, i: int, j: int) -> None: # self.visited = True # while True: # list_i = [] @@ -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 diff --git a/tests.py b/tests.py index e5ddcce..c023349 100644 --- a/tests.py +++ b/tests.py @@ -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__":