Compare commits

..

No commits in common. "182bf8112669b156faa27f63e87384e1cfff9df5" and "3a0541a2c310474a6c6c91dd0dbb1fa156ed8e67" have entirely different histories.

6 changed files with 45 additions and 35 deletions

View file

@ -46,6 +46,16 @@ impl BitBoard {
pub const EMPTY: BitBoard = BitBoard(u64::MIN); pub const EMPTY: BitBoard = BitBoard(u64::MIN);
pub const FULL: BitBoard = BitBoard(u64::MAX); pub const FULL: BitBoard = BitBoard(u64::MAX);
#[deprecated(note = "Use BitBoard::EMPTY instead")]
pub const fn empty() -> BitBoard {
Self::EMPTY
}
#[deprecated(note = "Use BitBoard::FULL instead")]
pub const fn full() -> BitBoard {
Self::FULL
}
pub const fn new(bits: u64) -> BitBoard { pub const fn new(bits: u64) -> BitBoard {
BitBoard(bits) BitBoard(bits)
} }
@ -99,7 +109,7 @@ impl BitBoard {
/// ///
/// ``` /// ```
/// use chessfriend_bitboard::BitBoard; /// use chessfriend_bitboard::BitBoard;
/// assert!(BitBoard::EMPTY.is_empty()); /// assert!(BitBoard::empty().is_empty());
/// assert!(!BitBoard::full().is_empty()); /// assert!(!BitBoard::full().is_empty());
/// assert!(!BitBoard::new(0b1000).is_empty()); /// assert!(!BitBoard::new(0b1000).is_empty());
/// ``` /// ```
@ -115,7 +125,7 @@ impl BitBoard {
/// ///
/// ``` /// ```
/// use chessfriend_bitboard::BitBoard; /// use chessfriend_bitboard::BitBoard;
/// assert!(!BitBoard::EMPTY.is_populated()); /// assert!(!BitBoard::empty().is_populated());
/// assert!(BitBoard::full().is_populated()); /// assert!(BitBoard::full().is_populated());
/// assert!(BitBoard::new(0b1).is_populated()); /// assert!(BitBoard::new(0b1).is_populated());
/// ``` /// ```
@ -554,8 +564,8 @@ mod tests {
let b = bitboard![B5 G7 H3]; let b = bitboard![B5 G7 H3];
assert_eq!(a ^ b, bitboard![B5 C5 H3]); assert_eq!(a ^ b, bitboard![B5 C5 H3]);
assert_eq!(a ^ BitBoard::EMPTY, a); assert_eq!(a ^ BitBoard::empty(), a);
assert_eq!(BitBoard::EMPTY ^ BitBoard::EMPTY, BitBoard::EMPTY); assert_eq!(BitBoard::empty() ^ BitBoard::empty(), BitBoard::empty());
} }
#[test] #[test]

View file

@ -110,14 +110,14 @@ pub(super) struct MoveLibrary {
impl MoveLibrary { impl MoveLibrary {
const fn new() -> MoveLibrary { const fn new() -> MoveLibrary {
MoveLibrary { MoveLibrary {
rays: [[BitBoard::EMPTY; Direction::NUM]; Square::NUM], rays: [[BitBoard::empty(); Direction::NUM]; Square::NUM],
pawn_attacks: [[BitBoard::EMPTY; Square::NUM]; Color::NUM], pawn_attacks: [[BitBoard::empty(); Square::NUM]; Color::NUM],
pawn_pushes: [[BitBoard::EMPTY; Square::NUM]; Color::NUM], pawn_pushes: [[BitBoard::empty(); Square::NUM]; Color::NUM],
knight_moves: [BitBoard::EMPTY; Square::NUM], knight_moves: [BitBoard::empty(); Square::NUM],
bishop_moves: [BitBoard::EMPTY; Square::NUM], bishop_moves: [BitBoard::empty(); Square::NUM],
rook_moves: [BitBoard::EMPTY; Square::NUM], rook_moves: [BitBoard::empty(); Square::NUM],
queen_moves: [BitBoard::EMPTY; Square::NUM], queen_moves: [BitBoard::empty(); Square::NUM],
king_moves: [BitBoard::EMPTY; Square::NUM], king_moves: [BitBoard::empty(); Square::NUM],
} }
} }
@ -238,7 +238,7 @@ impl MoveLibrary {
} }
fn _generate_ray(sq: BitBoard, shift: fn(&BitBoard) -> BitBoard) -> BitBoard { fn _generate_ray(sq: BitBoard, shift: fn(&BitBoard) -> BitBoard) -> BitBoard {
let mut ray = BitBoard::EMPTY; let mut ray = BitBoard::empty();
let mut iter = shift(&sq); let mut iter = shift(&sq);
while !iter.is_empty() { while !iter.is_empty() {

View file

@ -41,7 +41,7 @@ impl Movement for Piece {
let parameters = Board::castling_parameters(Wing::KingSide, color); let parameters = Board::castling_parameters(Wing::KingSide, color);
parameters.target.king.into() parameters.target.king.into()
} else { } else {
BitBoard::EMPTY BitBoard::empty()
}; };
let queenside_target_square = if board let queenside_target_square = if board
@ -51,7 +51,7 @@ impl Movement for Piece {
let parameters = Board::castling_parameters(Wing::QueenSide, color); let parameters = Board::castling_parameters(Wing::QueenSide, color);
parameters.target.king.into() parameters.target.king.into()
} else { } else {
BitBoard::EMPTY BitBoard::empty()
}; };
self.sight(square, board) | kingside_target_square | queenside_target_square self.sight(square, board) | kingside_target_square | queenside_target_square
@ -99,11 +99,11 @@ mod tests {
#[test] #[test]
fn white_pushes_empty_board() { fn white_pushes_empty_board() {
assert_eq!( assert_eq!(
pawn_pushes(Square::E4.into(), Color::White, BitBoard::EMPTY), pawn_pushes(Square::E4.into(), Color::White, BitBoard::empty()),
bitboard![E5] bitboard![E5]
); );
assert_eq!( assert_eq!(
pawn_pushes(Square::E2.into(), Color::White, BitBoard::EMPTY), pawn_pushes(Square::E2.into(), Color::White, BitBoard::empty()),
bitboard![E3 E4] bitboard![E3 E4]
); );
} }
@ -111,11 +111,11 @@ mod tests {
#[test] #[test]
fn black_pawn_empty_board() { fn black_pawn_empty_board() {
assert_eq!( assert_eq!(
pawn_pushes(Square::A4.into(), Color::Black, BitBoard::EMPTY), pawn_pushes(Square::A4.into(), Color::Black, BitBoard::empty()),
bitboard![A3] bitboard![A3]
); );
assert_eq!( assert_eq!(
pawn_pushes(Square::B7.into(), Color::Black, BitBoard::EMPTY), pawn_pushes(Square::B7.into(), Color::Black, BitBoard::empty()),
bitboard![B6 B5] bitboard![B6 B5]
); );
} }
@ -124,7 +124,7 @@ mod tests {
fn white_pushes_blocker() { fn white_pushes_blocker() {
assert_eq!( assert_eq!(
pawn_pushes(Square::C5.into(), Color::White, bitboard![C6]), pawn_pushes(Square::C5.into(), Color::White, bitboard![C6]),
BitBoard::EMPTY BitBoard::empty()
); );
assert_eq!( assert_eq!(
pawn_pushes(Square::D2.into(), Color::White, bitboard![D4]), pawn_pushes(Square::D2.into(), Color::White, bitboard![D4]),
@ -132,7 +132,7 @@ mod tests {
); );
assert_eq!( assert_eq!(
pawn_pushes(Square::D2.into(), Color::White, bitboard![D3]), pawn_pushes(Square::D2.into(), Color::White, bitboard![D3]),
BitBoard::EMPTY BitBoard::empty()
); );
} }
@ -140,7 +140,7 @@ mod tests {
fn black_pushes_blocker() { fn black_pushes_blocker() {
assert_eq!( assert_eq!(
pawn_pushes(Square::C5.into(), Color::Black, bitboard![C4]), pawn_pushes(Square::C5.into(), Color::Black, bitboard![C4]),
BitBoard::EMPTY BitBoard::empty()
); );
assert_eq!( assert_eq!(
pawn_pushes(Square::D7.into(), Color::Black, bitboard![D5]), pawn_pushes(Square::D7.into(), Color::Black, bitboard![D5]),
@ -148,7 +148,7 @@ mod tests {
); );
assert_eq!( assert_eq!(
pawn_pushes(Square::D7.into(), Color::Black, bitboard![D6]), pawn_pushes(Square::D7.into(), Color::Black, bitboard![D6]),
BitBoard::EMPTY BitBoard::empty()
); );
} }
} }

View file

@ -51,10 +51,11 @@ impl PieceSet {
color_occupancy[color_index] |= bitboard; color_occupancy[color_index] |= bitboard;
shape_occupancy[shape_index] |= bitboard; shape_occupancy[shape_index] |= bitboard;
counts.increment(color, shape);
for square in bitboard.occupied_squares(&IterationDirection::default()) { for square in bitboard.occupied_squares(&IterationDirection::default()) {
let piece = Piece::new(color, shape); let piece = Piece::new(color, shape);
mailbox.set(piece, square); mailbox.set(piece, square);
counts.increment(color, shape);
} }
} }
} }

View file

@ -17,21 +17,20 @@ impl Counts {
const SQUARE_NUM: u8 = Square::NUM as u8; const SQUARE_NUM: u8 = Square::NUM as u8;
let updated_value = self.0[color as usize][shape as usize] + 1; let updated_value = self.0[color as usize][shape as usize] + 1;
if updated_value > SQUARE_NUM { if updated_value <= SQUARE_NUM {
let shape_name = shape.name(); self.0[color as usize][shape as usize] = updated_value;
panic!("piece count for {color} {shape_name} overflowed"); } else {
unreachable!("piece count for {color} {shape} overflowed");
} }
self.0[color as usize][shape as usize] = updated_value;
} }
pub fn decrement(&mut self, color: Color, shape: Shape) { pub fn decrement(&mut self, color: Color, shape: Shape) {
let count = self.0[color as usize][shape as usize]; let count = self.0[color as usize][shape as usize];
let updated_count = count.checked_sub(1).unwrap_or_else(|| { if let Some(updated_count) = count.checked_sub(1) {
let shape_name = shape.name(); self.0[color as usize][shape as usize] = updated_count;
panic!("piece count for {color} {shape_name} should not underflow"); } else {
}); unreachable!("piece count for {color} {shape} underflowed");
self.0[color as usize][shape as usize] = updated_count; }
} }
#[cfg(test)] #[cfg(test)]

View file

@ -305,7 +305,7 @@ mod tests {
let piece = piece!(White Pawn); let piece = piece!(White Pawn);
let sight = piece.sight(Square::E4, &pos); let sight = piece.sight(Square::E4, &pos);
assert_eq!(sight, BitBoard::EMPTY); assert_eq!(sight, BitBoard::empty());
} }
#[test] #[test]