aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam <adammegarules1@gmail.com>2026-08-18 13:47:17 +0200
committerAdam <adammegarules1@gmail.com>2026-08-18 13:47:17 +0200
commit777e649fbf92057dbcdce0c291dbec46114d665e (patch)
treefe6950ae92718619ebe37f38da11fdbc211eecf4
parent56cbe07135f1b235747ea90f7308948fdd08abd0 (diff)
refactor(board): making king use same bitboard precompute bitboard style logic as knights
-rw-r--r--src/moves.cpp57
1 files changed, 39 insertions, 18 deletions
diff --git a/src/moves.cpp b/src/moves.cpp
index ac6e5cb..0b8230f 100644
--- a/src/moves.cpp
+++ b/src/moves.cpp
@@ -63,11 +63,34 @@ constexpr std::array<std::array<std::uint64_t, 64>, 2> computePawnAttacks() {
}
return attacks;
}
+constexpr std::array<std::uint64_t, 64> computeKingAttacks() {
+ std::array<std::uint64_t, 64> attacks{};
+ constexpr std::array<int, 8> king_offsets{-1, 1, 8, -8, -9, 9, -7, 7};
+
+ for (int i = 0; i < 64; i++) {
+ uint64_t bb = 0;
+ for (int offset : king_offsets) {
+ int next = i + offset;
+ if (next >= 64 || next < 0) {
+ continue;
+ }
+ const int fileDelta = (next % 8) - (i % 8);
+ if (fileDelta < -1 || fileDelta > 1) {
+ continue;
+ };
+ bb |= 1ULL << next;
+ }
+ attacks[static_cast<size_t>(i)] = bb;
+ }
+ return attacks;
+}
constexpr std::array<std::uint64_t, 64> KNIGHT_ATTACKS = computeKnightAttacks();
constexpr std::array<std::array<std::uint64_t, 64>, 2> PAWN_ATTACKS =
computePawnAttacks();
+constexpr std::array<std::uint64_t, 64> KING_ATTACKS = computeKingAttacks();
+
static void GenerateKnightMoves(const uint8_t &from, const Game &g,
std::vector<uint16_t> &moves,
const move_generate_options &options) {
@@ -79,11 +102,11 @@ static void GenerateKnightMoves(const uint8_t &from, const Game &g,
bool hasFriendlyPiece = g.turn
? (g.WhitePieceBitboard & (1ULL << next)) > 0
: (g.BlackPieceBitboard & (1ULL << next)) > 0;
-
- bool isCaptuare = (g.PieceBitboard & (1ULL << next)) > 0;
if (hasFriendlyPiece) {
continue;
}
+
+ bool isCaptuare = (g.PieceBitboard & (1ULL << next)) > 0;
if (options == NON_CAPTUARES_ONLY && isCaptuare) {
continue;
}
@@ -192,28 +215,26 @@ static void GenerateKingMoves(const Game &g, const uint8_t &from,
Assert_message();
exit(1);
}
- constexpr std::array<int, 8> king_offsets{-1, 1, 8, -8, -9, 9, -7, 7};
- for (int offset : king_offsets) {
- auto next = static_cast<uint8_t>(from + offset);
- if (next >= 64) {
- continue;
- }
- if (std::abs((next % 8) - (from % 8)) > 1) {
+ uint64_t king_attacks = KING_ATTACKS[static_cast<size_t>(from)];
+ while (king_attacks != 0) {
+ auto next = static_cast<uint8_t>(__builtin_ctzll(king_attacks));
+ king_attacks &= king_attacks - 1;
+
+ bool hasFriendlyPiece = g.turn
+ ? (g.WhitePieceBitboard & (1ULL << next)) > 0
+ : (g.BlackPieceBitboard & (1ULL << next)) > 0;
+ if (hasFriendlyPiece) {
continue;
- };
- if (g.pieces[next].type != NONEPIECE) {
- if (g.pieces[next].color == g.turn) {
- continue;
- };
}
- if (options == CAPTUARES_ONLY && g.pieces[next].type == NONEPIECE) {
+
+ bool isCaptuare = (g.PieceBitboard & (1ULL << next)) > 0;
+ if (options == NON_CAPTUARES_ONLY && isCaptuare) {
continue;
}
- if (options == NON_CAPTUARES_ONLY && g.pieces[next].type != NONEPIECE) {
+ if (options == CAPTUARES_ONLY && !isCaptuare) {
continue;
}
-
- moves.push_back(CreateMove(static_cast<uint8_t>(from), next));
+ moves.push_back(CreateMove(from, next));
}
};