fix: fixed issues with the maze generation
- Fixed the issue where during the reconfiguration of the walls of a cell, those that weren't being reconfigured were reset to True regardless of their previous configuration. - During testing, don't run the _break_walls_r method because it will run into the RecursionError exception. - Simplify the code for drawing cells and fix the redrawing wasn't applied properly.
This commit is contained in:
parent
a919c2698b
commit
4d72fe8e43
3 changed files with 60 additions and 36 deletions
12
cell.py
12
cell.py
|
@ -80,17 +80,21 @@ class Cell:
|
||||||
|
|
||||||
def configure_walls(
|
def configure_walls(
|
||||||
self,
|
self,
|
||||||
top: bool = True,
|
top: bool = None,
|
||||||
bottom: bool = True,
|
bottom: bool = None,
|
||||||
left: bool = True,
|
left: bool = None,
|
||||||
right: bool = True,
|
right: bool = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""
|
"""
|
||||||
configure_walls configures the existence of the Cell's walls.
|
configure_walls configures the existence of the Cell's walls.
|
||||||
"""
|
"""
|
||||||
|
if top is not None:
|
||||||
self._walls[CellWallLabel.TOP].exists = top
|
self._walls[CellWallLabel.TOP].exists = top
|
||||||
|
if bottom is not None:
|
||||||
self._walls[CellWallLabel.BOTTOM].exists = bottom
|
self._walls[CellWallLabel.BOTTOM].exists = bottom
|
||||||
|
if left is not None:
|
||||||
self._walls[CellWallLabel.LEFT].exists = left
|
self._walls[CellWallLabel.LEFT].exists = left
|
||||||
|
if right is not None:
|
||||||
self._walls[CellWallLabel.RIGHT].exists = right
|
self._walls[CellWallLabel.RIGHT].exists = right
|
||||||
|
|
||||||
def centre(self) -> Point:
|
def centre(self) -> Point:
|
||||||
|
|
70
maze.py
70
maze.py
|
@ -20,6 +20,7 @@ class Maze:
|
||||||
cell_size_y: int,
|
cell_size_y: int,
|
||||||
window: Window = None,
|
window: Window = None,
|
||||||
seed=None,
|
seed=None,
|
||||||
|
test=False,
|
||||||
) -> None:
|
) -> None:
|
||||||
self._x_position = x_position
|
self._x_position = x_position
|
||||||
self._y_position = y_position
|
self._y_position = y_position
|
||||||
|
@ -33,13 +34,15 @@ class Maze:
|
||||||
random.seed(seed)
|
random.seed(seed)
|
||||||
|
|
||||||
# Create the Maze's cells
|
# Create the Maze's cells
|
||||||
self._cells: List[List[Cell]] = [None for i in range(self._num_cell_rows)]
|
self._cells: List[List[Cell]] = [
|
||||||
self._create_cells()
|
None for i in range(self._num_cell_rows)]
|
||||||
|
self._create_cell_grid()
|
||||||
self._open_entrance_and_exit()
|
self._open_entrance_and_exit()
|
||||||
if self._window:
|
|
||||||
|
if not test:
|
||||||
self._break_walls_r(0, 0)
|
self._break_walls_r(0, 0)
|
||||||
|
|
||||||
def _create_cells(self) -> None:
|
def _create_cell_grid(self) -> None:
|
||||||
"""
|
"""
|
||||||
creates all the cells and draws them.
|
creates all the cells and draws them.
|
||||||
"""
|
"""
|
||||||
|
@ -65,7 +68,16 @@ class Maze:
|
||||||
cursor_y += self._cell_size_y
|
cursor_y += self._cell_size_y
|
||||||
|
|
||||||
if self._window:
|
if self._window:
|
||||||
self._draw_cells()
|
self._draw_cell_grid()
|
||||||
|
|
||||||
|
def _draw_cell_grid(self) -> None:
|
||||||
|
"""
|
||||||
|
draws all the cells on the maze with a short pause between each cell
|
||||||
|
for animation purposes.
|
||||||
|
"""
|
||||||
|
for y in range(self._num_cell_rows):
|
||||||
|
for x in range(self._num_cells_per_row):
|
||||||
|
self._draw_cell(y=y, x=x)
|
||||||
|
|
||||||
def _open_entrance_and_exit(self) -> None:
|
def _open_entrance_and_exit(self) -> None:
|
||||||
"""
|
"""
|
||||||
|
@ -74,12 +86,22 @@ class Maze:
|
||||||
at the bottom right of the maze.
|
at the bottom right of the maze.
|
||||||
"""
|
"""
|
||||||
self._cells[0][0].configure_walls(top=False)
|
self._cells[0][0].configure_walls(top=False)
|
||||||
self._cells[0][0].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].configure_walls(bottom=False)
|
if self._window:
|
||||||
self._cells[self._num_cell_rows-1][self._num_cells_per_row-1].draw()
|
self._draw_cell(0, 0)
|
||||||
|
self._draw_cell(
|
||||||
|
y=self._num_cell_rows-1,
|
||||||
|
x=self._num_cells_per_row-1
|
||||||
|
)
|
||||||
|
|
||||||
def _break_walls_r(self, y: int, x: int) -> None:
|
def _break_walls_r(self, y: int, x: int) -> None:
|
||||||
|
"""
|
||||||
|
_break_walls_r generates a random maze by traversing through the
|
||||||
|
cells and randomly knocking down the walls to create the maze's paths.
|
||||||
|
"""
|
||||||
|
|
||||||
current_cell = self._cells[y][x]
|
current_cell = self._cells[y][x]
|
||||||
current_cell.visited = True
|
current_cell.visited = True
|
||||||
above, below, left, right = "above", "below", "left", "right"
|
above, below, left, right = "above", "below", "left", "right"
|
||||||
|
@ -94,7 +116,7 @@ class Maze:
|
||||||
to_visit: List[str] = []
|
to_visit: List[str] = []
|
||||||
|
|
||||||
for k, value in adjacent_cells.items():
|
for k, value in adjacent_cells.items():
|
||||||
if (value[0] < 0)or (value[1] < 0) or (value[0] > self._num_cell_rows-1) or (value[1] > self._num_cells_per_row-1):
|
if (value[0] < 0) or (value[1] < 0) or (value[0] > self._num_cell_rows-1) or (value[1] > self._num_cells_per_row-1):
|
||||||
continue
|
continue
|
||||||
if self._cells[value[0]][value[1]].visited:
|
if self._cells[value[0]][value[1]].visited:
|
||||||
continue
|
continue
|
||||||
|
@ -102,11 +124,13 @@ class Maze:
|
||||||
to_visit.append(k)
|
to_visit.append(k)
|
||||||
|
|
||||||
if len(to_visit) == 0:
|
if len(to_visit) == 0:
|
||||||
current_cell.draw()
|
if self._window:
|
||||||
|
self._draw_cell(y=y, x=x)
|
||||||
break
|
break
|
||||||
|
|
||||||
next_direction = random.choice(to_visit)
|
next_direction = random.choice(to_visit)
|
||||||
next_cell = self._cells[adjacent_cells[next_direction][0]][adjacent_cells[next_direction][1]]
|
next_cell = self._cells[adjacent_cells[next_direction]
|
||||||
|
[0]][adjacent_cells[next_direction][1]]
|
||||||
|
|
||||||
if next_direction is above:
|
if next_direction is above:
|
||||||
current_cell.configure_walls(top=False)
|
current_cell.configure_walls(top=False)
|
||||||
|
@ -122,28 +146,18 @@ class Maze:
|
||||||
next_cell.configure_walls(left=False)
|
next_cell.configure_walls(left=False)
|
||||||
|
|
||||||
current_cell.draw()
|
current_cell.draw()
|
||||||
next_cell.draw()
|
if self._window:
|
||||||
self._animate()
|
self._draw_cell(y=y, x=x)
|
||||||
|
|
||||||
self._break_walls_r(
|
self._break_walls_r(
|
||||||
adjacent_cells[next_direction][0],
|
y=adjacent_cells[next_direction][0],
|
||||||
adjacent_cells[next_direction][1],
|
x=adjacent_cells[next_direction][1],
|
||||||
)
|
)
|
||||||
|
|
||||||
def _draw_cells(self) -> None:
|
def _draw_cell(self, y: int, x: int) -> None:
|
||||||
"""
|
"""
|
||||||
draws all the cells on the maze with a short pause between each cell
|
_draw_cell draws the cells in an animated way.
|
||||||
for animation purposes.
|
|
||||||
"""
|
|
||||||
for i in range(self._num_cell_rows):
|
|
||||||
for j in range(self._num_cells_per_row):
|
|
||||||
self._cells[i][j].draw()
|
|
||||||
self._animate()
|
|
||||||
|
|
||||||
def _animate(self) -> None:
|
|
||||||
"""
|
|
||||||
redraws the application and pauses for a short period of time to
|
|
||||||
provide an animation effect.
|
|
||||||
"""
|
"""
|
||||||
|
self._cells[y][x].draw()
|
||||||
self._window.redraw()
|
self._window.redraw()
|
||||||
sleep(0.05)
|
sleep(0.05)
|
||||||
|
|
6
tests.py
6
tests.py
|
@ -32,6 +32,9 @@ class Tests(unittest.TestCase):
|
||||||
case["number_of_cells_per_row"],
|
case["number_of_cells_per_row"],
|
||||||
2,
|
2,
|
||||||
2,
|
2,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
True,
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
len(maze._cells),
|
len(maze._cells),
|
||||||
|
@ -56,6 +59,9 @@ class Tests(unittest.TestCase):
|
||||||
number_of_cells_per_row,
|
number_of_cells_per_row,
|
||||||
2,
|
2,
|
||||||
2,
|
2,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
True,
|
||||||
)
|
)
|
||||||
self.assertFalse(maze._cells[0][0].wall_exists(CellWallLabel.TOP))
|
self.assertFalse(maze._cells[0][0].wall_exists(CellWallLabel.TOP))
|
||||||
self.assertFalse(
|
self.assertFalse(
|
||||||
|
|
Loading…
Reference in a new issue