From 5be7f5041a62be0b0f39a62a226a57ecdc4654d7 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Thu, 15 Feb 2024 16:36:18 +0000 Subject: [PATCH] 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. --- maze.py | 34 ++++++++++++++++++++-------------- tests.py | 14 ++++++++------ 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/maze.py b/maze.py index 4bfc7cc..301defe 100644 --- a/maze.py +++ b/maze.py @@ -89,7 +89,6 @@ class Maze: cell_size_y: int, window: Window = None, seed=None, - test=False, ) -> None: self._x_position = x_position self._y_position = y_position @@ -108,8 +107,21 @@ class Maze: self._create_cell_grid() self._open_entrance_and_exit() - if not test: - self._break_walls_r(0, 0) + start_position = MazePosition( + 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: """ @@ -165,19 +177,13 @@ class Maze: 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 cells and randomly knocking down the walls to create the maze's paths. """ - current_position = MazePosition( - 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 = self._cells[current_position.i][current_position.j] current_cell.visited_by_maze_generator = True while True: @@ -195,7 +201,7 @@ class Maze: if len(possible_directions) == 0: if self._window: - self._draw_cell(i=i, j=j) + self._draw_cell(i=current_position.i, j=current_position.j) break chosen_direction = random.choice(possible_directions) @@ -217,9 +223,9 @@ class Maze: next_cell.configure_walls(left=False) 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: """ diff --git a/tests.py b/tests.py index 5710a2f..2a28035 100644 --- a/tests.py +++ b/tests.py @@ -5,18 +5,22 @@ import errors class Tests(unittest.TestCase): + """ + Tests runs the test suite. + """ + def test_maze_create_cell_grid(self): """ test_maze_create_cell_grid tests that the maze is constructed properly. """ cases = [ { - "number_of_cell_rows": 12, - "number_of_cells_per_row": 10, + "number_of_cell_rows": 6, + "number_of_cells_per_row": 9, }, { - "number_of_cell_rows": 50, - "number_of_cells_per_row": 120, + "number_of_cell_rows": 3, + "number_of_cells_per_row": 12, }, { "number_of_cell_rows": 4, @@ -34,7 +38,6 @@ class Tests(unittest.TestCase): 2, None, None, - True, ) self.assertEqual( len(m._cells), @@ -61,7 +64,6 @@ class Tests(unittest.TestCase): 2, None, None, - True, ) self.assertFalse(m._cells[0][0].wall_exists(CellWallLabel.TOP)) self.assertFalse(