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
use std::ops::Add;

#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, Serialize, Deserialize)]
pub struct Point {
    pub x: u8,
    pub y: u8
}
impl Point {
    pub fn new(x: u8, y: u8) -> Point {
        assert!(x < 5 && y < 5);
        Point {x, y}
    }
    pub fn can_add(&self, s: Step) -> bool {
        let new_x = (self.x as i8) + s.dx;
        let new_y = (self.y as i8) + s.dy;
        new_x >= 0 && new_y >= 0 && new_x < 5 && new_y < 5
    }
}

impl Add<Step> for Point {
    type Output = Point;
    fn add(self, s: Step) -> Point {
        assert!(self.can_add(s));
        Point::new(
            ((self.x as i8) + s.dx) as u8,
            ((self.y as i8) + s.dy) as u8
        )
    }
}


#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, Serialize, Deserialize)]
pub struct Step {
    pub dx: i8,
    pub dy: i8
}
impl Step {
    pub fn new(dx: i8, dy: i8) -> Step {
        assert!(dx > -4 && dy > -4 && dx < 5 && dy < 5 && (dx != 0 || dy != 0));
        Step {dx, dy}
    }

    pub fn flip(&self) -> Step {
        Step::new(-self.dx, -self.dy)
    }
}