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:
parent
808e1cee53
commit
d87df48df2
2 changed files with 34 additions and 53 deletions
81
cell.py
81
cell.py
|
@ -8,9 +8,17 @@ class CellWall:
|
||||||
a Cell's wall.
|
a Cell's wall.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, line: Line) -> None:
|
def __init__(self, line: Line, window: Window) -> None:
|
||||||
self.exists = True
|
self.exists = True
|
||||||
self.line = line
|
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:
|
class Cell:
|
||||||
|
@ -46,10 +54,10 @@ class Cell:
|
||||||
right_wall = Line(Point(x2, y1), Point(x2, y2))
|
right_wall = Line(Point(x2, y1), Point(x2, y2))
|
||||||
|
|
||||||
self._walls: Dict[int, CellWall] = {
|
self._walls: Dict[int, CellWall] = {
|
||||||
Cell.TOP_WALL: CellWall(top_wall),
|
Cell.TOP_WALL: CellWall(top_wall, window),
|
||||||
Cell.BOTTOM_WALL: CellWall(bottom_wall),
|
Cell.BOTTOM_WALL: CellWall(bottom_wall, window),
|
||||||
Cell.LEFT_WALL: CellWall(left_wall),
|
Cell.LEFT_WALL: CellWall(left_wall, window),
|
||||||
Cell.RIGHT_WALL: CellWall(right_wall),
|
Cell.RIGHT_WALL: CellWall(right_wall, window),
|
||||||
}
|
}
|
||||||
|
|
||||||
# Calculate the cell's central point
|
# Calculate the cell's central point
|
||||||
|
@ -85,10 +93,10 @@ class Cell:
|
||||||
|
|
||||||
def wall_exists(self, wall: int) -> bool:
|
def wall_exists(self, wall: int) -> bool:
|
||||||
if wall not in self._walls:
|
if wall not in self._walls:
|
||||||
raise Exception("received invalid cell wall")
|
raise CellInvalidWallError(wall)
|
||||||
return self._walls[wall].exists
|
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
|
# self.visited = True
|
||||||
# while True:
|
# while True:
|
||||||
# list_i = []
|
# list_i = []
|
||||||
|
@ -102,49 +110,8 @@ class Cell:
|
||||||
if not self._window:
|
if not self._window:
|
||||||
return
|
return
|
||||||
|
|
||||||
if self.wall_exists(Cell.TOP_WALL):
|
for _, wall in self._walls.items():
|
||||||
self._window.draw_line(
|
wall.draw()
|
||||||
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,
|
|
||||||
)
|
|
||||||
|
|
||||||
def draw_move(self, to_cell: 'Cell', undo: bool = False) -> None:
|
def draw_move(self, to_cell: 'Cell', undo: bool = False) -> None:
|
||||||
"""
|
"""
|
||||||
|
@ -161,6 +128,20 @@ class Cell:
|
||||||
self._window.draw_line(line, fill_colour)
|
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):
|
class CellInvalidError(Exception):
|
||||||
"""
|
"""
|
||||||
CellInvalidError is raised when the program tries to create a Cell whose
|
CellInvalidError is raised when the program tries to create a Cell whose
|
||||||
|
|
6
tests.py
6
tests.py
|
@ -1,5 +1,6 @@
|
||||||
import unittest
|
import unittest
|
||||||
from maze import Maze
|
from maze import Maze
|
||||||
|
from cell import Cell
|
||||||
|
|
||||||
|
|
||||||
class Tests(unittest.TestCase):
|
class Tests(unittest.TestCase):
|
||||||
|
@ -55,9 +56,8 @@ class Tests(unittest.TestCase):
|
||||||
2,
|
2,
|
||||||
2,
|
2,
|
||||||
)
|
)
|
||||||
self.assertFalse(maze._cells[0][0]._top_wall.exists)
|
self.assertFalse(maze._cells[0][0].wall_exists(Cell.TOP_WALL))
|
||||||
self.assertFalse(
|
self.assertFalse(maze._cells[number_of_cell_rows - 1][number_of_cells_per_row - 1].wall_exists(Cell.BOTTOM_WALL))
|
||||||
maze._cells[number_of_cell_rows - 1][number_of_cells_per_row - 1]._bottom_wall.exists)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in a new issue