use super::Algorithm;
use crate::utils::types::Coords;
use crate::maze::grid::{Grid, cell::Cell};
use rand::prelude::*;
pub struct AldousBroder;
impl AldousBroder {}
impl Algorithm for AldousBroder {
fn generate(&mut self, grid: &mut Grid, rng: &mut StdRng) {
let start_coords = get_start_coords(grid, rng);
let mut x = start_coords.0;
let mut y = start_coords.1;
let mut remaining = grid.width() * grid.height() - 1;
while remaining > 0 {
let mut directions = [Cell::NORTH, Cell::SOUTH, Cell::WEST, Cell::EAST];
directions.shuffle(rng);
for dir in directions {
let next_cell = grid.get_next_cell_coords((x, y), dir);
if next_cell.is_err() {
continue;
}
let (nx, ny) = next_cell.unwrap();
if !grid.is_cell_visited((nx, ny)) {
grid.carve_passage((x, y), dir).unwrap();
remaining -= 1;
}
x = nx;
y = ny;
break;
}
}
}
}
fn get_start_coords<R: Rng>(grid: &Grid, rng: &mut R) -> Coords {
let y = rng.random_range(0..grid.height());
let x = rng.random_range(0..grid.width());
(x, y)
}