diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/attacks.rs | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/attacks.rs b/src/attacks.rs index 0315ec9..73f3d73 100644 --- a/src/attacks.rs +++ b/src/attacks.rs @@ -8,6 +8,8 @@ static B_FILE: Bitboard = 0x0202020202020202; static H_FILE: Bitboard = 0x8080808080808080; static DIAG_C2_H7: Bitboard = 0x0080402010080400; static DIAG_A1_H8: Bitboard = 0x8040201008040201; +static RANK_2: Bitboard = 0x000000000000FF00; +static RANK_6: Bitboard = 0x0000FF0000000000; /// An array where N-th item is an attack bitboard /// of a piece on N-th square @@ -40,7 +42,10 @@ enum Direction { pub struct Attacks { pub knight: AttackTable, pub king: AttackTable, + /// TODO: compute pawn attacks set-wise pub pawn: [AttackTable; 2], + pub pawn_pushes: [AttackTable; 2], + pub first_rank_attacks: FirstRankAttacks, /// Should be indexed by Direction pub ray_attacks: [AttackTable; 8], @@ -54,11 +59,13 @@ impl Attacks { let first_rank_attacks = Self::precompute_first_rank_attacks(); let ray_attacks = Self::precompute_ray_attacks(); let pawn = Self::precompute_pawn_attacks(); + let pawn_pushes = Self::precompute_pawn_pushes(); Self { knight, king, pawn, + pawn_pushes, first_rank_attacks, ray_attacks, } @@ -74,6 +81,16 @@ impl Attacks { attacks } + fn precompute_pawn_pushes() -> [AttackTable; 2] { + let mut pushes = [[0; 64]; 2]; + for index in 0..64 { + let square = 1u64 << index; + pushes[Color::White as usize][index] = (square << 8) | ((square & RANK_2) << 16); + pushes[Color::Black as usize][index] = (square >> 8) | ((square & RANK_6) >> 16); + } + pushes + } + fn precompute_knight_attacks() -> AttackTable { let mut attacks = [0; 64]; for index in 0..64 { @@ -307,6 +324,15 @@ mod tests { } #[test] + fn test_pawn_pushes() { + let pushes = Attacks::precompute_pawn_pushes(); + assert_eq!(pushes[Color::White as usize][Square::E4 as usize], 1 << Square::E5 as usize); + assert_eq!(pushes[Color::White as usize][Square::A2 as usize], 1 << Square::A3 as usize | 1 << Square::A4 as usize); + assert_eq!(pushes[Color::Black as usize][Square::E4 as usize], 1 << Square::E3 as usize); + assert_eq!(pushes[Color::Black as usize][Square::H6 as usize], 1 << Square::H5 as usize | 1 << Square::H4 as usize); + } + + #[test] fn test_knight_attacks() { let attacks = Attacks::precompute_knight_attacks(); let e4_attacks = attacks[Square::E4 as usize]; |