[board, position] Simplify check methods

Only one check-testing method, Board::is_in_check(), that tests if the current
active player is in check. It doesn't make sense to test if the non-active player
is in check.
This commit is contained in:
Eryn Wells 2025-06-29 09:25:08 -07:00
parent a30553503f
commit e3d17219ad
3 changed files with 9 additions and 17 deletions

View file

@ -5,18 +5,10 @@ use chessfriend_bitboard::BitBoard;
use chessfriend_core::{Color, Piece}; use chessfriend_core::{Color, Piece};
impl Board { impl Board {
/// Return whether the active color is in check.
#[must_use] #[must_use]
pub fn active_color_is_in_check(&self) -> bool { pub fn is_in_check(&self) -> bool {
self.unwrapped_color_is_in_check(self.active_color()) let color = self.active_color();
}
#[must_use]
pub fn color_is_in_check(&self, color: Option<Color>) -> bool {
self.unwrapped_color_is_in_check(self.unwrap_color(color))
}
#[must_use]
pub fn unwrapped_color_is_in_check(&self, color: Color) -> bool {
let king = self.king_bitboard(color); let king = self.king_bitboard(color);
let opposing_sight = self.opposing_sight(color); let opposing_sight = self.opposing_sight(color);
(king & opposing_sight).is_populated() (king & opposing_sight).is_populated()
@ -39,7 +31,7 @@ mod tests {
Black Rook on F3, Black Rook on F3,
); );
assert!(board.unwrapped_color_is_in_check(Color::White)); assert!(board.is_in_check());
} }
#[test] #[test]
@ -49,6 +41,6 @@ mod tests {
Black Rook on B4, Black Rook on B4,
); );
assert!(!board.unwrapped_color_is_in_check(Color::White)); assert!(!board.is_in_check());
} }
} }

View file

@ -129,7 +129,7 @@ impl Position {
); );
}); });
let move_is_legal = !test_board.color_is_in_check(Some(active_color_before_move)); let move_is_legal = !test_board.is_in_check();
test_board.unmake_move(&record).unwrap_or_else(|err| { test_board.unmake_move(&record).unwrap_or_else(|err| {
panic!( panic!(

View file

@ -107,7 +107,7 @@ fn en_passant_check_capture() {
White Pawn on D4, White Pawn on D4,
], D3); ], D3);
assert!(pos.board().active_color_is_in_check()); assert!(pos.board().is_in_check());
let generated_moves: HashSet<_> = pos.all_legal_moves(Some(Color::Black)).collect(); let generated_moves: HashSet<_> = pos.all_legal_moves(Some(Color::Black)).collect();
@ -123,7 +123,7 @@ fn en_passant_check_block() {
White Queen on F1, White Queen on F1,
], D3); ], D3);
assert!(pos.board().active_color_is_in_check()); assert!(pos.board().is_in_check());
let generated_moves: HashSet<_> = pos.all_legal_moves(Some(Color::Black)).collect(); let generated_moves: HashSet<_> = pos.all_legal_moves(Some(Color::Black)).collect();
@ -139,7 +139,7 @@ fn pinned_pieces_rook_cannot_move_out_of_pin() {
White King on C1, White King on C1,
]); ]);
assert!(!pos.board().active_color_is_in_check()); assert!(!pos.board().is_in_check());
let rook_moves: HashSet<_> = pos.all_legal_moves(None).collect(); let rook_moves: HashSet<_> = pos.all_legal_moves(None).collect();