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
use crate::ThudError;

/// Checked container for a coordinate to address into a [`Board`](enum.Board.html)
#[derive(Debug, PartialEq, Copy, Clone)]
pub struct Coord {
    x: usize,
    y: usize,
}

impl Coord {
    fn check_coords(x: usize, y: usize) -> Result<(), ThudError> {
        let sum = x + y;

        // Check flat bounds first
        let invalid = (x > 14)
            || (y > 14)
            // Extreme corners
            || (sum < 5)
            || (sum > 23)
            // Outer corners
            || (9 + y < x)
            || (9 + x < y);

        if invalid {
            Err(ThudError::InvalidPosition)
        } else {
            Ok(())
        }
    }

    /// Make a new `Coord` using 0-based axes values.
    ///
    /// The squares are addressed as if the board were a 15x15 square with the bottom-left square
    /// being (0, 0); confusingly, this is out of bounds. See the official Thud rules for the
    /// shape of the board.
    ///
    /// Will return [`Err(ThudError::InvalidPosition)`](enum.ThudError.html) if the coordinates supplied are out of bounds
    /// of the board.
    pub fn zero_based(x: usize, y: usize) -> Result<Self, ThudError> {
        Coord::check_coords(x, y)?;
        Ok(Coord { x, y })
    }

    pub fn one_based(x: usize, y: usize) -> Result<Self, ThudError> {
        let (x, y) = (x - 1, y - 1);
        Self::zero_based(x, y)
    }

    /// Get the values inside the coordinate, zero-based.
    ///
    /// Since `Coord` is bound-checked on creation, the values returned here are guaranteed to be
    /// valid coordinates on the board.
    pub fn value(&self) -> (usize, usize) {
        (self.x, self.y)
    }

    /// Return the larger of the two coordinates.
    ///
    /// Useful for use with [`.diff()`](#method.diff) to get the orthogonal/diagonal distance between two squares:
    /// ```
    /// use thud::Coord;
    ///
    /// let source = Coord::zero_based(7,7).unwrap();
    /// let destination1 = Coord::zero_based(10, 10).unwrap();
    /// let destination2 = Coord::zero_based(12, 7).unwrap();
    ///
    /// assert_eq!(source.diff(destination1).max(), 3);
    /// assert_eq!(source.diff(destination2).max(), 5);
    /// ```
    pub fn max(&self) -> usize {
        if self.x > self.y {
            self.x
        } else {
            self.y
        }
    }

    /// Return the *absolute* difference between two `Coord`s.
    ///
    /// Example:
    /// ```
    /// use thud::Coord;
    ///
    /// let source = Coord::zero_based(7,7).unwrap();
    /// let destination1 = Coord::zero_based(10, 10).unwrap();
    /// let destination2 = Coord::zero_based(12, 7).unwrap();
    ///
    /// assert_eq!(source.diff(destination1), (3, 3).into());
    /// assert_eq!(source.diff(destination2), (5, 0).into());
    /// ```
    pub fn diff(self, rhs: Self) -> Self {
        let new_x = if self.x > rhs.x {
            self.x - rhs.x
        } else {
            rhs.x - self.x
        };
        let new_y = if self.y > rhs.y {
            self.y - rhs.y
        } else {
            rhs.y - self.y
        };

        Coord { x: new_x, y: new_y }
    }
}

impl From<(usize, usize)> for Coord {
    fn from((x, y): (usize, usize)) -> Self {
        Coord::zero_based(x, y).unwrap()
    }
}

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

    #[test_case(6, 9)]
    #[test_case(2, 3)]
    #[test_case(5, 0)]
    #[test_case(9, 0)]
    #[test_case(10, 2)]
    #[test_case(13, 5)]
    #[test_case(14, 6)]
    #[test_case(14, 8)]
    #[test_case(0, 0 => panics "no")]
    #[test_case(4, 0 => panics "no")]
    #[test_case(10, 0 => panics "no")]
    #[test_case(14, 4 => panics "no")]
    #[test_case(14, 10 => panics "no")]
    fn valid_coordinates(x: usize, y: usize) {
        Coord::zero_based(x, y).expect("no");
    }

    #[test_case(0, 9)]
    #[test_case(2, 3)]
    #[test_case(5, 0)]
    #[test_case(9, 0)]
    #[test_case(10, 2)]
    #[test_case(13, 5)]
    #[test_case(14, 6)]
    fn into(x: usize, y: usize) {
        let _coord: Coord = (x, y).into();
    }

    #[test_case((7,7), (8,8) => 1)]
    #[test_case((8,8), (7,7) => 1)]
    #[test_case((7,8), (8,8) => 1)]
    #[test_case((7,7), (9,8) => 2)]
    #[test_case((7,7), (10,7) => 3)]
    #[test_case((8,7), (9,7) => 1)]
    fn diff_then_max(lhs: (usize, usize), rhs: (usize, usize)) -> usize {
        Coord::zero_based(lhs.0, lhs.1)
            .unwrap()
            .diff(Coord::zero_based(rhs.0, rhs.1).unwrap())
            .max()
    }
}