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..a1b47be 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]; @@ -152,13 +153,32 @@ 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); + + if (board.InCheck(board.sideToMove)) { + depth++; + } else { + //Null Move Pruning + if (!doingNullMove) { + if (ply && depth >= 3 && !board.InPossibleZug(board.sideToMove)) { + Board copy = board; + copy.MakeMove(Move()); + + doingNullMove = true; + ply++; + int score = -PVS(copy, depth - 3, -beta, -beta + 1).score; + ply--; + doingNullMove = false; + + if (searchStopped) return 0; + if (score >= beta) return score; + } + } + } int score = -inf; int nodeType = AllNode; - if (board.InCheck(board.sideToMove)) depth++; - nodes++; GenerateMoves(board, board.sideToMove);