~macaptain/lkdo-chess

ref: 0bf3bc692f7cf1486a36d984db92c580feb6634c lkdo-chess/src/attack.zig -rw-r--r-- 943 bytes
0bf3bc69Michael Captain Add pawn attack tables and tests 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
pub usingnamespace @import("board.zig");

const mask_not_a = 0xfefefefefefefefe;
const mask_not_h = 0x7f7f7f7f7f7f7f7f;

const pawn_attacks = initPawnAttacks();

fn initPawnAttacks() [2][64]u64 {
    var sq: u6 = 0;
    var attacks: [2][64]u64 = undefined;
    while (sq < 63) : (sq += 1) {
        attacks[0][sq] = maskPawnAttacks(Side.White, @intToEnum(Square, sq));
    }
    sq = 0;
    while (sq < 63) : (sq += 1) {
        attacks[1][sq] = maskPawnAttacks(Side.Black, @intToEnum(Square, sq));
    }
    return attacks;
}

fn maskPawnAttacks(side: Side, pawn: Square) u64 {
    const bitboard = bitboardFromSquare(pawn);
    return switch (side) {
        Side.White => (bitboard >> 9 & mask_not_h) | (bitboard >> 7 & mask_not_a),
        Side.Black => (bitboard << 9 & mask_not_a) | (bitboard << 7 & mask_not_h),
    };
}

pub fn pawnAttacks(side: Side, pawn: Square) u64 {
    return pawn_attacks[@enumToInt(side)][@enumToInt(pawn)];
}