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 std::str::FromStr;
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use labirust::{implementations::*, Executor};
|
use labirust::{implementations::*, Algorithm, Executor, SimpleGenerator};
|
||||||
|
|
||||||
enum Algorithms {
|
enum Algorithms {
|
||||||
DepthFirst,
|
DepthFirst,
|
||||||
|
@ -22,16 +22,37 @@ impl FromStr for Algorithms {
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
struct Parameters {
|
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,
|
width: usize,
|
||||||
|
|
||||||
|
/// Height of the maze to solve.
|
||||||
|
#[clap(short, default_value_t = 20)]
|
||||||
height: usize,
|
height: usize,
|
||||||
|
|
||||||
|
/// Delay between two simulation ticks.
|
||||||
|
#[clap(short, default_value_t = 100)]
|
||||||
delay: usize,
|
delay: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let params = Parameters::parse();
|
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);
|
let generator = Box::new(generator);
|
||||||
Self { generator }
|
Self { generator }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn new_dyn(generator: Box<dyn MazeGenerator + 'static>) -> Self {
|
||||||
|
Self { generator }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MazeState for Generated {}
|
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 {
|
pub struct DynExecutorBuilder {
|
||||||
maze: Option<Box<dyn BuildableMazeState>>,
|
maze: DynMazeState,
|
||||||
delay: Duration,
|
delay: Duration,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DynExecutorBuilder {
|
impl DynExecutorBuilder {
|
||||||
pub(crate) fn new() -> Self {
|
pub(crate) fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
maze: None,
|
maze: DynMazeState::None,
|
||||||
delay: Duration::from_millis(100),
|
delay: Duration::from_millis(100),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn maze(self, maze: Maze) -> Self {
|
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 {
|
pub fn generated(self, generator: Box<dyn MazeGenerator>) -> Self {
|
||||||
todo!()
|
let maze = DynMazeState::Generated(Generated::new_dyn(generator));
|
||||||
|
let Self { maze: _, delay } = self;
|
||||||
|
Self { delay, maze }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delay_ms(self, delay: u64) -> Self {
|
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) {
|
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;
|
let delay = self.delay;
|
||||||
(maze, delay)
|
(maze, delay)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue