From 27f914eeafdab428a15ae20beac5af136dabbbf9 Mon Sep 17 00:00:00 2001 From: rektdie Date: Wed, 5 Mar 2025 14:32:29 +0100 Subject: [PATCH 1/3] implemented NMP --- source/board.cpp | 49 +++++++++++++++++++++++++++++++++++------------ source/board.h | 4 ++-- source/search.cpp | 23 +++++++++++++++++++++- 3 files changed, 61 insertions(+), 15 deletions(-) diff --git a/source/board.cpp b/source/board.cpp index 835f50d..f09aa42 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -3,6 +3,8 @@ #include #include "tt.h" +static inline int prevEPTarget = noEPTarget; + void Board::Init() { SetByFen(StartingFen); } @@ -10,8 +12,6 @@ void Board::Init() { void Board::Reset() { castlingRights = 0; enPassantTarget = noEPTarget; - halfMoves = 0; - fullMoves = 0; sideToMove = White; occupied = 0ULL; @@ -175,12 +175,6 @@ void Board::SetByFen(const char* fen) { const int passantRank = *fen - 49; enPassantTarget = passantFile + passantRank*8; } - else if (section == 3) { - halfMoves = (*fen) - 48; - } - else if (section == 4) { - fullMoves = (*fen) - 48; - } } break; @@ -340,14 +334,36 @@ void Board::Promote(int square, int pieceType, int color, bool isCapture) { } void Board::MakeMove(Move move) { + + // Null Move + if (!move) { + int newEpTarget = prevEPTarget; + prevEPTarget = enPassantTarget; + + sideToMove = !sideToMove; + hashKey ^= zSide; + + if (enPassantTarget != noEPTarget) { + hashKey ^= zEnPassant[enPassantTarget % 8]; + } + + if (newEpTarget != noEPTarget) { + hashKey ^= zEnPassant[newEpTarget % 8]; + } + + enPassantTarget = newEpTarget; + + return; + } + + int newEpTarget = noEPTarget; + int attackerPiece = GetPieceType(move.MoveFrom()); int attackerColor = GetPieceColor(move.MoveFrom()); int targetPiece = GetPieceType(move.MoveTo()); int direction = attackerColor ? south : north; - int newEpTarget = noEPTarget; - // Removing attacker piece from old position RemovePiece(attackerPiece, move.MoveFrom(), attackerColor); @@ -428,8 +444,6 @@ void Board::MakeMove(Move move) { // Removing the right to castle on king and rook movement UpdateCastlingRights(*this, move.MoveFrom(), attackerPiece, attackerColor); - halfMoves++; - if (halfMoves % 2 == 0) fullMoves++; sideToMove = !attackerColor; hashKey ^= zSide; @@ -445,3 +459,14 @@ void Board::MakeMove(Move move) { GenAttackMaps(*this); } + +bool Board::InPossibleZug(bool side) { + Bitboard toCheck; + + // For all pieces other than pawns and king + for (int piece = Knight; piece <= Queen; piece++) { + toCheck |= (pieces[piece] & colors[side]); + } + + return !toCheck; +} diff --git a/source/board.h b/source/board.h index 2d02777..f1e097a 100644 --- a/source/board.h +++ b/source/board.h @@ -23,8 +23,6 @@ public: int currentMoveIndex = 0; bool sideToMove = White; - int halfMoves = 0; - int fullMoves = 0; uint8_t castlingRights = 0; int enPassantTarget = noEPTarget; @@ -51,4 +49,6 @@ public: void Promote(int square, int pieceType, int color, bool isCapture); void MakeMove(Move move); + + bool InPossibleZug(bool side); }; diff --git a/source/search.cpp b/source/search.cpp index 5973400..8885bcb 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -12,6 +12,7 @@ static inline int ply = 0; static inline auto timeStart = std::chrono::high_resolution_clock::now(); static inline int timeToSearch = 0; static inline bool searchStopped = false; +static inline bool doingNullMove = false; // [id][ply] static inline int killerMoves[2][64]; @@ -157,7 +158,27 @@ SearchResults PVS(Board board, int depth, int alpha, int beta) { int score = -inf; int nodeType = AllNode; - if (board.InCheck(board.sideToMove)) depth++; + if (board.InCheck(board.sideToMove)) { + depth++; + } else { + //Null Move Pruning + if (!doingNullMove) { + if (ply && depth >= 3 && !board.InPossibleZug(board.sideToMove)) { + board.MakeMove(Move()); + + doingNullMove = true; + ply++; + score = -PVS(board, depth - 3, -beta, -beta + 1).score; + ply--; + doingNullMove = false; + + board.MakeMove(Move()); + + if (searchStopped) return 0; + if (score >= beta) return beta; + } + } + } nodes++; -- 2.47.3 From 58da8b52fad4abed681e22bdc89bcf35f664577b Mon Sep 17 00:00:00 2001 From: rektdie Date: Wed, 5 Mar 2025 15:00:15 +0100 Subject: [PATCH 2/3] depth could go under 0 which I didnt account for --- source/search.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/search.cpp b/source/search.cpp index 8885bcb..510cb42 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -153,7 +153,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta) { } } - if (depth == 0) return Quiescence(board, alpha, beta); + if (depth <= 0) return Quiescence(board, alpha, beta); int score = -inf; int nodeType = AllNode; -- 2.47.3 From 1f868f07d2f2c64875c1456942f4398ae9ea47cb Mon Sep 17 00:00:00 2001 From: rektdie Date: Thu, 6 Mar 2025 05:43:30 +0100 Subject: [PATCH 3/3] doing copy make in NMP --- source/search.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/source/search.cpp b/source/search.cpp index 510cb42..a1b47be 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -155,31 +155,30 @@ SearchResults PVS(Board board, int depth, int alpha, int beta) { if (depth <= 0) return Quiescence(board, alpha, beta); - int score = -inf; - int nodeType = AllNode; - if (board.InCheck(board.sideToMove)) { depth++; } else { //Null Move Pruning if (!doingNullMove) { if (ply && depth >= 3 && !board.InPossibleZug(board.sideToMove)) { - board.MakeMove(Move()); + Board copy = board; + copy.MakeMove(Move()); doingNullMove = true; ply++; - score = -PVS(board, depth - 3, -beta, -beta + 1).score; + int score = -PVS(copy, depth - 3, -beta, -beta + 1).score; ply--; doingNullMove = false; - board.MakeMove(Move()); - if (searchStopped) return 0; - if (score >= beta) return beta; + if (score >= beta) return score; } } } + int score = -inf; + int nodeType = AllNode; + nodes++; GenerateMoves(board, board.sideToMove); -- 2.47.3