diff --git a/Scripts/game.pgn b/Scripts/game.pgn index a3409e7..f3dd06c 100644 --- a/Scripts/game.pgn +++ b/Scripts/game.pgn @@ -1,59 +1,33 @@ [Event "?"] [Site "?"] -[Date "2025.09.04"] +[Date "2026.02.19"] [Round "1"] -[White "Eleanor-base"] -[Black "Eleanor-dev"] -[Result "1-0"] -[FEN "rn1qk1nr/ppp2pRp/3bp3/3p4/3P4/2P2P1P/PP2PP2/RNBQKB2 b Qkq - 0 1"] -[GameDuration "00:01:31"] -[GameEndTime "2025-09-04T22:39:24.808 CEST"] -[GameStartTime "2025-09-04T22:37:53.089 CEST"] -[PlyCount "84"] +[White "Eleanor-dev"] +[Black "Eleanor-base"] +[Result "0-1"] +[FEN "rnbqkbnr/1pp2pp1/p2pp3/7p/2P5/5NP1/PP1PPPBP/RNBQ1RK1 b kq - 0 1"] +[GameDuration "00:00:09"] +[GameEndTime "2026-02-19T14:23:45.385 UTC"] +[GameStartTime "2026-02-19T14:23:36.234 UTC"] +[PlyCount "33"] [SetUp "1"] [Termination "abandoned"] -[TimeControl "37.56+0.38"] +[TimeControl "7.42+0.07"] -1... Ne7 {-1.65 14/26 1861 1544983} 2. Rg1 {+1.64 12/23 2087 934687} -c5 {-1.74 15/32 1565 1167061} 3. f4 {+1.61 13/27 2002 772197} -Nd7 {-1.59 15/31 1455 1090717} 4. e3 {+1.84 14/31 1753 1387350} -Qb6 {-1.65 14/31 1385 1273463} 5. a4 {+1.98 14/28 1505 1375155} -Qc7 {-2.11 14/29 1849 830961} 6. dxc5 {+1.98 15/30 1571 1101192} -Nxc5 {-2.16 15/34 1778 545375} 7. a5 {+1.90 14/29 1555 1021903} -a6 {-2.22 15/33 1706 667700} 8. Nd2 {+2.38 14/29 1267 922229} -O-O-O {-2.34 14/31 1641 580655} 9. b4 {+2.62 14/31 1463 975771} -Ne4 {-2.37 15/39 1336 932258} 10. Nxe4 {+2.55 13/31 1579 228776} -dxe4 {-2.85 15/31 1004 694847} 11. Bd2 {+3.02 13/30 1247 753167} -Ng6 {-2.40 14/35 1118 862794} 12. Qb1 {+2.40 12/26 1475 339681} -f5 {-1.97 13/31 1465 304169} 13. Qb3 {+2.25 12/34 957 597544} -Nh4 {-2.21 14/38 1409 432849} 14. Be2 {+2.30 13/35 1343 843064} -Nf3+ {-2.18 14/40 1355 318067} 15. Bxf3 {+2.49 14/39 1346 645681} -exf3 {-2.59 15/30 1115 864898} 16. O-O-O {+2.85 14/35 1297 425564} -Rhe8 {-2.11 13/34 1269 275858} 17. c4 {+2.35 13/30 1249 86929} -e5 {-1.53 14/40 1188 1007892} 18. Bc3 {+2.71 12/37 1207 154226} -exf4 {-0.71 13/45 1186 280646} 19. c5 {+0.71 13/34 883 618110} -Be5 {-0.52 13/65 1145 440206} 20. Rxd8+ {+0.56 13/36 861 757974} -Qxd8 {-0.55 15/56 1106 373775} 21. Bxe5 {+0.98 14/49 845 689690} -Rxe5 {-0.87 14/83 799 511973} 22. exf4 {+0.98 13/36 1092 370730} -Re2 {-0.87 13/89 751 367978} 23. Rg8 {+1.05 13/43 1056 334030} -Re8 {-0.87 13/79 1030 242199} 24. Rxe8 {+1.13 12/34 1023 141620} -Qxe8 {-0.87 13/41 998 22345} 25. Qd1 {+1.46 11/36 991 227470} -Qe4 {-0.89 12/59 968 192246} 26. Qd2 {+1.17 13/42 856 592181} -Qc4+ {-0.90 11/95 891 510827} 27. Kb2 {+1.46 12/38 937 154015} -h5 {-1.01 11/66 913 404234} 28. Qc3 {+1.38 13/37 909 437481} -Qe2+ {-0.73 10/76 886 55851} 29. Qc2 {+1.42 14/34 882 272787} -Qe4 {-1.19 10/49 860 32225} 30. Kb3 {+1.27 12/37 824 598253} -Qxf4 {-1.13 10/98 838 184366} 31. c6 {+1.24 12/51 734 627843} -Qe4 {-0.96 10/103 814 271816} 32. cxb7+ {+1.24 13/50 817 324130} -Kxb7 {-0.96 9/16 792 1604} 33. Qc3 {+1.24 14/30 796 171391} -Qd5+ {-1.20 8/35 772 15385} 34. Ka3 {+1.23 11/40 525 374116} -Qe4 {-1.25 8/85 752 9999} 35. Qd2 {+1.19 12/37 767 281732} -Kc7 {-1.09 10/65 733 175302} 36. Qb2 {+1.21 12/40 748 316739} -h4 {-1.09 7/48 715 39908} 37. Qc1+ {+1.19 11/38 529 523703} -Kd8 {-0.65 10/83 700 298167} 38. Qc3 {+1.26 12/49 723 322911} -Kd7 {-0.64 9/44 684 18906} 39. Qg7+ {+1.23 11/37 537 395773} -Kc8 {-1.04 8/14 667 847} 40. Qf7 {+1.23 11/45 572 501521} -Qd3+ {-1.04 7/81 653 28141} 41. Qb3 {+1.23 11/36 689 130089} -Qe2 {-1.28 8/54 639 10579} 42. Qc3+ {+1.08 11/38 581 477548} -Kd7 {-0.67 8/40 627 10552} 43. Qb2 {+1.08 11/33 663 104343, Black disconnects} -1-0 +1... Be7 {-2.14 13/26 298 257201} 2. h3 {+2.42 13/23 411 242566} +c5 {-1.86 14/28 322 258868} 3. d4 {+1.91 13/28 393 140944} +cxd4 {-2.02 14/28 292 226207} 4. Nxd4 {+1.78 14/26 369 310818} +Nf6 {-1.87 14/27 259 202951} 5. Nc3 {+1.74 14/28 262 199377} +Qc7 {-1.87 14/24 376 53755} 6. Bg5 {+1.96 14/27 260 198748} +Nc6 {-2.24 14/26 303 221514} 7. Rc1 {+1.96 13/26 346 111270} +Bd7 {-2.03 14/27 339 133181} 8. e3 {+2.23 13/27 329 122851} +Rc8 {-2.30 12/21 339 87727} 9. b3 {+1.79 14/24 279 223142} +Qa5 {-1.93 14/28 311 195408} 10. Bxf6 {+1.97 14/26 230 176615} +Bxf6 {-1.71 13/30 163 111085} 11. Ne4 {+1.38 14/28 274 214328} +Be7 {-1.52 14/27 255 187927} 12. Nxd6+ {+1.73 14/37 263 224669} +Bxd6 {-1.44 15/28 140 104101} 13. Nxc6 {+1.73 16/37 182 164873} +Bxc6 {-1.74 16/34 174 125622} 14. Qxd6 {+1.69 17/36 271 98496} +Bxg2 {-1.66 16/39 277 79460} 15. Kxg2 {+1.53 15/27 159 151849} +Rd8 {-1.44 16/31 175 147340} 16. b4 {+1.57 16/28 260 125736} +Rxd6 {-1.28 15/24 275 93890} 17. bxa5 {+1.46 14/31 249 87997} +Ke7 {-1.50 16/30 254 168346, White disconnects} 0-1 diff --git a/source/board.cpp b/source/board.cpp index bdb8a16..c060c4e 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "utils.h" void Board::Reset() { @@ -16,6 +17,9 @@ void Board::Reset() { sideToMove = White; occupied = 0ULL; + checkers = 0ULL; + pinned = std::array(); + pieces = std::array(); colors = std::array(); @@ -83,6 +87,8 @@ void Board::SetByFen(std::string_view fen) { occupied = colors[White] | colors[Black]; hashKey = UTILS::GetHashKey(*this); + checkers = CalcCheckers(); + pinned = {CalcPinned(White), CalcPinned(Black)}; MOVEGEN::GenThreatMaps(*this); MOVEGEN::GenerateMoves(*this, true); @@ -246,11 +252,11 @@ void Board::ResetMoves() { void Board::ListMoves() { int moveCount = 1; for (int i = 0; i < currentMoveIndex; i++) { - Board copy = *this; - bool isLegal = copy.MakeMove(moveList[i]); + if (!IsLegal(moveList[i])) continue; + + Board copy = *this; + copy.MakeMove(moveList[i]); - if (!isLegal) - continue; std::cout << moveCount << ". "; moveList[i].PrintMove(); @@ -342,8 +348,7 @@ void Board::Promote(int square, int pieceType, int color, bool isCapture) { SetPiece(pieceType, square, color); } -bool Board::MakeMove(Move move) { - Board save = *this; +void Board::MakeMove(Move move) { // Null Move if (!move) { int newEpTarget = noEPTarget; @@ -357,7 +362,7 @@ bool Board::MakeMove(Move move) { enPassantTarget = newEpTarget; - return true; + return; } int newEpTarget = noEPTarget; @@ -481,13 +486,16 @@ bool Board::MakeMove(Move move) { enPassantTarget = newEpTarget; MOVEGEN::GenThreatMaps(*this); + pinned = {CalcPinned(White), CalcPinned(Black)}; + checkers = CalcCheckers(); + /* sideToMove = !sideToMove; if (InCheck()) { - *this = save; return false; } sideToMove = !sideToMove; + */ if (attackerColor == Black) fullMoves++; if (attackerPiece == Pawn || move.IsCapture()) { @@ -497,8 +505,6 @@ bool Board::MakeMove(Move move) { } positionIndex++; - - return true; } bool Board::InPossibleZug() { @@ -577,6 +583,146 @@ Bitboard Board::AttacksTo(int square, Bitboard occupancy) { return attacks; } +static Bitboard rayBetween(int sq1, int sq2) { + if (sq1 == sq2) return 0ULL; + + int f1 = sq1 % 8, r1 = sq1 / 8; + int f2 = sq2 % 8, r2 = sq2 / 8; + + int df = (f2 > f1) - (f2 < f1); + int dr = (r2 > r1) - (r2 < r1); + + // must be same rank, file, or diagonal + if (df != 0 && dr != 0 && std::abs(f2 - f1) != std::abs(r2 - r1)) + return 0ULL; + + Bitboard ray = 0ULL; + + int f = f1 + df; + int r = r1 + dr; + + while (f != f2 || r != r2) { + ray |= 1ULL << (r * 8 + f); + f += df; + r += dr; + } + + ray &= ~((1ULL << sq1) | (1ULL << sq2)); + + return ray; +} + +// Returns the full ray from `from` in the direction of `to`, to the edge of the board +static Bitboard fullRay(int from, int to) { + int df = (to % 8) - (from % 8); + int dr = (to / 8) - (from / 8); + + if (df != 0) df /= std::abs(df); + if (dr != 0) dr /= std::abs(dr); + + Bitboard ray = 0ULL; + + int f = from % 8 + df; + int r = from / 8 + dr; + while (f >= 0 && f < 8 && r >= 0 && r < 8) { + ray |= 1ULL << (r * 8 + f); + f += df; + r += dr; + } + + f = from % 8 - df; + r = from / 8 - dr; + while (f >= 0 && f < 8 && r >= 0 && r < 8) { + ray |= 1ULL << (r * 8 + f); + f -= df; + r -= dr; + } + + ray |= 1ULL << from; + return ray; +} + bool Board::IsSquareThreatened(bool side, int square) { return colorThreats[!side].IsSet(square); } + +Bitboard Board::CalcCheckers() { + return AttacksTo((colors[sideToMove] & pieces[King]).getLS1BIndex(), occupied) & colors[!sideToMove]; +} + +Bitboard Board::CalcPinned(bool color) { + Bitboard pinned; + + int kingSquare = (pieces[King] & colors[color]).getLS1BIndex(); + + assert(kingSquare != -1); + + Bitboard oppQueens = pieces[Queen] & colors[!color]; + + Bitboard potentialAttackers = MOVEGEN::getBishopAttack(kingSquare, colors[!color]) & (oppQueens | (pieces[Bishop] & colors[!color])) + | MOVEGEN::getRookAttack(kingSquare, colors[!color]) & (oppQueens | (pieces[Rook] & colors[!color])); + + while (potentialAttackers) { + int attackerSquare = potentialAttackers.getLS1BIndex(); + + Bitboard isPinned = colors[color] & rayBetween(attackerSquare, kingSquare); + + if (isPinned.PopCount() == 1) + pinned |= isPinned; + + potentialAttackers.PopBit(attackerSquare); + } + + return pinned; +} + +bool Board::IsLegal(Move &move) { + assert(move != 0); + + int us = sideToMove; + int them = !us; + + int from = move.MoveFrom(); + int to = move.MoveTo(); + + Bitboard king = pieces[King] & colors[us]; + int kingSquare = king.getLS1BIndex(); + + int moveType = move.GetFlags(); + + if (moveType == epCapture) { + int epCapturedSquare = to + (us == White ? -8 : 8); + Bitboard occAfterEP = occupied ^ Bitboard::GetSquare(from) ^ Bitboard::GetSquare(to) + ^ Bitboard::GetSquare(epCapturedSquare); + + Bitboard theirQueens = pieces[Queen] & colors[them]; + + return (MOVEGEN::getBishopAttack(kingSquare, occAfterEP) & (theirQueens | (pieces[Bishop] & colors[them]))).PopCount() < 1 + && (MOVEGEN::getRookAttack(kingSquare, occAfterEP) & (theirQueens | (pieces[Rook] & colors[them]))).PopCount() < 1; + } + + int movingPiece = GetPieceType(from); + + if (movingPiece == King) { + Bitboard kinglessOcc = occupied ^ king; + Bitboard theirQueens = pieces[Queen] & colors[them]; + + return !colorThreats[them].IsSet(to) + && (MOVEGEN::getBishopAttack(to, kinglessOcc) & (theirQueens | (pieces[Bishop] & colors[them]))).PopCount() == 0 + && (MOVEGEN::getRookAttack(to, kinglessOcc) & (theirQueens | (pieces[Rook] & colors[them]))).PopCount() == 0; + } + + if (checkers.PopCount() > 1) { + return false; + } + + if (pinned[us].IsSet(from) && !fullRay(kingSquare, from).IsSet(to)) + return false; + + if (checkers.PopCount() < 1) + return true; + + int checkerSquare = checkers.getLS1BIndex(); + + return (rayBetween(kingSquare, checkerSquare) | checkers).IsSet(to); +} diff --git a/source/board.h b/source/board.h index e254044..6ecda1b 100644 --- a/source/board.h +++ b/source/board.h @@ -21,6 +21,9 @@ public: Bitboard occupied; + std::array pinned; + Bitboard checkers; + ACC::AccumulatorPair accPair; std::array pieceThreats; @@ -65,11 +68,16 @@ public: void RemovePiece(int piece, int square, bool color); void Promote(int square, int pieceType, int color, bool isCapture); - bool MakeMove(Move move); + void MakeMove(Move move); bool InPossibleZug(); Bitboard AttacksTo(int square, Bitboard occupancy); bool IsSquareThreatened(bool side, int square); + + Bitboard CalcCheckers(); + Bitboard CalcPinned(bool color); + + bool IsLegal(Move &move); }; diff --git a/source/datagen.cpp b/source/datagen.cpp index 8fc0c1c..dad1684 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -206,10 +206,11 @@ static bool IsGameOver(Board &board, SEARCH::SearchContext* ctx) { int moveSeen = 0; for (int i = 0; i < board.currentMoveIndex; i++) { - Board copy = board; - bool isLegal = copy.MakeMove(board.moveList[i]); - if (!isLegal) continue; + if (!board.IsLegal(board.moveList[i])) continue; + Board copy = board; + copy.MakeMove(board.moveList[i]); + moveSeen++; } @@ -227,12 +228,14 @@ static void PlayRandMoves(Board &board, SEARCH::SearchContext* ctx) { MOVEGEN::GenerateMoves(board, true); std::uniform_int_distribution dist(0, board.currentMoveIndex - 1); - bool isLegal = board.MakeMove(board.moveList[dist(rng)]); - if (!isLegal) { + Move currMove = board.moveList[dist(rng)]; + if (!board.IsLegal(currMove)) { count--; continue; } + board.MakeMove(currMove); + ctx->positionHistory[board.positionIndex] = board.hashKey; MOVEGEN::GenerateMoves(board, true); if (IsGameOver(board, ctx)) break; diff --git a/source/move.h b/source/move.h index 5d8ac1b..6c046ad 100644 --- a/source/move.h +++ b/source/move.h @@ -64,6 +64,10 @@ public: return (GetFlags() >= knightPromotion); } + bool IsCastle() { + return (GetFlags() == kingCastle || GetFlags() == queenCastle); + } + void PrintMove() { std::cout << squareCoords[MoveFrom()] << squareCoords[MoveTo()]; diff --git a/source/perft.cpp b/source/perft.cpp index 263fd2a..41f9446 100644 --- a/source/perft.cpp +++ b/source/perft.cpp @@ -1,8 +1,9 @@ #include "perft.h" #include "stopwatch.h" #include "movegen.h" +#include -static U64 HelperPerft(Board board, int depth) { +static U64 HelperPerft(Board &board, int depth) { MOVEGEN::GenerateMoves(board, true); if (depth == 0) return 1ULL; @@ -10,24 +11,27 @@ static U64 HelperPerft(Board board, int depth) { U64 nodes = 0; for (int i = 0; i < board.currentMoveIndex; i++) { + if (!board.IsLegal(board.moveList[i])) continue; + Board copy = board; - bool isLegal = copy.MakeMove(board.moveList[i]); - if (!isLegal) continue; + copy.MakeMove(board.moveList[i]); + nodes += HelperPerft(copy, depth - 1); } return nodes; } void Perft(Board &board, int depth) { + //board.SetByFen(StartingFen); MOVEGEN::GenerateMoves(board, true); U64 totalNodes = 0; Stopwatch sw; for (int i = 0; i < board.currentMoveIndex; i++) { + if (!board.IsLegal(board.moveList[i])) continue; Board copy = board; - bool isLegal = copy.MakeMove(board.moveList[i]); - if (!isLegal) continue; + copy.MakeMove(board.moveList[i]); U64 nodeCount = HelperPerft(copy, depth - 1); totalNodes += nodeCount; board.moveList[i].PrintMove(); diff --git a/source/search.cpp b/source/search.cpp index 455a2c1..d0c36b4 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -358,9 +358,9 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear continue; } + if (!board.IsLegal(board.moveList[i])) continue; Board copy = board; - int isLegal = copy.MakeMove(board.moveList[i]); - if (!isLegal) continue; + copy.MakeMove(board.moveList[i]); ctx->ss[ply].pieceType = board.GetPieceType(board.moveList[i].MoveFrom()); ctx->ss[ply].moveTo = board.moveList[i].MoveTo(); @@ -523,10 +523,9 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC if (!SEE(board, currMove, seeThreshold)) continue; + if (!board.IsLegal(currMove)) continue; Board copy = board; - bool isLegal = copy.MakeMove(currMove); - - if (!isLegal) continue; + copy.MakeMove(currMove); if (copy.positionIndex >= ctx->positionHistory.size()) { ctx->positionHistory.resize(copy.positionIndex + 100); @@ -610,10 +609,9 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC continue; } + if (!board.IsLegal(currMove)) continue; Board copy = board; - bool isLegal = copy.MakeMove(currMove); - - if (!isLegal) continue; + copy.MakeMove(currMove); ctx->ss[ply].pieceType = board.GetPieceType(currMove.MoveFrom()); ctx->ss[ply].moveTo = currMove.MoveTo();