diff --git a/Cargo.toml b/Cargo.toml index 41e8fa6..5c335d0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,4 @@ edition = "2021" [dependencies] rand = "0.8.5" -termion = "1.5.6" \ No newline at end of file +termion = "1.5.6" diff --git a/src/lib/controller.rs b/src/lib/controller.rs index 4fcec69..5027590 100644 --- a/src/lib/controller.rs +++ b/src/lib/controller.rs @@ -1,6 +1,9 @@ +use rand::{distributions::Standard, prelude::Distribution}; + use super::grid::Grid; use std::{error::Error, fmt::Display}; +#[derive(Debug)] pub enum Move { LEFT, RIGHT, @@ -8,6 +11,17 @@ pub enum Move { DOWN, } +impl Distribution for Standard { + fn sample(&self, rng: &mut R) -> Move { + match rng.gen_range(0..4) { + 0 => Move::DOWN, + 1 => Move::LEFT, + 2 => Move::RIGHT, + _ => Move::UP, + } + } +} + #[derive(Debug)] pub enum ControllerError { ExitSignal, @@ -36,3 +50,5 @@ pub trait Controller { } pub mod player; +pub mod random; +pub mod simulated; diff --git a/src/lib/controller/random.rs b/src/lib/controller/random.rs new file mode 100644 index 0000000..cb68ba3 --- /dev/null +++ b/src/lib/controller/random.rs @@ -0,0 +1,13 @@ +use rand::random; + +use super::{Controller, ControllerError, Move}; +use crate::lib::grid::Grid; + +pub struct RandomController; + +impl Controller for RandomController { + fn next_move(&mut self, _grid: &Grid) -> Result { + let movement = random(); + Ok(movement) + } +}