2023-12-19 11:13:06 -08:00
|
|
|
// Eryn Wells <eryn@erynwells.me>
|
|
|
|
|
2023-12-21 08:17:06 -08:00
|
|
|
use crate::square::Square;
|
|
|
|
|
2023-12-19 11:13:06 -08:00
|
|
|
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
2023-12-20 11:45:12 -08:00
|
|
|
pub struct BitBoard(pub u64);
|
2023-12-21 08:17:06 -08:00
|
|
|
|
|
|
|
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")));
|
|
|
|
}
|
|
|
|
}
|