From 4eb734d3eb9df150f2aa456f254fd413552ae3f2 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Thu, 21 Dec 2023 08:17:06 -0800 Subject: [PATCH] [board] Some basic bit operations for bitboards --- board/src/bitboard.rs | 46 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/board/src/bitboard.rs b/board/src/bitboard.rs index 68429fb..4579e47 100644 --- a/board/src/bitboard.rs +++ b/board/src/bitboard.rs @@ -1,4 +1,50 @@ // Eryn Wells +use crate::square::Square; + #[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)] pub struct BitBoard(pub u64); + +impl BitBoard { + fn is_empty(&self) -> bool { + self.0 == 0 + } + + fn has_piece_at(&self, sq: &Square) -> bool { + (self.0 & (1 << sq.index)) > 0 + } + + fn place_piece_at(&mut self, sq: &Square) { + self.0 |= 1 << sq.index + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::square::Square; + + #[test] + fn is_empty() { + assert!(BitBoard(0).is_empty()); + } + + #[test] + fn has_piece_at() { + let bb = BitBoard(0b1001100); + + assert!(bb.has_piece_at( + &Square::from_algebraic_string("a3").expect("Unable to get square for 'a3'") + )); + assert!(!bb.has_piece_at( + &Square::from_algebraic_string("a2").expect("Unable to get square for 'a2'") + )); + } + + #[test] + fn place_piece_at() { + let mut bb = BitBoard(0b1001100); + bb.place_piece_at(&Square::from_index(34).expect("Invalid square index")); + assert!(bb.has_piece_at(&Square::from_index(34).expect("Invalid square index"))); + } +}