Initial implementation of CheckingPieces and Position::checking_pieces()
This commit is contained in:
		
							parent
							
								
									26aedd8899
								
							
						
					
					
						commit
						8aa5dacfc8
					
				
					 3 changed files with 68 additions and 0 deletions
				
			
		
							
								
								
									
										29
									
								
								position/src/check.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								position/src/check.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,29 @@
 | 
			
		|||
// Eryn Wells <eryn@erynwells.me>
 | 
			
		||||
 | 
			
		||||
use chessfriend_bitboard::BitBoard;
 | 
			
		||||
 | 
			
		||||
pub struct CheckingPieces {
 | 
			
		||||
    pawn: BitBoard,
 | 
			
		||||
    knight: BitBoard,
 | 
			
		||||
    bishop: BitBoard,
 | 
			
		||||
    rook: BitBoard,
 | 
			
		||||
    queen: BitBoard,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl CheckingPieces {
 | 
			
		||||
    pub(crate) fn new(
 | 
			
		||||
        pawn: BitBoard,
 | 
			
		||||
        knight: BitBoard,
 | 
			
		||||
        bishop: BitBoard,
 | 
			
		||||
        rook: BitBoard,
 | 
			
		||||
        queen: BitBoard,
 | 
			
		||||
    ) -> CheckingPieces {
 | 
			
		||||
        CheckingPieces {
 | 
			
		||||
            pawn,
 | 
			
		||||
            knight,
 | 
			
		||||
            bishop,
 | 
			
		||||
            rook,
 | 
			
		||||
            queen,
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -2,6 +2,7 @@
 | 
			
		|||
 | 
			
		||||
pub mod fen;
 | 
			
		||||
 | 
			
		||||
mod check;
 | 
			
		||||
mod display;
 | 
			
		||||
mod r#move;
 | 
			
		||||
mod move_generator;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -249,6 +249,44 @@ impl Position {
 | 
			
		|||
            .next()
 | 
			
		||||
            .unwrap()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub(crate) fn checking_pieces(&self) -> CheckingPieces {
 | 
			
		||||
        let opponent = self.color_to_move.other();
 | 
			
		||||
        let king_square = self.king_square(self.color_to_move);
 | 
			
		||||
 | 
			
		||||
        let checking_pawns = {
 | 
			
		||||
            // The current player's pawn attack moves *from* this square are the
 | 
			
		||||
            // same as the pawn moves for the opposing player attacking this square.
 | 
			
		||||
            let pawn_moves_to_king_square = BitBoard::pawn_attacks(king_square, self.color_to_move);
 | 
			
		||||
            let opposing_pawn = Piece::pawn(opponent);
 | 
			
		||||
            let opposing_pawns = self.pieces.bitboard_for_piece(&opposing_pawn);
 | 
			
		||||
 | 
			
		||||
            pawn_moves_to_king_square & opposing_pawns
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        macro_rules! checking_piece {
 | 
			
		||||
            ($moves_bb_fn:path, $piece_fn:path) => {{
 | 
			
		||||
                let moves_from_opposing_square = $moves_bb_fn(king_square);
 | 
			
		||||
                let piece = $piece_fn(opponent);
 | 
			
		||||
                let opposing_pieces = self.pieces.bitboard_for_piece(&piece);
 | 
			
		||||
 | 
			
		||||
                moves_from_opposing_square & opposing_pieces
 | 
			
		||||
            }};
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        let checking_knights = checking_piece!(BitBoard::knight_moves, Piece::knight);
 | 
			
		||||
        let checking_bishops = checking_piece!(BitBoard::bishop_moves, Piece::bishop);
 | 
			
		||||
        let checking_rooks = checking_piece!(BitBoard::rook_moves, Piece::rook);
 | 
			
		||||
        let checking_queens = checking_piece!(BitBoard::queen_moves, Piece::queen);
 | 
			
		||||
 | 
			
		||||
        CheckingPieces::new(
 | 
			
		||||
            checking_pawns,
 | 
			
		||||
            checking_knights,
 | 
			
		||||
            checking_bishops,
 | 
			
		||||
            checking_rooks,
 | 
			
		||||
            checking_queens,
 | 
			
		||||
        )
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// crate::position methods
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue