CoordPair::from_axes

This commit is contained in:
2025-03-05 20:14:18 -05:00
parent e60e546f2f
commit 8a7496a492
4 changed files with 18 additions and 31 deletions

View File

@@ -14,6 +14,13 @@ const_assert!(CoordPairInner::MAX as usize >= Board::BOARD_AREA as usize);
#[derive(PartialEq, Eq, Copy, Clone, Hash)]
pub struct CoordPair(pub CoordPairInner);
impl CoordPair {
/// Convert a row and column to an index in the board
pub const fn from_axes(row: CoordAxis, col: CoordAxis) -> Self {
Self(row * Board::BOARD_SIZE + col)
}
}
impl std::fmt::Display for CoordPair {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let (i, j) = (*self).into();
@@ -27,18 +34,13 @@ impl std::fmt::Debug for CoordPair {
}
}
/// Convert a row and column to an index in the board
const fn get_index(row: CoordAxis, col: CoordAxis) -> CoordPair {
CoordPair(row * Board::BOARD_SIZE + col)
}
fn from_index(index: CoordPair) -> (CoordAxis, CoordAxis) {
index.0.div_mod_floor(&Board::BOARD_SIZE)
}
impl From<(CoordAxis, CoordAxis)> for CoordPair {
fn from(value: (CoordAxis, CoordAxis)) -> Self {
get_index(value.0, value.1)
Self::from_axes(value.0, value.1)
}
}