[board] Implement Board::is_king_attacked_from_square
Calculate whether the active color's king is attacked from the provided Square.
This commit is contained in:
		
							parent
							
								
									89a9588e69
								
							
						
					
					
						commit
						21a2a37e1f
					
				
					 1 changed files with 32 additions and 4 deletions
				
			
		| 
						 | 
				
			
			@ -2,19 +2,34 @@
 | 
			
		|||
 | 
			
		||||
use crate::Board;
 | 
			
		||||
use chessfriend_bitboard::BitBoard;
 | 
			
		||||
use chessfriend_core::{Color, Piece};
 | 
			
		||||
use chessfriend_core::{Color, Piece, Square};
 | 
			
		||||
 | 
			
		||||
impl Board {
 | 
			
		||||
    /// Return whether the active color is in check.
 | 
			
		||||
    #[must_use]
 | 
			
		||||
    pub fn is_in_check(&self) -> bool {
 | 
			
		||||
        let color = self.active_color();
 | 
			
		||||
        let king = self.king_bitboard(color);
 | 
			
		||||
        let king = self.king_bitboard_active();
 | 
			
		||||
        let opposing_sight = self.opposing_sight(color);
 | 
			
		||||
        (king & opposing_sight).is_populated()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn king_bitboard(&self, color: Color) -> BitBoard {
 | 
			
		||||
 | 
			
		||||
    fn is_king_attacked_from_square(&self, square: Square) -> bool {
 | 
			
		||||
        let active_king = self.king_bitboard_active();
 | 
			
		||||
        let sight_from_square = self.sight_piece(square);
 | 
			
		||||
        (active_king & sight_from_square).is_populated()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn king_bitboard(&self, color: Option<Color>) -> BitBoard {
 | 
			
		||||
        self.king_bitboard_unwrapped(self.unwrap_color(color))
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn king_bitboard_active(&self) -> BitBoard {
 | 
			
		||||
        self.king_bitboard_unwrapped(self.active_color())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn king_bitboard_unwrapped(&self, color: Color) -> BitBoard {
 | 
			
		||||
        self.find_pieces(Piece::king(color))
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -22,7 +37,7 @@ impl Board {
 | 
			
		|||
#[cfg(test)]
 | 
			
		||||
mod tests {
 | 
			
		||||
    use crate::test_board;
 | 
			
		||||
    use chessfriend_core::Color;
 | 
			
		||||
    use chessfriend_core::Square;
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn active_color_is_in_check() {
 | 
			
		||||
| 
						 | 
				
			
			@ -43,4 +58,17 @@ mod tests {
 | 
			
		|||
 | 
			
		||||
        assert!(!board.is_in_check());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn king_is_attacked_from_square() {
 | 
			
		||||
        let board = test_board!(
 | 
			
		||||
            White King on D3,
 | 
			
		||||
            Black Rook on H3,
 | 
			
		||||
            Black Queen on F4
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        assert!(board.is_king_attacked_from_square(Square::H3));
 | 
			
		||||
        assert!(!board.is_king_attacked_from_square(Square::F4));
 | 
			
		||||
        assert!(!board.is_king_attacked_from_square(Square::A3));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue