implemented random controller

This commit is contained in:
JOLIMAITRE Matthieu 2022-04-04 17:13:37 +03:00
parent 3f2058e332
commit 4c821be1fe
3 changed files with 30 additions and 1 deletions

View file

@ -7,4 +7,4 @@ edition = "2021"
[dependencies] [dependencies]
rand = "0.8.5" rand = "0.8.5"
termion = "1.5.6" termion = "1.5.6"

View file

@ -1,6 +1,9 @@
use rand::{distributions::Standard, prelude::Distribution};
use super::grid::Grid; use super::grid::Grid;
use std::{error::Error, fmt::Display}; use std::{error::Error, fmt::Display};
#[derive(Debug)]
pub enum Move { pub enum Move {
LEFT, LEFT,
RIGHT, RIGHT,
@ -8,6 +11,17 @@ pub enum Move {
DOWN, 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)] #[derive(Debug)]
pub enum ControllerError { pub enum ControllerError {
ExitSignal, ExitSignal,
@ -36,3 +50,5 @@ pub trait Controller {
} }
pub mod player; pub mod player;
pub mod random;
pub mod simulated;

View 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)
}
}