implemented NMP

This commit is contained in:
rektdie committed 2025-03-05 14:32:29 +01:00
1 parent ada7371c01
commit 27f914eeaf
3 files changed
+61 -15

No files matched your search

+37 -12
View File
@@ -3,6 +3,8 @@
#include <iostream>
#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;
}
+2 -2
View File
@@ -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);
};
+22 -1
View File
@@ -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++;