implemented random controller
This commit is contained in:
parent
3f2058e332
commit
4c821be1fe
3 changed files with 30 additions and 1 deletions
|
@ -7,4 +7,4 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
rand = "0.8.5"
|
||||
termion = "1.5.6"
|
||||
termion = "1.5.6"
|
||||
|
|
|
@ -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<Move> for Standard {
|
||||
fn sample<R: rand::Rng + ?Sized>(&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;
|
||||
|
|
13
src/lib/controller/random.rs
Normal file
13
src/lib/controller/random.rs
Normal file
|
@ -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<Move, ControllerError> {
|
||||
let movement = random();
|
||||
Ok(movement)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue