summaryrefslogtreecommitdiff
path: root/src/math.rs
blob: a93d03e5372a85b54ce6329649052cdb782c81e4 (plain)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
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")
        }
    }
}

#[cfg(test)]
mod direction_tests {
    use super::*;

    #[test]
    fn random_direction_does_not_panic() {
        for _ in 0..10000 {
            Direction::random();
        }
    }
}

#[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: i32, map_size: u16) -> Option<Point> {
        let x = self.x as i32 + match direction {
            West => -distance,
            East => distance,
            _ => 0
        };
        let y = self.y as i32 + match direction {
            South =>  -distance,
            North => distance,
            _ => 0
        };
        let max = map_size as i32;

        if x >= max || y >= max || x < 0 || y < 0 {
            None
        }
        else {
            Some(Point::new(x as u16, y as u16))
        }
    }

    pub fn move_point_no_bounds_check(&self, direction: Direction, distance: i32) -> Point {
        let x = self.x as i32 + match direction {
            West => -distance,
            East => distance,
            _ => 0
        };
        let y = self.y as i32 + match direction {
            South =>  -distance,
            North => distance,
            _ => 0
        };
        
        Point::new(x as u16, y as u16)
    }

    pub fn check_for_ship_collision(&self, ship_start: Point, direction: Direction, length: u16) -> bool {
        let reverse = match direction {
            West | South => true,
            East | North => false
        };
        
        let same_cross = match direction {
            East | West => self.y == ship_start.y,
            North | South => self.x == ship_start.x
        };

        let (parr_self, parr_ship) = match direction {
            East | West => (self.x, ship_start.x),
            North | South => (self.y, ship_start.y)
        };

        let corrected_parr_ship = match reverse {
            true => 1 + parr_ship - length,
            false => parr_ship
        };

        same_cross && parr_self >= corrected_parr_ship && parr_self < corrected_parr_ship + length
    }

    pub fn is_adjacent(&self, other: Point) -> bool {
        let dx = if self.x > other.x {self.x - other.x} else {other.x - self.x};
        let dy = if self.y > other.y {self.y - other.y} else {other.y - self.y};

        (dx == 0 && dy == 1) ||
            (dx == 1 && dy == 0)
        
    }

    pub fn is_on_lattice(&self, lattice_size: u16) -> bool {
        (self.x + self.y) % lattice_size == 0
    }
}


#[cfg(test)]
mod point_tests {
    use super::*;

    #[test]
    fn random_point_is_in_correct_range() {
        for _ in 0..10000 {
            let point = Point::random(15);
            assert!(point.x < 15);
            assert!(point.y < 15);
        }
    }

    
    #[test]
    fn move_point_works_west() {
        assert_eq!(Some(Point::new(3,5)), Point::new(5,5).move_point(West, 2, 10));
        assert_eq!(Some(Point::new(7,5)), Point::new(5,5).move_point(West, -2, 10));
        assert_eq!(None, Point::new(5,5).move_point(West, 6, 10));
        assert_eq!(None, Point::new(5,5).move_point(West, -5, 10));
    }

    #[test]
    fn move_point_works_east() {
        assert_eq!(Some(Point::new(7,5)), Point::new(5,5).move_point(East, 2, 10));
        assert_eq!(Some(Point::new(3,5)), Point::new(5,5).move_point(East, -2, 10));
        assert_eq!(None, Point::new(5,5).move_point(East, 5, 10));
        assert_eq!(None, Point::new(5,5).move_point(East, -6, 10));
    }

    #[test]
    fn move_point_works_south() {
        assert_eq!(Some(Point::new(5,3)), Point::new(5,5).move_point(South, 2, 10));
        assert_eq!(Some(Point::new(5,7)), Point::new(5,5).move_point(South, -2, 10));
        assert_eq!(None, Point::new(5,5).move_point(South, 6, 10));
        assert_eq!(None, Point::new(5,5).move_point(South, -5, 10));
    }

    #[test]
    fn move_point_works_north() {
        assert_eq!(Some(Point::new(5,7)), Point::new(5,5).move_point(North, 2, 10));
        assert_eq!(Some(Point::new(5,3)), Point::new(5,5).move_point(North, -2, 10));
        assert_eq!(None, Point::new(5,5).move_point(North, 5, 10));
        assert_eq!(None, Point::new(5,5).move_point(North, -6, 10));
    }

    #[test]
    fn unrestricted_move_point_works_west() {
        assert_eq!(Point::new(3,5), Point::new(5,5).move_point_no_bounds_check(West, 2));
        assert_eq!(Point::new(7,5), Point::new(5,5).move_point_no_bounds_check(West, -2));
    }

    #[test]
    fn unrestricted_move_point_works_east() {
        assert_eq!(Point::new(7,5), Point::new(5,5).move_point_no_bounds_check(East, 2));
        assert_eq!(Point::new(3,5), Point::new(5,5).move_point_no_bounds_check(East, -2));
    }

    #[test]
    fn unrestricted_move_point_works_south() {
        assert_eq!(Point::new(5,3), Point::new(5,5).move_point_no_bounds_check(South, 2));
        assert_eq!(Point::new(5,7), Point::new(5,5).move_point_no_bounds_check(South, -2));
    }

    #[test]
    fn unrestricted_move_point_works_north() {
        assert_eq!(Point::new(5,7), Point::new(5,5).move_point_no_bounds_check(North, 2));
        assert_eq!(Point::new(5,3), Point::new(5,5).move_point_no_bounds_check(North, -2));
    }
    
    #[test]
    fn ship_collision_check_works_west() {
        assert_eq!(true, Point::new(5,5).check_for_ship_collision(Point::new(5,5), West, 5));
        assert_eq!(true, Point::new(5,5).check_for_ship_collision(Point::new(6,5), West, 5));
        assert_eq!(true, Point::new(5,5).check_for_ship_collision(Point::new(7,5), West, 5));
        assert_eq!(true, Point::new(5,5).check_for_ship_collision(Point::new(8,5), West, 5));
        assert_eq!(true, Point::new(5,5).check_for_ship_collision(Point::new(9,5), West, 5));
        assert_eq!(false, Point::new(5,5).check_for_ship_collision(Point::new(10,5), West, 5));
        assert_eq!(false, Point::new(5,5).check_for_ship_collision(Point::new(4,5), West, 5));
        assert_eq!(false, Point::new(5,5).check_for_ship_collision(Point::new(6,4), West, 5));
        assert_eq!(false, Point::new(5,5).check_for_ship_collision(Point::new(6,6), West, 5));
    }
    
    #[test]
    fn ship_collision_check_works_east() {
        assert_eq!(true, Point::new(5,5).check_for_ship_collision(Point::new(5,5), East, 5));
        assert_eq!(true, Point::new(5,5).check_for_ship_collision(Point::new(4,5), East, 5));
        assert_eq!(true, Point::new(5,5).check_for_ship_collision(Point::new(3,5), East, 5));
        assert_eq!(true, Point::new(5,5).check_for_ship_collision(Point::new(2,5), East, 5));
        assert_eq!(true, Point::new(5,5).check_for_ship_collision(Point::new(1,5), East, 5));
        assert_eq!(false, Point::new(5,5).check_for_ship_collision(Point::new(0,5), East, 5));
        assert_eq!(false, Point::new(5,5).check_for_ship_collision(Point::new(6,5), East, 5));
        assert_eq!(false, Point::new(5,5).check_for_ship_collision(Point::new(4,4), East, 5));
        assert_eq!(false, Point::new(5,5).check_for_ship_collision(Point::new(4,6), East, 5));
    }

    #[test]
    fn ship_collision_check_works_north() {
        assert_eq!(true, Point::new(5,5).check_for_ship_collision(Point::new(5,5), North, 5));
        assert_eq!(true, Point::new(5,5).check_for_ship_collision(Point::new(5,4), North, 5));
        assert_eq!(true, Point::new(5,5).check_for_ship_collision(Point::new(5,3), North, 5));
        assert_eq!(true, Point::new(5,5).check_for_ship_collision(Point::new(5,2), North, 5));
        assert_eq!(true, Point::new(5,5).check_for_ship_collision(Point::new(5,1), North, 5));
        assert_eq!(false, Point::new(5,5).check_for_ship_collision(Point::new(5,0), North, 5));
        assert_eq!(false, Point::new(5,5).check_for_ship_collision(Point::new(5,6), North, 5));
        assert_eq!(false, Point::new(5,5).check_for_ship_collision(Point::new(4,4), North, 5));
        assert_eq!(false, Point::new(5,5).check_for_ship_collision(Point::new(6,4), North, 5));
    }

    #[test]
    fn ship_collision_check_works_south() {
        assert_eq!(true, Point::new(5,5).check_for_ship_collision(Point::new(5,5), South, 5));
        assert_eq!(true, Point::new(5,5).check_for_ship_collision(Point::new(5,6), South, 5));
        assert_eq!(true, Point::new(5,5).check_for_ship_collision(Point::new(5,7), South, 5));
        assert_eq!(true, Point::new(5,5).check_for_ship_collision(Point::new(5,8), South, 5));
        assert_eq!(true, Point::new(5,5).check_for_ship_collision(Point::new(5,9), South, 5));
        assert_eq!(false, Point::new(5,5).check_for_ship_collision(Point::new(5,10), South, 5));
        assert_eq!(false, Point::new(5,5).check_for_ship_collision(Point::new(5,4), South, 5));
        assert_eq!(false, Point::new(5,5).check_for_ship_collision(Point::new(4,6), South, 5));
        assert_eq!(false, Point::new(5,5).check_for_ship_collision(Point::new(6,6), South, 5));
    }

    #[test]
    fn adjacency_check_works() {
        assert_eq!(true, Point::new(5,5).is_adjacent(Point::new(4,5)));
        assert_eq!(true, Point::new(5,5).is_adjacent(Point::new(6,5)));
        assert_eq!(true, Point::new(5,5).is_adjacent(Point::new(5,4)));
        assert_eq!(true, Point::new(5,5).is_adjacent(Point::new(5,6)));

        assert_eq!(false, Point::new(5,5).is_adjacent(Point::new(4,4)));
        assert_eq!(false, Point::new(5,5).is_adjacent(Point::new(6,6)));
        assert_eq!(false, Point::new(5,5).is_adjacent(Point::new(6,4)));
        assert_eq!(false, Point::new(5,5).is_adjacent(Point::new(4,6)));
        assert_eq!(false, Point::new(5,5).is_adjacent(Point::new(5,5)));

        assert_eq!(false, Point::new(5,5).is_adjacent(Point::new(10,5)));
        
    }
    
    #[test]
    fn point_on_4_lattice_works() {
        assert_eq!(true, Point::new(0,0).is_on_lattice(4));
        assert_eq!(true, Point::new(4,0).is_on_lattice(4));
        assert_eq!(true, Point::new(0,4).is_on_lattice(4));
        assert_eq!(true, Point::new(4,4).is_on_lattice(4));
        assert_eq!(true, Point::new(1,3).is_on_lattice(4));
        assert_eq!(true, Point::new(3,1).is_on_lattice(4));
        
        assert_eq!(false, Point::new(0,1).is_on_lattice(4));
        assert_eq!(false, Point::new(0,2).is_on_lattice(4));
        assert_eq!(false, Point::new(0,3).is_on_lattice(4));
        assert_eq!(false, Point::new(1,0).is_on_lattice(4));
        assert_eq!(false, Point::new(2,0).is_on_lattice(4));
        assert_eq!(false, Point::new(3,0).is_on_lattice(4));        
    }

    #[test]
    fn point_on_2_lattice_works() {
        assert_eq!(true, Point::new(0,0).is_on_lattice(2));
        assert_eq!(true, Point::new(2,0).is_on_lattice(2));
        assert_eq!(true, Point::new(0,2).is_on_lattice(2));
        assert_eq!(true, Point::new(2,2).is_on_lattice(2));
        assert_eq!(true, Point::new(1,1).is_on_lattice(2));
        
        assert_eq!(false, Point::new(0,1).is_on_lattice(2));
        assert_eq!(false, Point::new(1,0).is_on_lattice(2));
    }
}