added better builder for executor
This commit is contained in:
parent
ee59d342a1
commit
a7ce6ffb56
4 changed files with 93 additions and 8 deletions
|
@ -6,6 +6,8 @@ use std::{
|
||||||
|
|
||||||
use crate::{Algorithm, Maze, Pos};
|
use crate::{Algorithm, Maze, Pos};
|
||||||
|
|
||||||
|
use self::builder::{new_builder, BuildableMazeState, ExecutorBuilder, Unprovided};
|
||||||
|
|
||||||
/// A guess to pass to the current [`Executor`] at the end of every `progress` call.
|
/// A guess to pass to the current [`Executor`] at the end of every `progress` call.
|
||||||
pub struct Guess(Vec<Pos>);
|
pub struct Guess(Vec<Pos>);
|
||||||
|
|
||||||
|
@ -79,11 +81,14 @@ impl<'m> Context<'m> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mod builder;
|
||||||
|
|
||||||
/// A structure holding a [`Maze`] and iteratively solving it with a provided [`Algorithm`].
|
/// A structure holding a [`Maze`] and iteratively solving it with a provided [`Algorithm`].
|
||||||
pub struct Executor<Algo>
|
pub struct Executor<Algo>
|
||||||
where
|
where
|
||||||
Algo: Algorithm,
|
Algo: Algorithm,
|
||||||
{
|
{
|
||||||
|
delay: Duration,
|
||||||
maze: Maze,
|
maze: Maze,
|
||||||
algorithm: Algo,
|
algorithm: Algo,
|
||||||
}
|
}
|
||||||
|
@ -93,13 +98,32 @@ where
|
||||||
A: Algorithm,
|
A: Algorithm,
|
||||||
{
|
{
|
||||||
/// Constructor.
|
/// Constructor.
|
||||||
pub fn new(maze: Maze, algorithm: A) -> Self {
|
fn new(maze: Maze, algorithm: A, delay: Duration) -> Self {
|
||||||
Self { maze, algorithm }
|
Self {
|
||||||
|
maze,
|
||||||
|
algorithm,
|
||||||
|
delay,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn build<'f, F, MS>(algorithm: A, builder: F) -> Self
|
||||||
|
where
|
||||||
|
MS: BuildableMazeState,
|
||||||
|
F: Fn(ExecutorBuilder<Unprovided>) -> ExecutorBuilder<MS>,
|
||||||
|
{
|
||||||
|
let operation = builder;
|
||||||
|
let mut builder = (operation)(new_builder());
|
||||||
|
let (maze, delay) = builder.build();
|
||||||
|
Self::new(maze, algorithm, delay)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Submit the maze to the [`Algorithm`] and iteratively progress through the maze driven by said algorithm.
|
/// Submit the maze to the [`Algorithm`] and iteratively progress through the maze driven by said algorithm.
|
||||||
pub fn run(&mut self) {
|
pub fn run(&mut self) {
|
||||||
let Self { maze, algorithm } = self;
|
let Self {
|
||||||
|
maze,
|
||||||
|
algorithm,
|
||||||
|
delay,
|
||||||
|
} = self;
|
||||||
let mut insight = Insight::from_position(maze.start(), &maze);
|
let mut insight = Insight::from_position(maze.start(), &maze);
|
||||||
let mut tick = 0;
|
let mut tick = 0;
|
||||||
let mut tried = HashSet::new();
|
let mut tried = HashSet::new();
|
||||||
|
@ -116,7 +140,7 @@ where
|
||||||
|
|
||||||
// draw
|
// draw
|
||||||
Self::draw(maze, &tried, tick, &guess);
|
Self::draw(maze, &tried, tick, &guess);
|
||||||
thread::sleep(Duration::from_millis(100));
|
thread::sleep(*delay);
|
||||||
tick += 1;
|
tick += 1;
|
||||||
|
|
||||||
// check for next iteration
|
// check for next iteration
|
||||||
|
|
57
src/executor/builder.rs
Normal file
57
src/executor/builder.rs
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use crate::{labyrinth::generator::MazeGenerator, Maze};
|
||||||
|
|
||||||
|
pub trait MazeState {}
|
||||||
|
pub trait BuildableMazeState: MazeState {
|
||||||
|
fn get(self) -> Maze;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Unprovided;
|
||||||
|
|
||||||
|
impl MazeState for Unprovided {}
|
||||||
|
|
||||||
|
struct Provided {
|
||||||
|
maze: Maze,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MazeState for Provided {}
|
||||||
|
impl BuildableMazeState for Provided {
|
||||||
|
fn get(self) -> Maze {
|
||||||
|
self.maze
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Generated {
|
||||||
|
generator: Box<dyn MazeGenerator>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MazeState for Generated {}
|
||||||
|
impl BuildableMazeState for Generated {
|
||||||
|
fn get(mut self) -> Maze {
|
||||||
|
self.generator.generate()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ExecutorBuilder<MS>
|
||||||
|
where
|
||||||
|
MS: MazeState,
|
||||||
|
{
|
||||||
|
maze_state: MS,
|
||||||
|
delay: Duration,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_builder() -> ExecutorBuilder<Unprovided> {
|
||||||
|
ExecutorBuilder {
|
||||||
|
maze_state: Unprovided,
|
||||||
|
delay: Duration::from_millis(100),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<MS: BuildableMazeState> ExecutorBuilder<MS> {
|
||||||
|
pub fn build(self) -> (Maze, Duration) {
|
||||||
|
let maze = self.maze_state.get();
|
||||||
|
let delay = self.delay;
|
||||||
|
(maze, delay)
|
||||||
|
}
|
||||||
|
}
|
|
@ -15,8 +15,8 @@ fn depth_first() {
|
||||||
use crate::{generate, Executor};
|
use crate::{generate, Executor};
|
||||||
let algorithm = DepthFirst::new();
|
let algorithm = DepthFirst::new();
|
||||||
let maze = generate(20, 20);
|
let maze = generate(20, 20);
|
||||||
let mut executor = Executor::new(maze, algorithm);
|
// let mut executor = Executor::new(maze, algorithm);
|
||||||
executor.run();
|
// executor.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -24,6 +24,6 @@ fn breath_first() {
|
||||||
use crate::{generate, Executor};
|
use crate::{generate, Executor};
|
||||||
let algorithm = BreathFirst::new();
|
let algorithm = BreathFirst::new();
|
||||||
let maze = generate(20, 20);
|
let maze = generate(20, 20);
|
||||||
let mut executor = Executor::new(maze, algorithm);
|
// let mut executor = Executor::new(maze, algorithm);
|
||||||
executor.run();
|
// executor.run();
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,3 +44,7 @@ fn generation() {
|
||||||
let text = maze.display(None);
|
let text = maze.display(None);
|
||||||
println!("{text}");
|
println!("{text}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait MazeGenerator {
|
||||||
|
fn generate(&mut self) -> Maze;
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue