[board, position] Implement some methods to check for whether a king is in check

Remove some dead code from Position.
This commit is contained in:
Eryn Wells 2025-05-23 09:57:48 -07:00
parent 0abe9b6c19
commit b8a51990a3
3 changed files with 50 additions and 17 deletions

49
board/src/check.rs Normal file
View file

@ -0,0 +1,49 @@
// Eryn Wells <eryn@erynwells.me>
use crate::Board;
use chessfriend_bitboard::BitBoard;
use chessfriend_core::{Color, Piece};
impl Board {
#[must_use]
pub fn active_color_is_in_check(&self) -> bool {
self.color_is_in_check(self.active_color)
}
#[must_use]
pub fn color_is_in_check(&self, color: Color) -> bool {
let king = self.king_bitboard(color);
let opposing_sight = self.opposing_sight(color);
(king & opposing_sight).is_populated()
}
fn king_bitboard(&self, color: Color) -> BitBoard {
self.pieces.find_pieces(Piece::king(color))
}
}
#[cfg(test)]
mod tests {
use crate::test_board;
use chessfriend_core::Color;
#[test]
fn active_color_is_in_check() {
let board = test_board!(
White King on A3,
Black Rook on F3,
);
assert!(board.color_is_in_check(Color::White));
}
#[test]
fn active_color_is_not_in_check() {
let board = test_board!(
White King on A3,
Black Rook on B4,
);
assert!(!board.color_is_in_check(Color::White));
}
}

View file

@ -9,6 +9,7 @@ pub mod macros;
pub mod movement;
pub mod sight;
mod check;
mod piece_sets;
pub use board::Board;