1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
use std::fmt;
use rand;
use rand::distributions::{IndependentSample, Range};
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)]
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, Serialize, Deserialize)]
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<Point> {
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))
}
}
}
|