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:
parent
32696f311d
commit
5be7f5041a
2 changed files with 28 additions and 20 deletions
34
maze.py
34
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:
|
||||
"""
|
||||
|
|
14
tests.py
14
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(
|
||||
|
|
Loading…
Reference in a new issue