diff --git a/src/repr/bitboard.rs b/src/repr/bitboard.rs index 521211e..c548e6c 100644 --- a/src/repr/bitboard.rs +++ b/src/repr/bitboard.rs @@ -12,7 +12,7 @@ pub type BitBoardInner = u64; pub struct BitBoard(BitBoardInner); // BitBoard should be big enough to fit all points on the board -const_assert!(std::mem::size_of::() * 8 >= Board::BOARD_AREA as usize); +const_assert!(std::mem::size_of::() * 8 >= Board::BOARD_AREA.0 as usize); impl BitBoard { #[allow(clippy::new_without_default)] @@ -38,7 +38,6 @@ impl BitBoard { self.0 |= (value as BitBoardInner) << index; // set bit (if needed) } - // works on both `bitvec` and native (const on native) pub const fn count(&self) -> usize { self.0.count_ones() as usize } @@ -94,7 +93,7 @@ impl BitBoard { const fn col_mask(col: CoordAxis) -> Self { let mut mask = 0; let mut i = 0; - while i < Board::BOARD_AREA { + while i < Board::BOARD_AREA.0 { mask |= 1 << (i + col); i += Board::BOARD_SIZE; } @@ -154,7 +153,7 @@ mod test { #[test] fn set_and_get() { let mut b = BitBoard::new(); - for c in 0..Board::BOARD_AREA { + for c in 0..Board::BOARD_AREA.0 { assert!( !b.get(CoordPair(c)), "A just-initalized BitBoard should be completely empty" diff --git a/src/repr/board.rs b/src/repr/board.rs index 8a57052..cf95dde 100644 --- a/src/repr/board.rs +++ b/src/repr/board.rs @@ -6,13 +6,13 @@ use std::{cmp::Ordering, fmt}; /// Map of all points on the board against some type T /// Used to index like so: example[i][j] /// with each coordinate -pub struct PosMap(ArrayVec); +pub struct PosMap(ArrayVec); impl PosMap { #[allow(clippy::new_without_default)] pub fn new() -> Self { Self(ArrayVec::from_iter( - (0..Board::BOARD_AREA).map(|_| Default::default()), + (0..Board::BOARD_AREA.0).map(|_| Default::default()), )) } @@ -117,7 +117,7 @@ impl Board { pub const BOARD_SIZE: CoordAxis = 8; /// Area of the board - pub const BOARD_AREA: CoordAxis = Self::BOARD_SIZE.pow(2); + pub const BOARD_AREA: CoordPair = CoordPair(CoordPair(Self::BOARD_SIZE).0.pow(2)); /// Create a new empty board #[allow(clippy::new_without_default)] @@ -156,7 +156,7 @@ impl Board { /// Provides an iterator of all possible positions on the board pub fn all_positions() -> impl Iterator { - (0..Self::BOARD_SIZE).flat_map(|i| (0..Self::BOARD_SIZE).map(move |j| (i, j).into())) + (0..Self::BOARD_AREA.0).map(CoordPair) } /// Returns an iterator of all possible moves a `color` can make @@ -248,12 +248,16 @@ impl Board { let count = flip_mask.count(); // Apply the flips - *self.board_mut(starting_color) |= flip_mask; - *self.board_mut(starting_color.flip()) &= !flip_mask; + self.apply_flip_mask(starting_color, flip_mask); count } + fn apply_flip_mask(&mut self, color: Piece, flip_mask: BitBoard) { + *self.board_mut(color) |= flip_mask; + *self.board_mut(color.flip()) &= !flip_mask; + } + /// Propegate piece captures originating from (i, j) /// DO NOT USE THIS ALONE, this should be called as a part of /// [`Board::place`] or [`Board::place_and_prop_unchecked`] diff --git a/src/repr/coords.rs b/src/repr/coords.rs index 3ad43d2..86ef6b0 100644 --- a/src/repr/coords.rs +++ b/src/repr/coords.rs @@ -9,7 +9,7 @@ pub type CoordAxis = u8; /// using u8 for this results in a ~3-6% perf increase pub type CoordPairInner = u8; -const_assert!(CoordPairInner::MAX as usize >= Board::BOARD_AREA as usize); +const_assert!(CoordPairInner::MAX as usize >= Board::BOARD_AREA.0 as usize); #[derive(PartialEq, Eq, Copy, Clone, Hash)] pub struct CoordPair(pub CoordPairInner);