2024-02-13 12:18:47 +00:00
|
|
|
from typing import List
|
|
|
|
from time import sleep
|
2024-02-13 18:06:46 +00:00
|
|
|
import random
|
2024-02-13 12:18:47 +00:00
|
|
|
from graphics import Window
|
|
|
|
from cell import Cell
|
|
|
|
|
|
|
|
|
|
|
|
class Maze:
|
|
|
|
"""
|
|
|
|
Maze represents a two-dimensional grid of Cells.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
x_position: int,
|
|
|
|
y_position: int,
|
2024-02-13 14:21:35 +00:00
|
|
|
num_cell_rows: int,
|
|
|
|
num_cells_per_row: int,
|
2024-02-13 12:18:47 +00:00
|
|
|
cell_size_x: int,
|
|
|
|
cell_size_y: int,
|
2024-02-13 13:39:25 +00:00
|
|
|
window: Window = None,
|
2024-02-13 18:06:46 +00:00
|
|
|
seed=None,
|
2024-02-13 13:39:25 +00:00
|
|
|
) -> None:
|
|
|
|
self._x_position = x_position
|
|
|
|
self._y_position = y_position
|
2024-02-13 14:21:35 +00:00
|
|
|
self._num_cell_rows = num_cell_rows
|
|
|
|
self._num_cells_per_row = num_cells_per_row
|
2024-02-13 13:39:25 +00:00
|
|
|
self._cell_size_x = cell_size_x
|
|
|
|
self._cell_size_y = cell_size_y
|
|
|
|
self._window = window
|
2024-02-13 18:06:46 +00:00
|
|
|
self._seed = None
|
|
|
|
|
|
|
|
# Generate the seed if it does not exist
|
|
|
|
if seed:
|
|
|
|
self._seed = seed
|
|
|
|
else:
|
|
|
|
random.seed(self._seed)
|
2024-02-13 12:18:47 +00:00
|
|
|
|
|
|
|
# Create the Maze's cells
|
2024-02-13 14:21:35 +00:00
|
|
|
self._cells: List[List[Cell]] = [None for i in range(self._num_cell_rows)]
|
2024-02-13 13:39:25 +00:00
|
|
|
self._create_cells()
|
2024-02-13 15:52:28 +00:00
|
|
|
self._break_entrance_and_exit()
|
2024-02-13 12:18:47 +00:00
|
|
|
|
2024-02-13 13:39:25 +00:00
|
|
|
def _create_cells(self):
|
|
|
|
cursor_x = self._x_position
|
|
|
|
cursor_y = self._y_position
|
2024-02-13 12:18:47 +00:00
|
|
|
|
2024-02-13 14:21:35 +00:00
|
|
|
for i in range(self._num_cell_rows):
|
|
|
|
cells: List[Cell] = [None for j in range(self._num_cells_per_row)]
|
|
|
|
for j in range(self._num_cells_per_row):
|
2024-02-13 12:18:47 +00:00
|
|
|
cell = Cell(
|
|
|
|
cursor_x,
|
|
|
|
cursor_y,
|
2024-02-13 13:39:25 +00:00
|
|
|
(cursor_x + self._cell_size_x),
|
|
|
|
(cursor_y + self._cell_size_y),
|
|
|
|
self._window
|
2024-02-13 12:18:47 +00:00
|
|
|
)
|
2024-02-13 14:21:35 +00:00
|
|
|
cells[j] = cell
|
|
|
|
if j == self._num_cells_per_row - 1:
|
2024-02-13 13:39:25 +00:00
|
|
|
cursor_x = self._x_position
|
2024-02-13 12:18:47 +00:00
|
|
|
else:
|
2024-02-13 13:39:25 +00:00
|
|
|
cursor_x += self._cell_size_x
|
2024-02-13 14:21:35 +00:00
|
|
|
self._cells[i] = cells
|
2024-02-13 13:39:25 +00:00
|
|
|
cursor_y += self._cell_size_y
|
2024-02-13 12:18:47 +00:00
|
|
|
|
2024-02-13 13:39:25 +00:00
|
|
|
if self._window:
|
|
|
|
self._draw_cells()
|
2024-02-13 12:18:47 +00:00
|
|
|
|
2024-02-13 15:52:28 +00:00
|
|
|
def _break_entrance_and_exit(self):
|
|
|
|
# break entrance and draw
|
|
|
|
self._cells[0][0].configure_walls(top=False)
|
|
|
|
self._cells[0][0].draw()
|
|
|
|
# break exit and draw
|
|
|
|
self._cells[self._num_cell_rows - 1][self._num_cells_per_row - 1].configure_walls(bottom=False)
|
|
|
|
self._cells[self._num_cell_rows - 1][self._num_cells_per_row - 1].draw()
|
|
|
|
|
2024-02-13 13:39:25 +00:00
|
|
|
def _draw_cells(self):
|
2024-02-13 14:21:35 +00:00
|
|
|
for i in range(self._num_cell_rows):
|
|
|
|
for j in range(self._num_cells_per_row):
|
2024-02-13 13:39:25 +00:00
|
|
|
self._cells[i][j].draw()
|
|
|
|
self._animate()
|
2024-02-13 12:18:47 +00:00
|
|
|
|
2024-02-13 13:39:25 +00:00
|
|
|
def _animate(self):
|
|
|
|
self._window.redraw()
|
2024-02-13 12:18:47 +00:00
|
|
|
sleep(0.05)
|