fixed rare ep legality bug | bench 1812688 #140
No files matched your search
+90
-6
@@ -4,6 +4,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <ranges>
|
#include <ranges>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
#include <cassert>
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
void Board::Reset() {
|
void Board::Reset() {
|
||||||
@@ -348,7 +349,6 @@ void Board::Promote(int square, int pieceType, int color, bool isCapture) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool Board::MakeMove(Move move) {
|
bool Board::MakeMove(Move move) {
|
||||||
Board save = *this;
|
|
||||||
// Null Move
|
// Null Move
|
||||||
if (!move) {
|
if (!move) {
|
||||||
int newEpTarget = noEPTarget;
|
int newEpTarget = noEPTarget;
|
||||||
@@ -487,13 +487,15 @@ bool Board::MakeMove(Move move) {
|
|||||||
|
|
||||||
MOVEGEN::GenThreatMaps(*this);
|
MOVEGEN::GenThreatMaps(*this);
|
||||||
pinned = {CalcPinned(White), CalcPinned(Black)};
|
pinned = {CalcPinned(White), CalcPinned(Black)};
|
||||||
|
checkers = CalcCheckers();
|
||||||
|
|
||||||
|
/*
|
||||||
sideToMove = !sideToMove;
|
sideToMove = !sideToMove;
|
||||||
if (InCheck()) {
|
if (InCheck()) {
|
||||||
*this = save;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
sideToMove = !sideToMove;
|
sideToMove = !sideToMove;
|
||||||
|
*/
|
||||||
|
|
||||||
if (attackerColor == Black) fullMoves++;
|
if (attackerColor == Black) fullMoves++;
|
||||||
if (attackerPiece == Pawn || move.IsCapture()) {
|
if (attackerPiece == Pawn || move.IsCapture()) {
|
||||||
@@ -612,6 +614,36 @@ static Bitboard rayBetween(int sq1, int sq2) {
|
|||||||
return ray;
|
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) {
|
bool Board::IsSquareThreatened(bool side, int square) {
|
||||||
return colorThreats[!side].IsSet(square);
|
return colorThreats[!side].IsSet(square);
|
||||||
}
|
}
|
||||||
@@ -623,19 +655,21 @@ Bitboard Board::CalcCheckers() {
|
|||||||
Bitboard Board::CalcPinned(bool color) {
|
Bitboard Board::CalcPinned(bool color) {
|
||||||
Bitboard pinned;
|
Bitboard pinned;
|
||||||
|
|
||||||
Bitboard kingSquare = (pieces[King] & colors[color]).getLS1BIndex();
|
int kingSquare = (pieces[King] & colors[color]).getLS1BIndex();
|
||||||
|
|
||||||
|
assert(kingSquare != -1);
|
||||||
|
|
||||||
Bitboard oppQueens = pieces[Queen] & colors[!color];
|
Bitboard oppQueens = pieces[Queen] & colors[!color];
|
||||||
|
|
||||||
Bitboard potentialAttackers = MOVEGEN::getPieceAttacks(kingSquare, Bishop, !color, colors[!color]) & (oppQueens | (pieces[Bishop] & colors[!color]))
|
Bitboard potentialAttackers = MOVEGEN::getBishopAttack(kingSquare, colors[!color]) & (oppQueens | (pieces[Bishop] & colors[!color]))
|
||||||
| MOVEGEN::getPieceAttacks(kingSquare, Rook, !color, colors[!color]) & (oppQueens | (pieces[Rook] & colors[!color]));
|
| MOVEGEN::getRookAttack(kingSquare, colors[!color]) & (oppQueens | (pieces[Rook] & colors[!color]));
|
||||||
|
|
||||||
while (potentialAttackers) {
|
while (potentialAttackers) {
|
||||||
int attackerSquare = potentialAttackers.getLS1BIndex();
|
int attackerSquare = potentialAttackers.getLS1BIndex();
|
||||||
|
|
||||||
Bitboard isPinned = colors[color] & rayBetween(attackerSquare, kingSquare);
|
Bitboard isPinned = colors[color] & rayBetween(attackerSquare, kingSquare);
|
||||||
|
|
||||||
if (isPinned)
|
if (isPinned.PopCount() == 1)
|
||||||
pinned |= isPinned;
|
pinned |= isPinned;
|
||||||
|
|
||||||
potentialAttackers.PopBit(attackerSquare);
|
potentialAttackers.PopBit(attackerSquare);
|
||||||
@@ -643,3 +677,53 @@ Bitboard Board::CalcPinned(bool color) {
|
|||||||
|
|
||||||
return pinned;
|
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) {
|
||||||
|
Bitboard occAfterEP = occupied ^ Bitboard::GetSquare(from) ^ Bitboard::GetSquare(to)
|
||||||
|
^ Bitboard::GetSquare(enPassantTarget);
|
||||||
|
|
||||||
|
Bitboard theirQueens = pieces[Queen] & colors[them];
|
||||||
|
|
||||||
|
return (MOVEGEN::getPieceAttacks(kingSquare, Bishop, them, occAfterEP) & (theirQueens | (pieces[Bishop] & colors[them]))).PopCount() < 1
|
||||||
|
&& (MOVEGEN::getPieceAttacks(kingSquare, Rook, them, 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);
|
||||||
|
}
|
||||||
@@ -78,4 +78,6 @@ public:
|
|||||||
|
|
||||||
Bitboard CalcCheckers();
|
Bitboard CalcCheckers();
|
||||||
Bitboard CalcPinned(bool color);
|
Bitboard CalcPinned(bool color);
|
||||||
|
|
||||||
|
bool IsLegal(Move &move);
|
||||||
};
|
};
|
||||||
@@ -64,6 +64,10 @@ public:
|
|||||||
return (GetFlags() >= knightPromotion);
|
return (GetFlags() >= knightPromotion);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool IsCastle() {
|
||||||
|
return (GetFlags() == kingCastle || GetFlags() == queenCastle);
|
||||||
|
}
|
||||||
|
|
||||||
void PrintMove() {
|
void PrintMove() {
|
||||||
std::cout << squareCoords[MoveFrom()] << squareCoords[MoveTo()];
|
std::cout << squareCoords[MoveFrom()] << squareCoords[MoveTo()];
|
||||||
|
|
||||||
|
|||||||
+7
-3
@@ -1,8 +1,9 @@
|
|||||||
#include "perft.h"
|
#include "perft.h"
|
||||||
#include "stopwatch.h"
|
#include "stopwatch.h"
|
||||||
#include "movegen.h"
|
#include "movegen.h"
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
static U64 HelperPerft(Board board, int depth) {
|
static U64 HelperPerft(Board &board, int depth) {
|
||||||
MOVEGEN::GenerateMoves<All>(board, true);
|
MOVEGEN::GenerateMoves<All>(board, true);
|
||||||
|
|
||||||
if (depth == 0) return 1ULL;
|
if (depth == 0) return 1ULL;
|
||||||
@@ -10,15 +11,18 @@ static U64 HelperPerft(Board board, int depth) {
|
|||||||
U64 nodes = 0;
|
U64 nodes = 0;
|
||||||
|
|
||||||
for (int i = 0; i < board.currentMoveIndex; i++) {
|
for (int i = 0; i < board.currentMoveIndex; i++) {
|
||||||
|
if (!board.IsLegal(board.moveList[i])) continue;
|
||||||
|
|
||||||
Board copy = board;
|
Board copy = board;
|
||||||
bool isLegal = copy.MakeMove(board.moveList[i]);
|
copy.MakeMove(board.moveList[i]);
|
||||||
if (!isLegal) continue;
|
|
||||||
nodes += HelperPerft(copy, depth - 1);
|
nodes += HelperPerft(copy, depth - 1);
|
||||||
}
|
}
|
||||||
return nodes;
|
return nodes;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Perft(Board &board, int depth) {
|
void Perft(Board &board, int depth) {
|
||||||
|
board.SetByFen(StartingFen);
|
||||||
MOVEGEN::GenerateMoves<All>(board, true);
|
MOVEGEN::GenerateMoves<All>(board, true);
|
||||||
|
|
||||||
U64 totalNodes = 0;
|
U64 totalNodes = 0;
|
||||||
|
|||||||
+6
-8
@@ -358,9 +358,9 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!board.IsLegal(board.moveList[i])) continue;
|
||||||
Board copy = board;
|
Board copy = board;
|
||||||
int isLegal = copy.MakeMove(board.moveList[i]);
|
copy.MakeMove(board.moveList[i]);
|
||||||
if (!isLegal) continue;
|
|
||||||
|
|
||||||
ctx->ss[ply].pieceType = board.GetPieceType(board.moveList[i].MoveFrom());
|
ctx->ss[ply].pieceType = board.GetPieceType(board.moveList[i].MoveFrom());
|
||||||
ctx->ss[ply].moveTo = board.moveList[i].MoveTo();
|
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))
|
if (!SEE(board, currMove, seeThreshold))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
if (!board.IsLegal(currMove)) continue;
|
||||||
Board copy = board;
|
Board copy = board;
|
||||||
bool isLegal = copy.MakeMove(currMove);
|
copy.MakeMove(currMove);
|
||||||
|
|
||||||
if (!isLegal) continue;
|
|
||||||
|
|
||||||
if (copy.positionIndex >= ctx->positionHistory.size()) {
|
if (copy.positionIndex >= ctx->positionHistory.size()) {
|
||||||
ctx->positionHistory.resize(copy.positionIndex + 100);
|
ctx->positionHistory.resize(copy.positionIndex + 100);
|
||||||
@@ -610,10 +609,9 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!board.IsLegal(currMove)) continue;
|
||||||
Board copy = board;
|
Board copy = board;
|
||||||
bool isLegal = copy.MakeMove(currMove);
|
copy.MakeMove(currMove);
|
||||||
|
|
||||||
if (!isLegal) continue;
|
|
||||||
|
|
||||||
ctx->ss[ply].pieceType = board.GetPieceType(currMove.MoveFrom());
|
ctx->ss[ply].pieceType = board.GetPieceType(currMove.MoveFrom());
|
||||||
ctx->ss[ply].moveTo = currMove.MoveTo();
|
ctx->ss[ply].moveTo = currMove.MoveTo();
|
||||||
|
|||||||
Reference in new issue
Block a user