diff --git a/cell.py b/cell.py index 82242d4..80f4ecc 100644 --- a/cell.py +++ b/cell.py @@ -1,3 +1,4 @@ +from typing import Dict from graphics import Window, Point, Line @@ -17,6 +18,11 @@ class Cell: A Cell represents a grid on the maze. """ + TOP_WALL = 0 + BOTTOM_WALL = 1 + LEFT_WALL = 2 + RIGHT_WALL = 4 + def __init__( self, x1: int, y1: int, @@ -39,10 +45,12 @@ class Cell: left_wall = Line(Point(x1, y1), Point(x1, y2)) right_wall = Line(Point(x2, y1), Point(x2, y2)) - self._top_wall = CellWall(top_wall) - self._bottom_wall = CellWall(bottom_wall) - self._left_wall = CellWall(left_wall) - self._right_wall = CellWall(right_wall) + 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), + } # Calculate the cell's central point centre_x = x1 + ((x2 - x1) / 2) @@ -64,10 +72,10 @@ class Cell: """ configure_walls configures the existence of the Cell's walls. """ - self._top_wall.exists = top - self._bottom_wall.exists = bottom - self._left_wall.exists = left - self._right_wall.exists = right + self._walls[Cell.TOP_WALL].exists = top + self._walls[Cell.BOTTOM_WALL].exists = bottom + self._walls[Cell.LEFT_WALL].exists = left + self._walls[Cell.RIGHT_WALL].exists = right def centre(self) -> Point: """ @@ -75,6 +83,11 @@ class Cell: """ return self._centre + def wall_exists(self, wall: int) -> bool: + if wall not in self._walls: + raise Exception("received invalid cell wall") + return self._walls[wall].exists + #def break_walls_r(self, i: int, j: int) -> None: # self.visited = True # while True: @@ -89,47 +102,47 @@ class Cell: if not self._window: return - if self._top_wall.exists: + if self.wall_exists(Cell.TOP_WALL): self._window.draw_line( - self._top_wall.line, + self._walls[Cell.TOP_WALL].line, fill_colour=self._window.cell_grid_colour, ) else: self._window.draw_line( - self._top_wall.line, + self._walls[Cell.TOP_WALL].line, fill_colour=self._window.background_colour, ) - if self._bottom_wall.exists: + if self.wall_exists(Cell.BOTTOM_WALL): self._window.draw_line( - self._bottom_wall.line, + self._walls[Cell.BOTTOM_WALL].line, fill_colour=self._window.cell_grid_colour, ) else: self._window.draw_line( - self._bottom_wall.line, + self._walls[Cell.BOTTOM_WALL].line, fill_colour=self._window.background_colour, ) - if self._left_wall.exists: + if self.wall_exists(Cell.LEFT_WALL): self._window.draw_line( - self._left_wall.line, + self._walls[Cell.LEFT_WALL].line, fill_colour=self._window.cell_grid_colour, ) else: self._window.draw_line( - self._left_wall.line, + self._walls[Cell.LEFT_WALL].line, fill_colour=self._window.background_colour, ) - if self._right_wall.exists: + if self.wall_exists(Cell.RIGHT_WALL): self._window.draw_line( - self._right_wall.line, + self._walls[Cell.RIGHT_WALL].line, fill_colour=self._window.cell_grid_colour, ) else: self._window.draw_line( - self._right_wall.line, + self._walls[Cell.RIGHT_WALL].line, fill_colour=self._window.background_colour, ) @@ -150,11 +163,11 @@ class Cell: class CellInvalidError(Exception): """ - CellInvalidError is returned when the program tries to create a Cell whose + CellInvalidError is raised when the program tries to create a Cell whose values are invalid. The values are invalid when x2 is smaller than x1 and/or y2 is smaller than y1. When creating a Cell the x and y values - should always represent the top left and the bottom right of the cell's - walls (i.e. x1 < x2 and y1 < y2). + should always represent the top left and the bottom right corners of + the cell (i.e. x1 < x2 and y1 < y2). """ def __init__(self, x1: int, y1: int, x2: int, y2: int, *args): @@ -170,7 +183,7 @@ class CellInvalidError(Exception): class CellTooSmallError(Exception): """ - CellTooSmallError is returned when the program tries to create a Cell + CellTooSmallError is raised when the program tries to create a Cell which is too small to correctly draw it's central point. """