diff options
author | eug-vs <eugene@eug-vs.xyz> | 2023-01-22 18:04:35 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2023-01-22 18:04:35 +0300 |
commit | cd1e76b490c34ae2e2eaeed27eebe524d3cad486 (patch) | |
tree | 67b8e57fe42691f91c0a7f223dab18d7b6db2a9d /src/board.rs | |
parent | 451bde2e5a07645afb98edb5760f1726e2f0be4a (diff) | |
download | chessnost-cd1e76b490c34ae2e2eaeed27eebe524d3cad486.tar.gz |
feat: compute board occupancy and empty squares
Diffstat (limited to 'src/board.rs')
-rw-r--r-- | src/board.rs | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/src/board.rs b/src/board.rs index 241412e..b76356f 100644 --- a/src/board.rs +++ b/src/board.rs @@ -3,6 +3,9 @@ use crate::bitboard::Bitboard; #[derive(Debug)] pub struct Board { pub pieces: [Bitboard; 12], + + pub occupancy: Bitboard, + pub ply: u16, } @@ -80,7 +83,21 @@ impl Board { } } - Self { pieces } + let mut board = Self { pieces, occupancy: 0, ply: 0 }; + board.update_occupancy(); + board + } + + fn update_occupancy(&mut self) { + self.occupancy = 0; + // TODO: reduce + for piece in self.pieces { + self.occupancy |= piece; + } + } + + fn empty(&self) -> Bitboard { + !self.occupancy } pub fn print(&self) { @@ -130,7 +147,7 @@ pub enum Square { #[cfg(test)] mod tests { use super::*; - use crate::bitboard::{pop_count, bitscan}; + use crate::bitboard::{pop_count, bitscan, print}; #[test] fn test_square_enum() { @@ -144,6 +161,9 @@ mod tests { let fen = String::from("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); let board = Board::from_FEN(fen); + board.print(); + print(board.empty(), "Empty squares"); + assert_eq!(pop_count(board.pieces[PieceTypes::Pawn as usize]), 8); assert_eq!(pop_count(board.pieces[PieceTypes::Knight as usize]), 2); assert_eq!(pop_count(board.pieces[PieceTypes::Bishop as usize]), 2); @@ -161,6 +181,7 @@ mod tests { assert_eq!(bitscan(board.pieces[PieceTypes::King as usize]), Square::E1 as u8); assert_eq!(bitscan(board.pieces[PieceTypes::QueenBlack as usize]), Square::D8 as u8); - board.print(); + assert_eq!(pop_count(board.occupancy), 32); + assert_eq!(pop_count(board.empty()), 32); } } |