mirror of
https://github.com/kristoferssolo/Qualification-Thesis.git
synced 2025-10-21 20:10:37 +00:00
36 lines
1.1 KiB
Rust
36 lines
1.1 KiB
Rust
pub fn generate_backtracking(
|
|
maze: &mut HexMaze,
|
|
start_pos: Option<Hex>,
|
|
seed: Option<u64>,
|
|
) {
|
|
if maze.is_empty() {
|
|
return;
|
|
}
|
|
let start = start_pos.unwrap_or(Hex::ZERO);
|
|
let mut visited = HashSet::new();
|
|
let mut rng: Box<dyn RngCore> = seed.map_or_else(
|
|
|| Box::new(thread_rng()) as Box<dyn RngCore>,
|
|
|seed| Box::new(ChaCha8Rng::seed_from_u64(seed)) as Box<dyn RngCore>,
|
|
);
|
|
recursive_backtrack(maze, start, &mut visited, &mut rng);
|
|
}
|
|
|
|
fn recursive_backtrack<R: Rng>(
|
|
maze: &mut HexMaze,
|
|
current: Hex,
|
|
visited: &mut HashSet<Hex>,
|
|
rng: &mut R,
|
|
) {
|
|
visited.insert(current);
|
|
let mut directions = EdgeDirection::ALL_DIRECTIONS;
|
|
directions.shuffle(rng);
|
|
for direction in directions {
|
|
let neighbor = current + direction;
|
|
if maze.get_tile(&neighbor).is_some() && !visited.contains(&neighbor) {
|
|
maze.remove_tile_wall(¤t, direction);
|
|
maze.remove_tile_wall(&neighbor, direction.const_neg());
|
|
recursive_backtrack(maze, neighbor, visited, rng);
|
|
}
|
|
}
|
|
}
|