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

@@ -143,12 +143,13 @@ impl Board {
}
/// Starting position
pub fn starting_pos(mut self) -> Self {
pub const fn starting_pos(mut self) -> Self {
let hf = Self::BOARD_SIZE / 2;
self.place_unchecked((hf - 1, hf - 1).into(), Piece::White);
self.place_unchecked((hf, hf - 1).into(), Piece::Black);
self.place_unchecked((hf - 1, hf).into(), Piece::Black);
self.place_unchecked((hf, hf).into(), Piece::White);
self.place_unchecked(CoordPair::from_axes(hf - 1, hf - 1), Piece::White);
self.place_unchecked(CoordPair::from_axes(hf, hf - 1), Piece::Black);
self.place_unchecked(CoordPair::from_axes(hf - 1, hf), Piece::Black);
self.place_unchecked(CoordPair::from_axes(hf, hf), Piece::White);
self
}
@@ -178,13 +179,11 @@ impl Board {
}
}
#[const_fn(cfg(not(feature = "bitvec")))]
pub const fn get_piece(&self, coord: CoordPair, color: Piece) -> bool {
self.board(color).get(coord)
}
/// Returns the color of a place on the [`Board`] at a position
#[const_fn(cfg(not(feature = "bitvec")))]
pub const fn get(&self, coord: CoordPair) -> Option<Piece> {
if self.get_piece(coord, Piece::White) {
Some(Piece::White)
@@ -197,13 +196,11 @@ impl Board {
/// Place a piece without checking for propegation of validity
/// only pub for setting up benchmark
#[const_fn(cfg(not(feature = "bitvec")))]
pub const fn place_unchecked(&mut self, coord: CoordPair, piece: Piece) {
self.board_mut(piece).set(coord, true);
self.board_mut(piece.flip()).set(coord, false);
}
#[const_fn(cfg(not(feature = "bitvec")))]
const fn delete(&mut self, coord: CoordPair) {
self.board_mut(Piece::White).set(coord, false);
self.board_mut(Piece::Black).set(coord, false);
@@ -289,14 +286,12 @@ impl Board {
}
/// Count the number of a type of [`Piece`] on the board
#[const_fn(cfg(not(feature = "bitvec")))]
pub const fn count(&self, piece: Piece) -> usize {
self.board(piece).count()
}
/// Get the "net score" of a player
/// Formula: `net_score = Score_player - Score_opponent`
#[const_fn(cfg(not(feature = "bitvec")))]
pub const fn net_score(&self, piece: Piece) -> i16 {
self.count(piece) as i16 - self.count(piece.flip()) as i16
}

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)
}
}