From dd7525ce7009b586079db00596ea4c67dc8d45da Mon Sep 17 00:00:00 2001
From: Michael Captain
Date: Sun, 4 Apr 2021 18:13:07 +0300
Subject: [PATCH] Add bishop relative occupancy mask
---
src/attack.zig | 44 ++++++++++++++++++++++++++++++++++++++++++++
test/attack.zig | 32 ++++++++++++++++++++++++++++++++
2 files changed, 76 insertions(+)
diff --git a/src/attack.zig b/src/attack.zig
index c5696da..8d12d22 100644
--- a/src/attack.zig
+++ b/src/attack.zig
@@ -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;
+}
diff --git a/test/attack.zig b/test/attack.zig
index 53c8d33..3cc0829 100644
--- a/test/attack.zig
+++ b/test/attack.zig
@@ -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));
+}
--
2.32.0