fn genBishopAttacks() [@typeInfo(Square).Enum.fields.len]BoardType {
const moves = [_][2]i2{
.{ -1, -1 },
.{ -1, 1 },
.{ 1, -1 },
.{ 1, 1 },
};
var ret: [64]BoardType = [_]BoardType{0} ** 64;
for (std.enums.values(Square)) |square| {
const rank = @intCast(isize, SIZE - square.int() / SIZE);
const file = @intCast(isize, square.int() % SIZE);
var attacks: BoardType = 0;
var step: i5 = 1;
while (step < SIZE - 1) : (step += 1) {
for (moves) |m| {
const dr = rank + m[0] * step;
const df = file + m[1] * step;
if (dr < 2 or dr > 7) continue;
if (df < 1 or df > 7) continue;
const s = @intCast(u6, (SIZE - dr) * SIZE + df);
attacks |= @intCast(BoardType, @as(BoardType, 1) << s);
}
}
ret[@enumToInt(square)] = attacks;
}
return ret;
}
test "BishopAttacks" {
const ba = genBishopAttacks();
{
var bb: BitBoard = .{};
var set = [_]Square{ .b2, .c3, .d4, .e5, .f6, .g7 };
bb.setSlice(&set);
try std.testing.expectEqual(
bb.board,
ba[@enumToInt(Square.a1)],
);
}
}