Merge branch 'rand-orintation' into 'master'
generate random Orientation enum See merge request mhart/DancingDroids!47
This commit is contained in:
commit
dc98eddcf6
@ -8,3 +8,4 @@ edition = "2018"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = "2.33.3"
|
clap = "2.33.3"
|
||||||
|
rand = "0.7.3"
|
||||||
|
28
src/robot.rs
28
src/robot.rs
@ -1,5 +1,10 @@
|
|||||||
/// A Robot *aka droid* is represented here.
|
/// A Robot *aka droid* is represented here.
|
||||||
/// Each robot must have a unique id.
|
/// Each robot must have a unique id.
|
||||||
|
use rand::{
|
||||||
|
distributions::{Distribution, Standard},
|
||||||
|
Rng,
|
||||||
|
};
|
||||||
|
|
||||||
pub struct Robot {
|
pub struct Robot {
|
||||||
pub id: u32,
|
pub id: u32,
|
||||||
pub o: Orientation,
|
pub o: Orientation,
|
||||||
@ -42,6 +47,7 @@ impl Robot {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Enum to store all possible orientations.
|
/// Enum to store all possible orientations.
|
||||||
|
#[derive(Debug)]
|
||||||
pub enum Orientation {
|
pub enum Orientation {
|
||||||
N,
|
N,
|
||||||
E,
|
E,
|
||||||
@ -49,6 +55,17 @@ pub enum Orientation {
|
|||||||
W,
|
W,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Distribution<Orientation> for Standard {
|
||||||
|
fn sample<R: Rng + ?Sized>(&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.
|
/// Struct to store robot position.
|
||||||
#[derive(PartialEq, Eq, Hash)]
|
#[derive(PartialEq, Eq, Hash)]
|
||||||
pub struct Position {
|
pub struct Position {
|
||||||
@ -101,6 +118,17 @@ pub fn print_robots(robot_pool: &Vec<Robot>) {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use std::any::type_name;
|
||||||
|
|
||||||
|
fn type_of<T>(_: T) -> &'static str {
|
||||||
|
type_name::<T>()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_rand_orientation() {
|
||||||
|
let o: Orientation = rand::random();
|
||||||
|
assert_eq!(type_of(o), "dancing_droid::robot::Orientation");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_new_robot() {
|
fn test_new_robot() {
|
||||||
|
Loading…
Reference in New Issue
Block a user