use rand::rngs::StdRng;
use rand::SeedableRng;
use crate::maze::algorithms::{Algorithm, RecursiveBacktracking};
use crate::maze::OrthogonalMaze;
pub struct OrthogonalMazeBuilder {
width: usize,
height: usize,
algorithm: Box<dyn Algorithm>,
seed: Option<u64>,
}
impl OrthogonalMazeBuilder {
pub fn new() -> Self {
OrthogonalMazeBuilder {
width: 10,
height: 10,
algorithm: Box::new(RecursiveBacktracking),
seed: None,
}
}
pub const fn width(mut self, width: usize) -> Self {
self.width = width;
self
}
pub const fn height(mut self, height: usize) -> Self {
self.height = height;
self
}
pub fn algorithm(mut self, algorithm: Box<dyn Algorithm>) -> Self {
self.algorithm = algorithm;
self
}
pub const fn seed(mut self, seed: Option<u64>) -> Self {
self.seed = seed;
self
}
pub fn build(mut self) -> OrthogonalMaze {
let mut maze = OrthogonalMaze::new(self.width, self.height);
let mut rng = match self.seed {
Some(val) => StdRng::seed_from_u64(val),
None => StdRng::from_os_rng(),
};
self.algorithm.generate(maze.get_grid_mut(), &mut rng);
maze
}
}
impl Default for OrthogonalMazeBuilder {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn build() {
let maze = OrthogonalMazeBuilder::default().build();
assert!(maze.is_valid());
}
}