refactor: add a CellWall class

Add a CellWall class to simplify the code within the Cell class.
This commit is contained in:
Dan Anglin 2024-02-13 01:15:19 +00:00
parent 60e557c725
commit a19d9260d2
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
2 changed files with 42 additions and 23 deletions

61
cell.py
View file

@ -12,28 +12,47 @@ class Cell:
x2: int, y2: int,
window: Window
):
self.__top_wall_exists = True
self.__bottom_wall_exists = True
self.__left_wall_exists = True
self.__right_wall_exists = True
self.__top_wall = Line(Point(x1, y1), Point(x2, y1))
self.__bottom_wall = Line(Point(x1, y2), Point(x2, y2))
self.__left_wall = Line(Point(x1, y1), Point(x1, y2))
self.__right_wall = Line(Point(x2, y1), Point(x2, y2))
self.__top_wall = CellWall(Line(Point(x1, y1), Point(x2, y1)))
self.__bottom_wall = CellWall(Line(Point(x1, y2), Point(x2, y2)))
self.__left_wall = CellWall(Line(Point(x1, y1), Point(x1, y2)))
self.__right_wall = CellWall(Line(Point(x2, y1), Point(x2, y2)))
self.__window = window
def set_walls(self, top: bool, bottom: bool, left: bool, right: bool):
self.__top_wall_exists = top
self.__bottom_wall_exists = bottom
self.__left_wall_exists = left
self.__right_wall_exists = right
def configure_walls(
self,
top: bool = True,
bottom: bool = True,
left: bool = True,
right: bool = True,
):
"""
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
def draw(self):
if self.__top_wall_exists:
self.__window.draw_line(self.__top_wall)
if self.__bottom_wall_exists:
self.__window.draw_line(self.__bottom_wall)
if self.__left_wall_exists:
self.__window.draw_line(self.__left_wall)
if self.__right_wall_exists:
self.__window.draw_line(self.__right_wall)
"""
draw draws the cell onto the canvas
"""
if self.__top_wall.exists:
self.__window.draw_line(self.__top_wall.line)
if self.__bottom_wall.exists:
self.__window.draw_line(self.__bottom_wall.line)
if self.__left_wall.exists:
self.__window.draw_line(self.__left_wall.line)
if self.__right_wall.exists:
self.__window.draw_line(self.__right_wall.line)
class CellWall:
"""
A CellWall represents the existence (or non-existence) of
a Cell's wall.
"""
def __init__(self, line: Line):
self.exists = True
self.line = line

View file

@ -11,12 +11,12 @@ def main():
# Draw vertical walls only
cell_2 = Cell(300, 50, 400, 100, window)
cell_2.set_walls(False, False, True, True)
cell_2.configure_walls(top=False, bottom=False, left=True, right=True)
cell_2.draw()
# Draw horizontal walls only
cell_3 = Cell(200, 400, 500, 600, window)
cell_3.set_walls(True, True, False, False)
cell_3.configure_walls(top=True, bottom=True, left=False, right=False)
cell_3.draw()
window.wait_for_close()