Files
local-chess/src/chess_api/board/mod.rs
T
2026-10-13 00:05:10 +03:00

81 lines
2.1 KiB
Rust

mod trajectory;
pub use trajectory::*;
mod movement_mask;
pub use movement_mask::mov_mask;
use super::Color;
#[derive(Clone, Copy)]
pub struct Board<const N: usize> {
layers: [[u64; 2]; N],
}
impl<const N: usize> Board<N> {
pub const fn from_layers(layers: [[u64; 2]; N]) -> Self {
Self { layers }
}
pub fn apply_patch(&mut self, patch: &MaskPatch) {
for layer in self.layers.iter_mut() {
layer[0] &= !patch.clear_mask;
layer[1] &= !patch.clear_mask;
}
for (side, y, mask) in patch.iter() {
let x = side.index();
debug_assert!(x < 2 && y < N);
if x >= 2 || y >= N {
eprintln!(
"Board::apply_patch: invalid set_mask coords (side={:?}, layer={}, N={})",
side, y, N
);
panic!("invalid set_mask coordinates");
}
self.layers[y][x] |= mask;
}
}
pub fn get_mask(&self, x: Color, y: usize) -> u64 {
if y >= N || x.index() >= 2 {
return 0;
}
self.layers[y][x.index()]
}
pub fn is_mask_zero(&self, x: Color, y: usize) -> bool {
self.get_mask(x, y) == 0
}
pub fn and_mask_is_zero(&self, x: Color, y: usize, mask: u64) -> bool {
if y >= N || x.index() >= 2 {
return true;
}
(self.layers[y][x.index()] & mask) == 0
}
}
#[derive(Clone, Copy)]
pub struct MaskPatch {
pub clear_mask: u64,
pub set_masks: [Option<(Color, usize, u64)>; 4],
len: u8,
}
impl MaskPatch {
pub fn new(clear_mask: u64) -> Self {
Self { clear_mask, set_masks: [None; 4], len: 0 }
}
pub fn push(&mut self, side: Color, layer: usize, mask: u64) {
debug_assert!((self.len as usize) < 4);
self.set_masks[self.len as usize] = Some((side, layer, mask));
self.len += 1;
}
pub fn iter(&self) -> impl Iterator<Item = (Color, usize, u64)> + '_ {
self.set_masks[..self.len as usize]
.iter()
.map(|o| o.unwrap())
}
}