use super::Algorithm;
use crate::maze::grid::{Grid, cell::Cell};
use crate::utils::types::Coords;
use rand::prelude::*;
pub struct RecursiveBacktracking;
impl Algorithm for RecursiveBacktracking {
fn generate(&mut self, grid: &mut Grid, rng: &mut StdRng) {
let start_coords = (0, 0);
carve_passages_from(start_coords, grid, rng);
}
}
fn carve_passages_from<R: Rng>(coords: Coords, grid: &mut Grid, rng: &mut R) {
let mut dirs = [Cell::NORTH, Cell::SOUTH, Cell::WEST, Cell::EAST];
dirs.shuffle(rng);
for dir in dirs {
let next = match grid.get_next_cell_coords(coords, dir) {
Ok(next) => next,
Err(_) => continue,
};
if grid.is_cell_visited(next) {
continue;
}
if let Ok(next) = grid.carve_passage(coords, dir) {
carve_passages_from(next, grid, rng);
}
}
}