2024-02-13 21:13:15 +00:00
|
|
|
from typing import Dict
|
2024-02-12 23:48:41 +00:00
|
|
|
from graphics import Window, Point, Line
|
|
|
|
|
|
|
|
|
2024-02-13 15:52:28 +00:00
|
|
|
class CellWall:
|
|
|
|
"""
|
|
|
|
A CellWall represents the existence (or non-existence) of
|
|
|
|
a Cell's wall.
|
|
|
|
"""
|
|
|
|
|
2024-02-13 21:40:56 +00:00
|
|
|
def __init__(self, line: Line, window: Window) -> None:
|
2024-02-13 15:52:28 +00:00
|
|
|
self.exists = True
|
|
|
|
self.line = line
|
2024-02-13 21:40:56 +00:00
|
|
|
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)
|
2024-02-13 15:52:28 +00:00
|
|
|
|
|
|
|
|
2024-02-12 23:48:41 +00:00
|
|
|
class Cell:
|
|
|
|
"""
|
|
|
|
A Cell represents a grid on the maze.
|
|
|
|
"""
|
|
|
|
|
2024-02-13 21:13:15 +00:00
|
|
|
TOP_WALL = 0
|
|
|
|
BOTTOM_WALL = 1
|
|
|
|
LEFT_WALL = 2
|
2024-02-14 02:50:57 +00:00
|
|
|
RIGHT_WALL = 3
|
2024-02-13 21:13:15 +00:00
|
|
|
|
2024-02-12 23:48:41 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
x1: int, y1: int,
|
|
|
|
x2: int, y2: int,
|
2024-02-13 13:39:25 +00:00
|
|
|
window: Window = None,
|
2024-02-13 10:59:47 +00:00
|
|
|
) -> None:
|
2024-02-13 18:54:58 +00:00
|
|
|
# Validation
|
|
|
|
if (x2 < x1) or (y2 < y1):
|
|
|
|
raise CellInvalidError(x1, y1, x2, y2)
|
|
|
|
|
|
|
|
if (x2 - x1) < 2:
|
|
|
|
raise CellTooSmallError("horizontal", x2-x1)
|
|
|
|
|
|
|
|
if (y2 - y1) < 2:
|
|
|
|
raise CellTooSmallError("vertical", y2-y1)
|
|
|
|
|
2024-02-13 10:48:26 +00:00
|
|
|
# Define the cell walls
|
|
|
|
top_wall = Line(Point(x1, y1), Point(x2, y1))
|
|
|
|
bottom_wall = Line(Point(x1, y2), Point(x2, y2))
|
|
|
|
left_wall = Line(Point(x1, y1), Point(x1, y2))
|
|
|
|
right_wall = Line(Point(x2, y1), Point(x2, y2))
|
|
|
|
|
2024-02-13 21:13:15 +00:00
|
|
|
self._walls: Dict[int, CellWall] = {
|
2024-02-13 21:40:56 +00:00
|
|
|
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),
|
2024-02-13 21:13:15 +00:00
|
|
|
}
|
2024-02-13 10:48:26 +00:00
|
|
|
|
|
|
|
# Calculate the cell's central point
|
|
|
|
centre_x = x1 + ((x2 - x1) / 2)
|
|
|
|
centre_y = y1 + ((y2 - y1) / 2)
|
2024-02-13 13:39:25 +00:00
|
|
|
self._centre = Point(centre_x, centre_y)
|
2024-02-13 10:48:26 +00:00
|
|
|
|
|
|
|
# A reference to the root Window class for drawing purposes.
|
2024-02-13 13:39:25 +00:00
|
|
|
self._window = window
|
2024-02-12 23:48:41 +00:00
|
|
|
|
2024-02-13 18:06:46 +00:00
|
|
|
self.visited = False
|
|
|
|
|
2024-02-13 01:15:19 +00:00
|
|
|
def configure_walls(
|
|
|
|
self,
|
|
|
|
top: bool = True,
|
|
|
|
bottom: bool = True,
|
|
|
|
left: bool = True,
|
|
|
|
right: bool = True,
|
2024-02-13 10:59:47 +00:00
|
|
|
) -> None:
|
2024-02-13 01:15:19 +00:00
|
|
|
"""
|
|
|
|
configure_walls configures the existence of the Cell's walls.
|
|
|
|
"""
|
2024-02-13 21:13:15 +00:00
|
|
|
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
|
2024-02-12 23:48:41 +00:00
|
|
|
|
2024-02-13 10:48:26 +00:00
|
|
|
def centre(self) -> Point:
|
|
|
|
"""
|
|
|
|
centre returns the Cell's central point
|
|
|
|
"""
|
2024-02-13 13:39:25 +00:00
|
|
|
return self._centre
|
2024-02-13 10:48:26 +00:00
|
|
|
|
2024-02-13 21:13:15 +00:00
|
|
|
def wall_exists(self, wall: int) -> bool:
|
|
|
|
if wall not in self._walls:
|
2024-02-13 21:40:56 +00:00
|
|
|
raise CellInvalidWallError(wall)
|
2024-02-13 21:13:15 +00:00
|
|
|
return self._walls[wall].exists
|
|
|
|
|
2024-02-13 21:40:56 +00:00
|
|
|
# def break_walls_r(self, i: int, j: int) -> None:
|
2024-02-13 18:06:46 +00:00
|
|
|
# self.visited = True
|
|
|
|
# while True:
|
|
|
|
# list_i = []
|
|
|
|
# list_j = []
|
|
|
|
# break
|
|
|
|
|
2024-02-13 10:59:47 +00:00
|
|
|
def draw(self) -> None:
|
2024-02-13 01:15:19 +00:00
|
|
|
"""
|
|
|
|
draw draws the cell onto the canvas
|
|
|
|
"""
|
2024-02-13 15:52:28 +00:00
|
|
|
if not self._window:
|
|
|
|
return
|
|
|
|
|
2024-02-13 21:40:56 +00:00
|
|
|
for _, wall in self._walls.items():
|
|
|
|
wall.draw()
|
2024-02-13 01:15:19 +00:00
|
|
|
|
2024-02-13 10:59:47 +00:00
|
|
|
def draw_move(self, to_cell: 'Cell', undo: bool = False) -> None:
|
2024-02-13 10:48:26 +00:00
|
|
|
"""
|
|
|
|
draw_move draws a path between the centre of this cell and
|
|
|
|
the centre of the given cell.
|
|
|
|
"""
|
2024-02-13 15:52:28 +00:00
|
|
|
if not self._window:
|
|
|
|
return
|
2024-02-13 01:15:19 +00:00
|
|
|
|
2024-02-13 15:52:28 +00:00
|
|
|
fill_colour = "red"
|
|
|
|
if undo:
|
|
|
|
fill_colour = "grey"
|
|
|
|
line = Line(self.centre(), to_cell.centre())
|
|
|
|
self._window.draw_line(line, fill_colour)
|
2024-02-13 18:54:58 +00:00
|
|
|
|
|
|
|
|
2024-02-13 21:40:56 +00:00
|
|
|
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."
|
|
|
|
|
|
|
|
|
2024-02-13 18:54:58 +00:00
|
|
|
class CellInvalidError(Exception):
|
|
|
|
"""
|
2024-02-13 21:13:15 +00:00
|
|
|
CellInvalidError is raised when the program tries to create a Cell whose
|
2024-02-13 18:54:58 +00:00
|
|
|
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
|
2024-02-13 21:13:15 +00:00
|
|
|
should always represent the top left and the bottom right corners of
|
|
|
|
the cell (i.e. x1 < x2 and y1 < y2).
|
2024-02-13 18:54:58 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, x1: int, y1: int, x2: int, y2: int, *args):
|
|
|
|
super().__init__(args)
|
|
|
|
self.x1 = x1
|
|
|
|
self.x2 = x2
|
|
|
|
self.y1 = y1
|
|
|
|
self.y2 = y2
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f"Invalid Cell values received. Please ensure that both: x1 ({self.x1}) < x2 ({self.x2}), and y1 ({self.y1}) < y2 ({self.y2})"
|
|
|
|
|
|
|
|
|
|
|
|
class CellTooSmallError(Exception):
|
|
|
|
"""
|
2024-02-13 21:13:15 +00:00
|
|
|
CellTooSmallError is raised when the program tries to create a Cell
|
2024-02-13 18:54:58 +00:00
|
|
|
which is too small to correctly draw it's central point.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, size_type: str, size: int, *args):
|
|
|
|
super().__init__(args)
|
|
|
|
self.size_type = size_type
|
|
|
|
self.size = size
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f"The {self.size_type} size of the cell ({self.size}) is too small."
|