diff --git a/Cargo.toml b/Cargo.toml index 863e846..a264f32 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,4 @@ edition = "2018" [dependencies] clap = "2.33.3" +rand = "0.7.3" diff --git a/src/robot.rs b/src/robot.rs index ad6552f..c81452e 100644 --- a/src/robot.rs +++ b/src/robot.rs @@ -1,5 +1,10 @@ /// A Robot *aka droid* is represented here. /// Each robot must have a unique id. +use rand::{ + distributions::{Distribution, Standard}, + Rng, +}; + pub struct Robot { pub id: u32, pub o: Orientation, @@ -42,6 +47,7 @@ impl Robot { } /// Enum to store all possible orientations. +#[derive(Debug)] pub enum Orientation { N, E, @@ -49,6 +55,17 @@ pub enum Orientation { W, } +impl Distribution for Standard { + fn sample(&self, rng: &mut R) -> Orientation { + match rng.gen_range(0, 3) { + 0 => Orientation::N, + 1 => Orientation::E, + 2 => Orientation::S, + _ => Orientation::W, + } + } +} + /// Struct to store robot position. #[derive(PartialEq, Eq, Hash)] pub struct Position { @@ -101,6 +118,17 @@ pub fn print_robots(robot_pool: &Vec) { #[cfg(test)] mod tests { use super::*; + use std::any::type_name; + + fn type_of(_: T) -> &'static str { + type_name::() + } + + #[test] + fn test_rand_orientation() { + let o: Orientation = rand::random(); + assert_eq!(type_of(o), "dancing_droid::robot::Orientation"); + } #[test] fn test_new_robot() {