@@ 103,38 103,65 @@ pub fn maskBishopOccupancy(bishop: Square) u64 {
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) : ({
+ var r: u6 = rank;
+ var f: u6 = file;
+ while (r <= 6 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 = rank;
+ f = file;
+ while (r >= 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 = rank;
+ f = file;
+ while (r <= 6 and f >= 1) : ({
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 = rank;
+ f = file;
+ while (r >= 1 and f >= 1) : ({
r -= 1;
f -= 1;
}) {
bitboard |= @as(u64, 1) << (8 * r + f);
}
+ return resetBit(bitboard, bishop);
+}
- return bitboard;
+/// Given a square occupied by a rook, 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 maskRookOccupancy(rook: Square) u64 {
+ const sq = @enumToInt(rook);
+ const rank = sq / 8;
+ const file = sq % 8;
+ var bitboard: u64 = 0;
+ var f = file;
+ var r = rank;
+ while (r <= 6) : (r += 1) {
+ bitboard |= @as(u64, 1) << (8 * r + f);
+ }
+ r = rank;
+ while (r >= 1) : (r -= 1) {
+ bitboard |= @as(u64, 1) << (8 * r + f);
+ }
+ r = rank;
+ while (f >= 1) : (f -= 1) {
+ bitboard |= @as(u64, 1) << (8 * r + f);
+ }
+ f = file;
+ while (f <= 6) : (f += 1) {
+ bitboard |= @as(u64, 1) << (8 * r + f);
+ }
+ return resetBit(bitboard, rook);
}
@@ 129,7 129,7 @@ test "kings in corners attack only 3 squares" {
expectEqual(bitboardFromSquares(expected_a1[0..]), kingAttacks(king_a1));
}
-test "bishop in centre occupancy bits" {
+test "bishop in centre relevant occupancy bits" {
const bishop = Square.e4;
const expected_squares = [_]Square{
@@ 147,7 147,7 @@ test "bishop in centre occupancy bits" {
expectEqual(bitboardFromSquares(expected_squares[0..]), maskBishopOccupancy(bishop));
}
-test "bishop at side occupancy bits" {
+test "bishop at side relevant occupancy bits" {
const bishop = Square.h6;
const expected_squares = [_]Square{
@@ 160,3 160,63 @@ test "bishop at side occupancy bits" {
expectEqual(bitboardFromSquares(expected_squares[0..]), maskBishopOccupancy(bishop));
}
+
+test "rook in centre relevant occupancy bits" {
+ const rook = Square.d5;
+
+ const expected_squares = [_]Square{
+ Square.d6,
+ Square.d7,
+ Square.e5,
+ Square.f5,
+ Square.g5,
+ Square.d4,
+ Square.d3,
+ Square.d2,
+ Square.c5,
+ Square.b5,
+ };
+
+ expectEqual(bitboardFromSquares(expected_squares[0..]), maskRookOccupancy(rook));
+}
+
+test "rook at side relevant occupancy bits" {
+ const rook = Square.h3;
+
+ const expected_squares = [_]Square{
+ Square.h4,
+ Square.h5,
+ Square.h6,
+ Square.h7,
+ Square.h2,
+ Square.g3,
+ Square.f3,
+ Square.e3,
+ Square.d3,
+ Square.c3,
+ Square.b3,
+ };
+
+ expectEqual(bitboardFromSquares(expected_squares[0..]), maskRookOccupancy(rook));
+}
+
+test "rook in corner relevant occupancy bits" {
+ const rook = Square.a1;
+
+ const expected_squares = [_]Square{
+ Square.a2,
+ Square.a3,
+ Square.a4,
+ Square.a5,
+ Square.a6,
+ Square.a7,
+ Square.b1,
+ Square.c1,
+ Square.d1,
+ Square.e1,
+ Square.f1,
+ Square.g1,
+ };
+
+ expectEqual(bitboardFromSquares(expected_squares[0..]), maskRookOccupancy(rook));
+}