pub struct Coord { /* private fields */ }Expand description
Checked container for a coordinate to address into a Board
Implementations§
Source§impl Coord
impl Coord
Sourcepub fn valid(x: usize, y: usize) -> bool
pub fn valid(x: usize, y: usize) -> bool
Check whether the given (x, y) pair is a valid coordinate pair
Sourcepub fn zero_based(x: usize, y: usize) -> Result<Self, ThudError>
pub fn zero_based(x: usize, y: usize) -> Result<Self, ThudError>
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) if the coordinates supplied are out of bounds
of the board.
Sourcepub fn value(&self) -> (usize, usize)
pub fn value(&self) -> (usize, usize)
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.
Sourcepub fn max(&self) -> usize
pub fn max(&self) -> usize
Return the larger of the two coordinates.
Useful for use with .diff() to get the orthogonal/diagonal distance between two squares:
use thud::Coord;
let source = Coord::zero_based(7,7)?;
let destination1 = Coord::zero_based(10, 10)?;
let destination2 = Coord::zero_based(12, 7)?;
assert_eq!(source.diff(destination1).max(), 3);
assert_eq!(source.diff(destination2).max(), 5);Sourcepub fn diff(self, rhs: Self) -> Self
pub fn diff(self, rhs: Self) -> Self
Return the absolute difference between two Coords.
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());