From f337b8053d2e1b567d5f42f21bc4b00f3fcdd2d2 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Wed, 17 Jan 2024 08:48:23 -0800 Subject: [PATCH] [board] Break getting the square the king is on into a separate helper method Implement Position::king_square() and use it in is_king_in_check() --- board/src/position/position.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/board/src/position/position.rs b/board/src/position/position.rs index a307b71..d45fd1d 100644 --- a/board/src/position/position.rs +++ b/board/src/position/position.rs @@ -212,12 +212,15 @@ impl Position { pub(crate) fn is_king_in_check(&self) -> bool { let sight_of_opposing_player = self.sight_of_player(self.color_to_move.other()); - let king_square = self - .bitboard_for_piece(Piece::king(self.color_to_move)) + sight_of_opposing_player.is_set(self.king_square()) + } + + fn king_square(&self) -> Square { + self.bitboard_for_piece(Piece::king(self.color_to_move)) .occupied_squares() .next() - .unwrap(); - sight_of_opposing_player.is_set(king_square) + .unwrap() + } } #[cfg(test)]