fixed checkmate and stalemate detection
This commit is contained in:
1 parent
3090dbac8b
commit
d4c7263b93
5 files changed
+23
-26
No files matched your search
+8
-11
@@ -159,20 +159,14 @@ int Board::GetPieceColor(int square) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool Board::InCheck() {
|
bool Board::InCheck() {
|
||||||
int kingSquare = (pieces[King] & colors[sideToMove]).getLS1BIndex();
|
Bitboard myKingSquare = colors[sideToMove] & pieces[King];
|
||||||
|
|
||||||
for (int type = Pawn; type <= Queen; type++) {
|
return GetThreatMaps(!sideToMove) & myKingSquare;
|
||||||
Bitboard moves = MOVEGEN::getPieceAttacks(kingSquare, type, sideToMove, occupied);
|
|
||||||
|
|
||||||
if (moves & colors[!sideToMove] & pieces[type]) return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Board::IsLegal(Move move) {
|
bool Board::IsLegal(Move move) {
|
||||||
Board copy = *this;
|
Board copy = *this;
|
||||||
copy.MakeMove(move);
|
copy.MakeMove(move, true);
|
||||||
copy.sideToMove = sideToMove;
|
copy.sideToMove = sideToMove;
|
||||||
positionIndex--;
|
positionIndex--;
|
||||||
|
|
||||||
@@ -231,9 +225,10 @@ void Board::Promote(int square, int pieceType, int color, bool isCapture) {
|
|||||||
SetPiece(pieceType, square, color);
|
SetPiece(pieceType, square, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Board::MakeMove(Move move) {
|
void Board::MakeMove(Move move, bool legalityCheck) {
|
||||||
|
if (!legalityCheck) {
|
||||||
SEARCH::nodes++;
|
SEARCH::nodes++;
|
||||||
|
}
|
||||||
// Null Move
|
// Null Move
|
||||||
if (!move) {
|
if (!move) {
|
||||||
int newEpTarget = noEPTarget;
|
int newEpTarget = noEPTarget;
|
||||||
@@ -357,6 +352,8 @@ void Board::MakeMove(Move move) {
|
|||||||
|
|
||||||
positionIndex++;
|
positionIndex++;
|
||||||
positionHistory[positionIndex] = hashKey;
|
positionHistory[positionIndex] = hashKey;
|
||||||
|
|
||||||
|
MOVEGEN::GenThreatMaps(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Board::InPossibleZug() {
|
bool Board::InPossibleZug() {
|
||||||
|
|||||||
+1
-1
@@ -53,7 +53,7 @@ public:
|
|||||||
void RemovePiece(int piece, int square, bool color);
|
void RemovePiece(int piece, int square, bool color);
|
||||||
|
|
||||||
void Promote(int square, int pieceType, int color, bool isCapture);
|
void Promote(int square, int pieceType, int color, bool isCapture);
|
||||||
void MakeMove(Move move);
|
void MakeMove(Move move, bool legalityCheck);
|
||||||
|
|
||||||
bool InPossibleZug();
|
bool InPossibleZug();
|
||||||
};
|
};
|
||||||
+2
-2
@@ -12,7 +12,7 @@ static U64 HelperPerft(Board board, int depth) {
|
|||||||
for (int i = 0; i < board.currentMoveIndex; i++) {
|
for (int i = 0; i < board.currentMoveIndex; i++) {
|
||||||
if (!board.IsLegal(board.moveList[i])) continue;
|
if (!board.IsLegal(board.moveList[i])) continue;
|
||||||
Board copy = board;
|
Board copy = board;
|
||||||
copy.MakeMove(board.moveList[i]);
|
copy.MakeMove(board.moveList[i], false);
|
||||||
nodes += HelperPerft(copy, depth - 1);
|
nodes += HelperPerft(copy, depth - 1);
|
||||||
positionIndex--;
|
positionIndex--;
|
||||||
}
|
}
|
||||||
@@ -28,7 +28,7 @@ void Perft(Board &board, int depth) {
|
|||||||
for (int i = 0; i < board.currentMoveIndex; i++) {
|
for (int i = 0; i < board.currentMoveIndex; i++) {
|
||||||
if (!board.IsLegal(board.moveList[i])) continue;
|
if (!board.IsLegal(board.moveList[i])) continue;
|
||||||
Board copy = board;
|
Board copy = board;
|
||||||
copy.MakeMove(board.moveList[i]);
|
copy.MakeMove(board.moveList[i], false);
|
||||||
U64 nodeCount = HelperPerft(copy, depth - 1);
|
U64 nodeCount = HelperPerft(copy, depth - 1);
|
||||||
positionIndex--;
|
positionIndex--;
|
||||||
totalNodes += nodeCount;
|
totalNodes += nodeCount;
|
||||||
|
|||||||
+11
-11
@@ -140,7 +140,7 @@ static SearchResults Quiescence(Board board, int alpha, int beta, int ply) {
|
|||||||
if (!board.IsLegal(board.moveList[i])) continue;
|
if (!board.IsLegal(board.moveList[i])) continue;
|
||||||
if (board.moveList[i].IsCapture()) {
|
if (board.moveList[i].IsCapture()) {
|
||||||
Board copy = board;
|
Board copy = board;
|
||||||
copy.MakeMove(board.moveList[i]);
|
copy.MakeMove(board.moveList[i], false);
|
||||||
int score = -Quiescence(copy, -beta, -alpha, ply + 1).score;
|
int score = -Quiescence(copy, -beta, -alpha, ply + 1).score;
|
||||||
positionIndex--;
|
positionIndex--;
|
||||||
|
|
||||||
@@ -201,7 +201,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) {
|
|||||||
if (!doingNullMove && staticEval >= beta) {
|
if (!doingNullMove && staticEval >= beta) {
|
||||||
if (depth >= 3 && !board.InPossibleZug()) {
|
if (depth >= 3 && !board.InPossibleZug()) {
|
||||||
Board copy = board;
|
Board copy = board;
|
||||||
copy.MakeMove(Move());
|
copy.MakeMove(Move(), false);
|
||||||
|
|
||||||
doingNullMove = true;
|
doingNullMove = true;
|
||||||
int score = -PVS<false>(copy, depth - 3, -beta, -beta + 1, ply + 1).score;
|
int score = -PVS<false>(copy, depth - 3, -beta, -beta + 1, ply + 1).score;
|
||||||
@@ -216,14 +216,6 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) {
|
|||||||
|
|
||||||
MOVEGEN::GenerateMoves<All>(board);
|
MOVEGEN::GenerateMoves<All>(board);
|
||||||
|
|
||||||
if (board.currentMoveIndex == 0) {
|
|
||||||
if (board.InCheck()) { // checkmate
|
|
||||||
return -99000 + ply;
|
|
||||||
} else { // stalemate
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
SortMoves(board, ply);
|
SortMoves(board, ply);
|
||||||
|
|
||||||
int score = -inf;
|
int score = -inf;
|
||||||
@@ -239,7 +231,7 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) {
|
|||||||
if (!board.IsLegal(currMove)) continue;
|
if (!board.IsLegal(currMove)) continue;
|
||||||
|
|
||||||
Board copy = board;
|
Board copy = board;
|
||||||
copy.MakeMove(currMove);
|
copy.MakeMove(currMove, false);
|
||||||
|
|
||||||
int reductions = GetReductions(board, currMove, depth, moveSeen, ply);
|
int reductions = GetReductions(board, currMove, depth, moveSeen, ply);
|
||||||
|
|
||||||
@@ -291,6 +283,14 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (moveSeen == 0) {
|
||||||
|
if (board.InCheck()) { // checkmate
|
||||||
|
return -99000 + ply;
|
||||||
|
} else { // stalemate
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TT.WriteEntry(board.hashKey, depth, results.score, nodeType, results.bestMove);
|
TT.WriteEntry(board.hashKey, depth, results.score, nodeType, results.bestMove);
|
||||||
if (searchStopped) return 0;
|
if (searchStopped) return 0;
|
||||||
return results;
|
return results;
|
||||||
|
|||||||
+1
-1
@@ -32,7 +32,7 @@ static void ParsePosition(Board &board, std::string_view command) {
|
|||||||
std::vector<std::string> moves = UTILS::split(command.substr(movesIndex + 6, command.length() - (movesIndex + 5)), ' ');
|
std::vector<std::string> moves = UTILS::split(command.substr(movesIndex + 6, command.length() - (movesIndex + 5)), ' ');
|
||||||
|
|
||||||
for (std::string_view move : moves) {
|
for (std::string_view move : moves) {
|
||||||
board.MakeMove(UTILS::parseMove(board, move));
|
board.MakeMove(UTILS::parseMove(board, move), false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user