initial bit board storage

This commit is contained in:
2025-02-12 01:02:51 -05:00
parent 7992aca803
commit 59944e1e9e
4 changed files with 94 additions and 40 deletions

44
src/bitboard.rs Normal file
View File

@@ -0,0 +1,44 @@
use crate::board::BOARD_SIZE;
/// 8x8
/// TODO! look into variable length bit arrays in rust
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct BitBoard(u64);
impl BitBoard {
pub const fn new() -> Self {
BitBoard(0)
}
pub const fn get(&self, row: usize, col: usize) -> bool {
((self.0 >> (row * BOARD_SIZE + col)) & 1) != 0
}
pub const fn set(&mut self, row: usize, col: usize, value: bool) {
let index = row * BOARD_SIZE + col;
if value {
self.0 |= 1 << index; // Set the bit at (row, col) to 1
} else {
self.0 &= !(1 << index); // Clear the bit at (row, col)
}
}
pub const fn count(&self) -> u32 {
self.0.count_ones()
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn set_and_get() {
let mut b = BitBoard::new();
assert!(!b.get(2, 4));
b.set(2, 4, true);
assert!(b.get(2, 4));
b.set(2, 4, false);
assert!(!b.get(2, 4));
}
}