From e9e7e1ebd00574b7ce36e8bff9c77f8ce57b0bb3 Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 11 Aug 2026 10:55:26 +0200 Subject: removing hard coded knight moves --- src/moves.cpp | 55 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/moves.cpp b/src/moves.cpp index a130055..52c8d2f 100644 --- a/src/moves.cpp +++ b/src/moves.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -9,29 +10,31 @@ #include "board/board.hpp" #include "moves.hpp" -constexpr std::array KNIGHT_ATTACKS = { - 0x0000000000020400ULL, 0x0000000000050800ULL, 0x00000000000A1100ULL, - 0x0000000000142200ULL, 0x0000000000284400ULL, 0x0000000000508800ULL, - 0x0000000000A01000ULL, 0x0000000000402000ULL, 0x0000000002040004ULL, - 0x0000000005080008ULL, 0x000000000A110011ULL, 0x0000000014220022ULL, - 0x0000000028440044ULL, 0x0000000050880088ULL, 0x00000000A0100010ULL, - 0x0000000040200020ULL, 0x0000000204000402ULL, 0x0000000508000805ULL, - 0x0000000A1100110AULL, 0x0000001422002214ULL, 0x0000002844004428ULL, - 0x0000005088008850ULL, 0x000000A0100010A0ULL, 0x0000004020002040ULL, - 0x0000020400040200ULL, 0x0000050800080500ULL, 0x00000A1100110A00ULL, - 0x0000142200221400ULL, 0x0000284400442800ULL, 0x0000508800885000ULL, - 0x0000A0100010A000ULL, 0x0000402000204000ULL, 0x0002040004020000ULL, - 0x0005080008050000ULL, 0x000A1100110A0000ULL, 0x0014220022140000ULL, - 0x0028440044280000ULL, 0x0050880088500000ULL, 0x00A0100010A00000ULL, - 0x0040200020400000ULL, 0x0204000402000000ULL, 0x0508000805000000ULL, - 0x0A1100110A000000ULL, 0x1422002214000000ULL, 0x2844004428000000ULL, - 0x5088008850000000ULL, 0xA0100010A0000000ULL, 0x4020002040000000ULL, - 0x0400040200000000ULL, 0x0800080500000000ULL, 0x1100110A00000000ULL, - 0x2200221400000000ULL, 0x4400442800000000ULL, 0x8800885000000000ULL, - 0x100010A000000000ULL, 0x2000204000000000ULL, 0x0004020000000000ULL, - 0x0008050000000000ULL, 0x00110A0000000000ULL, 0x0022140000000000ULL, - 0x0044280000000000ULL, 0x0088500000000000ULL, 0x0010A00000000000ULL, - 0x0020400000000000ULL}; +constexpr std::array computeKnightAttacks() { + std::array attacks{}; + + for (int sq = 0; sq < 64; ++sq) { + const int f = sq % 8; // file (0=a, 7=h) + const int r = sq / 8; // rank (0=1, 7=8) + std::uint64_t bb = 0; + + // {df, dr} for all 8 knight leaps + const int leaps[8][2] = {{1, 2}, {2, 1}, {2, -1}, {1, -2}, + {-1, -2}, {-2, -1}, {-2, 1}, {-1, 2}}; + + for (int i = 0; i < 8; ++i) { + const int nf = f + leaps[i][0]; + const int nr = r + leaps[i][1]; + if (nf >= 0 && nf < 8 && nr >= 0 && nr < 8) { + bb |= 1ULL << (nr * 8 + nf); + } + } + attacks[static_cast(sq)] = bb; + } + return attacks; +} + +constexpr std::array KNIGHT_ATTACKS = computeKnightAttacks(); static void GenerateKnightMoves(Game *b, std::vector &moves, bool GenerateQuietMoves) { @@ -44,17 +47,21 @@ static void GenerateKnightMoves(Game *b, std::vector &moves, int next = __builtin_ctzll(knight_attacks); knight_attacks &= knight_attacks - 1; + // maybe remove this line of code if (next >= 64 || next < 0) { continue; } + + // maybe also remove this line of code const int fileDelta = std::abs((next % 8) - (from % 8)); if (fileDelta != 1 && fileDelta != 2) { continue; }; + bool hasFriendlyPiece = b->turn ? (b->WhitePieceBitboard & (1ULL << next)) > 0 : (b->BlackPieceBitboard & (1ULL << next)) > 0; - + bool isCaptuare = (b->PieceBitboard & (1ULL << next)) > 0; if (hasFriendlyPiece) { continue; -- cgit v1.2.3