fn genPawnAttack() [@typeInfo(Colors).Enum.fields.len][@typeInfo(Square).Enum.fields.len]BoardType {
var ret: [2][64]BoardType = [_][64]BoardType{[_]BoardType{0} ** 64} ** 2;
const a_file = blk: {
var bb: BitBoard = .{};
var a_set = [_]Square{ .a8, .a7, .a6, .a5, .a4, .a3, .a2, .a1 };
bb.setSlice(&a_set);
break :blk bb.board;
};
const h_file = blk: {
var bb: BitBoard = .{};
var h_set = [_]Square{ .h8, .h7, .h6, .h5, .h4, .h3, .h2, .h1 };
bb.setSlice(&h_set);
break :blk bb.board;
};
var bb: BitBoard = .{};
for (std.enums.values(Colors)) |color| {
for (std.enums.values(Square)) |square| {
bb.board = 0;
bb.set(square);
switch (color) {
.white => {
// towards lower file
if (bb.board & a_file == 0)
ret[@enumToInt(color)][@enumToInt(square)] += bb.board >> 9;
// towards higher file
if (bb.board & h_file == 0)
ret[@enumToInt(color)][@enumToInt(square)] += bb.board >> 7;
},
.black => {
// towards higher file
if (bb.board & h_file == 0)
ret[@enumToInt(color)][@enumToInt(square)] += bb.board << 9;
// towards lower file
if (bb.board & a_file == 0)
ret[@enumToInt(color)][@enumToInt(square)] += bb.board << 7;
},
}
}
}
return ret;
}
test "PawnAttack" {
const pa = genPawnAttack();
{ // White
{
var bb: BitBoard = .{};
bb.set(.c5);
bb.set(.e5);
try std.testing.expectEqual(
bb.board,
pa[@enumToInt(Colors.white)][@enumToInt(Square.d4)],
);
}
{
// no left attack
var bb: BitBoard = .{};
bb.set(.b5);
try std.testing.expectEqual(
bb.board,
pa[@enumToInt(Colors.white)][@enumToInt(Square.a4)],
);
}
{
// no right attack
var bb: BitBoard = .{};
bb.set(.g5);
try std.testing.expectEqual(
bb.board,
pa[@enumToInt(Colors.white)][@enumToInt(Square.h4)],
);
}
}
{ // Black
{
var bb: BitBoard = .{};
bb.set(.c3);
bb.set(.e3);
try std.testing.expectEqual(
bb.board,
pa[@enumToInt(Colors.black)][@enumToInt(Square.d4)],
);
}
{
// no left attack
var bb: BitBoard = .{};
bb.set(.b3);
try std.testing.expectEqual(
bb.board,
pa[@enumToInt(Colors.black)][@enumToInt(Square.a4)],
);
}
{
// no right attack
var bb: BitBoard = .{};
bb.set(.g3);
try std.testing.expectEqual(
bb.board,
pa[@enumToInt(Colors.black)][@enumToInt(Square.h4)],
);
}
}
}