~macaptain/lkdo-chess

dd7525ce7009b586079db00596ea4c67dc8d45da — Michael Captain 3 years ago 4a145c4
Add bishop relative occupancy mask
2 files changed, 76 insertions(+), 0 deletions(-)

M src/attack.zig
M test/attack.zig
M src/attack.zig => src/attack.zig +44 -0
@@ 94,3 94,47 @@ fn maskKingAttacks(king: Square) u64 {
pub fn kingAttacks(king: Square) u64 {
    return king_attacks[@enumToInt(king)];
}

/// Given a square occupied by a bishop, return a bitboard of relevant
/// occupancy bits.  i.e. squares on which, if other pieces are placed, the
/// line of attack is blocked and the attack bitboard is altered.
pub fn maskBishopOccupancy(bishop: Square) u64 {
    const sq = @enumToInt(bishop);
    const rank = sq / 8;
    const file = sq % 8;
    var bitboard: u64 = 0;
    var r: u6 = rank + 1;
    var f: u6 = file + 1;
    while (r >= 1 and r <= 6 and f >= 1 and f <= 6) : ({
        r += 1;
        f += 1;
    }) {
        bitboard |= @as(u64, 1) << (8 * r + f);
    }
    r = rank - 1;
    f = file + 1;
    while (r >= 1 and r <= 6 and f >= 1 and f <= 6) : ({
        r -= 1;
        f += 1;
    }) {
        bitboard |= @as(u64, 1) << (8 * r + f);
    }
    r = rank + 1;
    f = file - 1;
    while (r >= 1 and r <= 6 and f >= 1 and f <= 6) : ({
        r += 1;
        f -= 1;
    }) {
        bitboard |= @as(u64, 1) << (8 * r + f);
    }
    r = rank - 1;
    f = file - 1;
    while (r >= 1 and r <= 6 and f >= 1 and f <= 6) : ({
        r -= 1;
        f -= 1;
    }) {
        bitboard |= @as(u64, 1) << (8 * r + f);
    }

    return bitboard;
}

M test/attack.zig => test/attack.zig +32 -0
@@ 128,3 128,35 @@ test "kings in corners attack only 3 squares" {
    expectEqual(bitboardFromSquares(expected_h1[0..]), kingAttacks(king_h1));
    expectEqual(bitboardFromSquares(expected_a1[0..]), kingAttacks(king_a1));
}

test "bishop in centre occupancy bits" {
    const bishop = Square.e4;

    const expected_squares = [_]Square{
        Square.f5,
        Square.g6,
        Square.f3,
        Square.g2,
        Square.d3,
        Square.c2,
        Square.d5,
        Square.c6,
        Square.b7,
    };

    expectEqual(bitboardFromSquares(expected_squares[0..]), maskBishopOccupancy(bishop));
}

test "bishop at side occupancy bits" {
    const bishop = Square.h6;

    const expected_squares = [_]Square{
        Square.g5,
        Square.f4,
        Square.e3,
        Square.d2,
        Square.g7,
    };

    expectEqual(bitboardFromSquares(expected_squares[0..]), maskBishopOccupancy(bishop));
}