[board] Some basic bit operations for bitboards

This commit is contained in:
Eryn Wells 2023-12-21 08:17:06 -08:00
parent 0a42adf1fa
commit 4eb734d3eb

View file

@ -1,4 +1,50 @@
// Eryn Wells <eryn@erynwells.me>
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")));
}
}