use std::fmt; use rand; use rand::distributions::{IndependentSample, Range}; #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] pub enum Direction { North, East, South, West, } use Direction::*; impl fmt::Display for Direction { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.write_str( match self { &North => "North", &East => "East", &South => "South", &West => "West" } ) } } impl Direction { pub fn random() -> Direction { let mut rng = rand::thread_rng(); let between = Range::new(0, 4); let dir = between.ind_sample(&mut rng); match dir { 0 => North, 1 => East, 2 => South, 3 => West, _ => panic!("Invalid number generated by random number generator") } } } #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] pub struct Point { pub x: u16, pub y: u16 } impl Point { pub fn new(x: u16, y: u16) -> Point { Point { x: x, y: y } } pub fn random(map_size: u16) -> Point { let mut rng = rand::thread_rng(); let between = Range::new(0, map_size); let x = between.ind_sample(&mut rng); let y = between.ind_sample(&mut rng); Point::new(x, y) } pub fn move_point(&self, direction: Direction, distance: u16, map_size: u16) -> Option { let x = self.x; let y = self.y; match direction { South if y < distance => None, West if x < distance => None, North if y + distance >= map_size => None, East if x + distance >= map_size => None, South => Some(Point::new(x, y-distance)), West => Some(Point::new(x-distance, y)), North => Some(Point::new(x, y+distance)), East => Some(Point::new(x+distance, y)) } } }