diff --git a/board/src/check.rs b/board/src/check.rs new file mode 100644 index 0000000..92fa168 --- /dev/null +++ b/board/src/check.rs @@ -0,0 +1,49 @@ +// Eryn Wells + +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)); + } +} diff --git a/board/src/lib.rs b/board/src/lib.rs index 3a347d4..1df6832 100644 --- a/board/src/lib.rs +++ b/board/src/lib.rs @@ -9,6 +9,7 @@ pub mod macros; pub mod movement; pub mod sight; +mod check; mod piece_sets; pub use board::Board; diff --git a/position/src/position/position.rs b/position/src/position/position.rs index 0eedb0d..c3d5793 100644 --- a/position/src/position/position.rs +++ b/position/src/position/position.rs @@ -122,23 +122,6 @@ impl Position { self.moves().moves_for_piece(piece) } - #[cfg(test)] - pub(crate) fn is_king_in_check(&self) -> bool { - let danger_squares = self.king_danger(self.player_to_move()); - !(danger_squares & self.king_bitboard(self.player_to_move())).is_empty() - } - - fn king_bitboard(&self, player: Color) -> BitBoard { - self.board.pieces.bitboard_for_piece(Piece::king(player)) - } - - pub(crate) fn king_square(&self, player: Color) -> Square { - self.king_bitboard(player) - .occupied_squares(&IterationDirection::default()) - .next() - .unwrap() - } - pub(crate) fn checking_pieces(&self) -> CheckingPieces { let opponent = self.player_to_move().other(); let king_square = self.king_square(self.player_to_move());