refactor: use MazePosition as an argument

- Use MazePosition as the argument for the _break_wall_r method
- Generate small mazes for tests to avoid RecursionError exceptions.
This commit is contained in:
Dan Anglin 2024-02-15 16:36:18 +00:00
parent 32696f311d
commit 5be7f5041a
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
2 changed files with 28 additions and 20 deletions

34
maze.py
View file

@ -89,7 +89,6 @@ 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
@ -108,8 +107,21 @@ class Maze:
self._create_cell_grid() self._create_cell_grid()
self._open_entrance_and_exit() self._open_entrance_and_exit()
if not test: start_position = MazePosition(
self._break_walls_r(0, 0) i=0,
j=0,
max_i=self._num_cell_rows-1,
max_j=self._num_cells_per_row-1,
)
end_position = MazePosition(
i=self._num_cell_rows-1,
j=self._num_cells_per_row-1,
max_i=self._num_cell_rows-1,
max_j=self._num_cells_per_row-1,
)
self._break_walls_r(start_position)
def _create_cell_grid(self) -> None: def _create_cell_grid(self) -> None:
""" """
@ -165,19 +177,13 @@ class Maze:
j=self._num_cells_per_row-1 j=self._num_cells_per_row-1
) )
def _break_walls_r(self, i: int, j: int) -> None: def _break_walls_r(self, current_position: MazePosition) -> None:
""" """
_break_walls_r generates a random maze by traversing through the _break_walls_r generates a random maze by traversing through the
cells and randomly knocking down the walls to create the maze's paths. cells and randomly knocking down the walls to create the maze's paths.
""" """
current_position = MazePosition( current_cell = self._cells[current_position.i][current_position.j]
i=i,
j=j,
max_i=self._num_cell_rows-1,
max_j=self._num_cells_per_row-1
)
current_cell = self._cells[i][j]
current_cell.visited_by_maze_generator = True current_cell.visited_by_maze_generator = True
while True: while True:
@ -195,7 +201,7 @@ class Maze:
if len(possible_directions) == 0: if len(possible_directions) == 0:
if self._window: if self._window:
self._draw_cell(i=i, j=j) self._draw_cell(i=current_position.i, j=current_position.j)
break break
chosen_direction = random.choice(possible_directions) chosen_direction = random.choice(possible_directions)
@ -217,9 +223,9 @@ class Maze:
next_cell.configure_walls(left=False) next_cell.configure_walls(left=False)
if self._window: if self._window:
self._draw_cell(i=i, j=j) self._draw_cell(i=current_position.i, j=current_position.j)
self._break_walls_r(i=next_position.i, j=next_position.j) self._break_walls_r(next_position)
def _draw_cell(self, i: int, j: int) -> None: def _draw_cell(self, i: int, j: int) -> None:
""" """

View file

@ -5,18 +5,22 @@ import errors
class Tests(unittest.TestCase): class Tests(unittest.TestCase):
"""
Tests runs the test suite.
"""
def test_maze_create_cell_grid(self): def test_maze_create_cell_grid(self):
""" """
test_maze_create_cell_grid tests that the maze is constructed properly. test_maze_create_cell_grid tests that the maze is constructed properly.
""" """
cases = [ cases = [
{ {
"number_of_cell_rows": 12, "number_of_cell_rows": 6,
"number_of_cells_per_row": 10, "number_of_cells_per_row": 9,
}, },
{ {
"number_of_cell_rows": 50, "number_of_cell_rows": 3,
"number_of_cells_per_row": 120, "number_of_cells_per_row": 12,
}, },
{ {
"number_of_cell_rows": 4, "number_of_cell_rows": 4,
@ -34,7 +38,6 @@ class Tests(unittest.TestCase):
2, 2,
None, None,
None, None,
True,
) )
self.assertEqual( self.assertEqual(
len(m._cells), len(m._cells),
@ -61,7 +64,6 @@ class Tests(unittest.TestCase):
2, 2,
None, None,
None, None,
True,
) )
self.assertFalse(m._cells[0][0].wall_exists(CellWallLabel.TOP)) self.assertFalse(m._cells[0][0].wall_exists(CellWallLabel.TOP))
self.assertFalse( self.assertFalse(