finished to implement the dynamic builder
This commit is contained in:
parent
c260404065
commit
e8102f0e72
2 changed files with 58 additions and 11 deletions
|
@ -1,7 +1,7 @@
|
|||
use std::str::FromStr;
|
||||
|
||||
use clap::Parser;
|
||||
use labirust::{implementations::*, Executor};
|
||||
use labirust::{implementations::*, Algorithm, Executor, SimpleGenerator};
|
||||
|
||||
enum Algorithms {
|
||||
DepthFirst,
|
||||
|
@ -22,16 +22,37 @@ impl FromStr for Algorithms {
|
|||
|
||||
#[derive(Parser)]
|
||||
struct Parameters {
|
||||
algorithm_kind: Algorithms,
|
||||
/// Algorithm to use in the simulation.
|
||||
/// One of: "depth-first", "breath-first"
|
||||
algorithm: Algorithms,
|
||||
|
||||
/// Width of the maze to solve.
|
||||
#[clap(short, default_value_t = 40)]
|
||||
width: usize,
|
||||
|
||||
/// Height of the maze to solve.
|
||||
#[clap(short, default_value_t = 20)]
|
||||
height: usize,
|
||||
|
||||
/// Delay between two simulation ticks.
|
||||
#[clap(short, default_value_t = 100)]
|
||||
delay: usize,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let params = Parameters::parse();
|
||||
|
||||
let executor = todo!();
|
||||
let algorithm: Box<dyn Algorithm> = match params.algorithm {
|
||||
Algorithms::DepthFirst => Box::new(DepthFirst::new()),
|
||||
Algorithms::BreathFirst => Box::new(BreathFirst::new()),
|
||||
};
|
||||
|
||||
println!("Hello, world!");
|
||||
let mut executor = Executor::build_dyn(algorithm, |b| {
|
||||
b.generated(Box::new(SimpleGenerator::new(
|
||||
params.width as isize,
|
||||
params.height as isize,
|
||||
)))
|
||||
});
|
||||
|
||||
executor.run();
|
||||
}
|
||||
|
|
|
@ -47,6 +47,10 @@ pub mod maze_state {
|
|||
let generator = Box::new(generator);
|
||||
Self { generator }
|
||||
}
|
||||
|
||||
pub fn new_dyn(generator: Box<dyn MazeGenerator + 'static>) -> Self {
|
||||
Self { generator }
|
||||
}
|
||||
}
|
||||
|
||||
impl MazeState for Generated {}
|
||||
|
@ -120,33 +124,55 @@ impl<MS: BuildableMazeState> ExecutorBuilder<MS> {
|
|||
}
|
||||
}
|
||||
|
||||
pub enum DynMazeState {
|
||||
None,
|
||||
Provided(Provided),
|
||||
Generated(Generated),
|
||||
}
|
||||
|
||||
impl DynMazeState {
|
||||
pub fn get(self) -> Option<Maze> {
|
||||
match self {
|
||||
DynMazeState::None => None,
|
||||
DynMazeState::Provided(provided) => Some(provided.get()),
|
||||
DynMazeState::Generated(generated) => Some(generated.get()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DynExecutorBuilder {
|
||||
maze: Option<Box<dyn BuildableMazeState>>,
|
||||
maze: DynMazeState,
|
||||
delay: Duration,
|
||||
}
|
||||
|
||||
impl DynExecutorBuilder {
|
||||
pub(crate) fn new() -> Self {
|
||||
Self {
|
||||
maze: None,
|
||||
maze: DynMazeState::None,
|
||||
delay: Duration::from_millis(100),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn maze(self, maze: Maze) -> Self {
|
||||
todo!()
|
||||
let maze = DynMazeState::Provided(Provided::new(maze));
|
||||
let Self { maze: _, delay } = self;
|
||||
Self { maze, delay }
|
||||
}
|
||||
|
||||
pub fn generated(self, maze: Box<dyn MazeGenerator>) -> Self {
|
||||
todo!()
|
||||
pub fn generated(self, generator: Box<dyn MazeGenerator>) -> Self {
|
||||
let maze = DynMazeState::Generated(Generated::new_dyn(generator));
|
||||
let Self { maze: _, delay } = self;
|
||||
Self { delay, maze }
|
||||
}
|
||||
|
||||
pub fn delay_ms(self, delay: u64) -> Self {
|
||||
todo!()
|
||||
let delay = Duration::from_millis(delay);
|
||||
let Self { maze, delay: _ } = self;
|
||||
Self { maze, delay }
|
||||
}
|
||||
|
||||
pub(crate) fn build(self) -> (Maze, Duration) {
|
||||
let maze = self.maze.expect("no buildable maze provided").get();
|
||||
let maze = self.maze.get().expect("no buildable maze provided");
|
||||
let delay = self.delay;
|
||||
(maze, delay)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue