fix: add randomisation to original DFS algorithm
Update the DFS algorithm to allow the user to select if the solver should randomly choose the directions when traversing through the maze. The duplicate method has been removed.
This commit is contained in:
parent
b4d2e1161b
commit
fa75e61acf
1 changed files with 12 additions and 50 deletions
60
solver.py
60
solver.py
|
@ -49,6 +49,7 @@ class Solver:
|
||||||
self,
|
self,
|
||||||
current_position: MazePosition,
|
current_position: MazePosition,
|
||||||
end_position: MazePosition,
|
end_position: MazePosition,
|
||||||
|
enable_random_direction: bool = False,
|
||||||
) -> bool:
|
) -> bool:
|
||||||
if current_position == end_position:
|
if current_position == end_position:
|
||||||
return True
|
return True
|
||||||
|
@ -59,54 +60,15 @@ class Solver:
|
||||||
visitor=self._solver,
|
visitor=self._solver,
|
||||||
)
|
)
|
||||||
|
|
||||||
for direction in MazeDirection:
|
if enable_random_direction:
|
||||||
adjacent_position = current_position.get_adjacent_position(
|
|
||||||
direction,
|
|
||||||
)
|
|
||||||
|
|
||||||
if adjacent_position is None:
|
|
||||||
continue
|
|
||||||
if self._game.cell_was_visited_by(
|
|
||||||
i=adjacent_position.i,
|
|
||||||
j=adjacent_position.j,
|
|
||||||
visitor=self._solver,
|
|
||||||
) or self._game.cell_wall_exists(
|
|
||||||
i=adjacent_position.i,
|
|
||||||
j=adjacent_position.j,
|
|
||||||
wall=self._wall_map[direction],
|
|
||||||
):
|
|
||||||
continue
|
|
||||||
|
|
||||||
self._game.draw_path_between(current_position, adjacent_position)
|
|
||||||
solved = self.solve_with_dfs_r(adjacent_position, end_position)
|
|
||||||
if solved:
|
|
||||||
return True
|
|
||||||
self._game.draw_path_between(
|
|
||||||
current_position, adjacent_position, undo=True)
|
|
||||||
|
|
||||||
return False
|
|
||||||
|
|
||||||
def solve_with_randomised_dfs_r(
|
|
||||||
self,
|
|
||||||
current_position: MazePosition,
|
|
||||||
end_position: MazePosition,
|
|
||||||
) -> bool:
|
|
||||||
if current_position == end_position:
|
|
||||||
return True
|
|
||||||
|
|
||||||
self._game.mark_cell_as_visited(
|
|
||||||
i=current_position.i,
|
|
||||||
j=current_position.j,
|
|
||||||
visitor=self._solver,
|
|
||||||
)
|
|
||||||
|
|
||||||
random.seed()
|
random.seed()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
possible_directions: List[MazeDirection] = []
|
possible_directions: List[MazeDirection] = []
|
||||||
for direction in MazeDirection:
|
for direction in MazeDirection:
|
||||||
adjacent_position = current_position.get_adjacent_position(
|
adjacent_position = current_position.get_adjacent_position(
|
||||||
direction)
|
direction,
|
||||||
|
)
|
||||||
if adjacent_position is None:
|
if adjacent_position is None:
|
||||||
continue
|
continue
|
||||||
if self._game.cell_was_visited_by(
|
if self._game.cell_was_visited_by(
|
||||||
|
@ -122,16 +84,16 @@ class Solver:
|
||||||
possible_directions.append(direction)
|
possible_directions.append(direction)
|
||||||
if len(possible_directions) == 0:
|
if len(possible_directions) == 0:
|
||||||
break
|
break
|
||||||
chosen_direction = None
|
next_direction = None
|
||||||
if len(possible_directions) == 1:
|
if len(possible_directions) == 1 or not enable_random_direction:
|
||||||
chosen_direction = possible_directions[0]
|
next_direction = possible_directions[0]
|
||||||
else:
|
else:
|
||||||
chosen_direction = random.choice(possible_directions)
|
next_direction = random.choice(possible_directions)
|
||||||
next_position = current_position.get_adjacent_position(
|
next_position = current_position.get_adjacent_position(
|
||||||
chosen_direction,
|
next_direction,
|
||||||
)
|
)
|
||||||
self._game.draw_path_between(current_position, next_position)
|
self._game.draw_path_between(current_position, next_position)
|
||||||
solved = self.solve_with_randomised_dfs_r(
|
solved = self.solve_with_dfs_r(
|
||||||
next_position, end_position)
|
next_position, end_position)
|
||||||
if solved:
|
if solved:
|
||||||
return True
|
return True
|
||||||
|
@ -147,7 +109,7 @@ class Solver:
|
||||||
self,
|
self,
|
||||||
current_position: MazePosition,
|
current_position: MazePosition,
|
||||||
end_position: MazePosition,
|
end_position: MazePosition,
|
||||||
enable_random_direction: False,
|
enable_random_direction: bool = False,
|
||||||
) -> bool:
|
) -> bool:
|
||||||
self._game.mark_cell_as_visited(
|
self._game.mark_cell_as_visited(
|
||||||
i=current_position.i,
|
i=current_position.i,
|
||||||
|
|
Loading…
Reference in a new issue