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

/// Choice for direction on board
#[derive(Eq, PartialEq, Copy, Clone, Debug)]
pub enum Direction {
    Up,
    Down,
    Left,
    Right,
    Center,
}

/// Coordinate to show position on board
#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
pub(super) struct Coord {
    pub(super) x: u8,
    pub(super) y: u8,
}

impl Coord {
    pub fn in_bounds(self, size: u8) -> bool {
        self.x < size && self.y < size
    }
}

impl Add<Direction> for Coord {
    type Output = Result<Self, &'static str>;

    fn add(self, dir: Direction) -> Result<Self, &'static str> {
        match dir {
            Direction::Up => {
                if self.y == 0 {
                    Err("Already at top row")
                } else {
                    Ok(Coord {
                        x: self.x,
                        y: self.y - 1,
                    })
                }
            }
            Direction::Down => Ok(Coord {
                x: self.x,
                y: self.y + 1,
            }),
            Direction::Left => {
                if self.x == 0 {
                    Err("Already at left column")
                } else {
                    Ok(Coord {
                        x: self.x - 1,
                        y: self.y,
                    })
                }
            }
            Direction::Right => Ok(Coord {
                x: self.x + 1,
                y: self.y,
            }),
            Direction::Center => Ok(self),
        }
    }
}