Codex-generated pull request #139

Closed
rektdie wants to merge 2 commits from codex/add-fast-islegal-function into main
pull from: codex/add-fast-islegal-function
5 changed files with 114 additions and 22 deletions
Showing only changes of commit a9f466b37b - Show all commits

No files matched your search

+89 -4
View File
@@ -246,10 +246,7 @@ void Board::ResetMoves() {
void Board::ListMoves() {
int moveCount = 1;
for (int i = 0; i < currentMoveIndex; i++) {
Board copy = *this;
bool isLegal = copy.MakeMove(moveList[i]);
if (!isLegal)
if (!IsLegal(moveList[i]))
continue;
std::cout << moveCount << ". ";
@@ -501,6 +498,69 @@ bool Board::MakeMove(Move move) {
return true;
}
bool Board::IsLegal(Move move) {
if (!move) {
return true;
}
const int from = move.MoveFrom();
const int to = move.MoveTo();
const int piece = GetPieceType(from);
if (piece == nullPieceType || GetPieceColor(from) != sideToMove) {
return false;
}
if (colors[sideToMove].IsSet(to)) {
return false;
}
const bool enemy = !sideToMove;
const int direction = sideToMove ? south : north;
int kingSquare = (pieces[King] & colors[sideToMove]).getLS1BIndex();
int kingTarget = kingSquare;
Bitboard postMoveOccupancy = occupied;
postMoveOccupancy.PopBit(from);
if (move.GetFlags() == epCapture) {
postMoveOccupancy.PopBit(to - direction);
} else if (move.IsCapture()) {
postMoveOccupancy.PopBit(to);
}
if (move.GetFlags() == kingCastle || move.GetFlags() == queenCastle) {
const int rookFrom = move.GetFlags() == kingCastle ? (sideToMove ? h8 : h1) : (sideToMove ? a8 : a1);
const int rookTo = move.GetFlags() == kingCastle ? rookFrom - 2 : rookFrom + 3;
const int kingMid = from + (to > from ? 1 : -1);
if (IsSquareAttackedBy(enemy, from, occupied)) {
return false;
}
Bitboard midOccupancy = occupied;
midOccupancy.PopBit(from);
midOccupancy.SetBit(kingMid);
if (IsSquareAttackedBy(enemy, kingMid, midOccupancy)) {
return false;
}
postMoveOccupancy.PopBit(rookFrom);
postMoveOccupancy.PopBit(from);
postMoveOccupancy.SetBit(rookTo);
}
postMoveOccupancy.SetBit(to);
if (piece == King) {
kingTarget = to;
}
return !IsSquareAttackedBy(enemy, kingTarget, postMoveOccupancy);
}
bool Board::InPossibleZug() {
Bitboard toCheck;
@@ -577,6 +637,31 @@ Bitboard Board::AttacksTo(int square, Bitboard occupancy) {
return attacks;
}
bool Board::IsSquareAttackedBy(bool attackerSide, int square, Bitboard occupancy) {
const Bitboard pawns = pieces[Pawn] & colors[attackerSide];
if (MOVEGEN::pawnAttacks[!attackerSide][square] & pawns) {
return true;
}
if (MOVEGEN::knightAttacks[square] & pieces[Knight] & colors[attackerSide]) {
return true;
}
if (MOVEGEN::kingAttacks[square] & pieces[King] & colors[attackerSide]) {
return true;
}
if (MOVEGEN::getBishopAttack(square, occupancy) & (pieces[Bishop] | pieces[Queen]) & colors[attackerSide]) {
return true;
}
if (MOVEGEN::getRookAttack(square, occupancy) & (pieces[Rook] | pieces[Queen]) & colors[attackerSide]) {
return true;
}
return false;
}
bool Board::IsSquareThreatened(bool side, int square) {
return colorThreats[!side].IsSet(square);
}
+2
View File
@@ -66,10 +66,12 @@ public:
void Promote(int square, int pieceType, int color, bool isCapture);
bool MakeMove(Move move);
bool IsLegal(Move move);
bool InPossibleZug();
Bitboard AttacksTo(int square, Bitboard occupancy);
bool IsSquareAttackedBy(bool attackerSide, int square, Bitboard occupancy);
bool IsSquareThreatened(bool side, int square);
};
+5 -6
View File
@@ -206,10 +206,7 @@ static bool IsGameOver(Board &board, SEARCH::SearchContext* ctx) {
int moveSeen = 0;
for (int i = 0; i < board.currentMoveIndex; i++) {
Board copy = board;
bool isLegal = copy.MakeMove(board.moveList[i]);
if (!isLegal) continue;
if (!board.IsLegal(board.moveList[i])) continue;
moveSeen++;
}
@@ -227,12 +224,14 @@ static void PlayRandMoves(Board &board, SEARCH::SearchContext* ctx) {
MOVEGEN::GenerateMoves<All>(board, true);
std::uniform_int_distribution<int> dist(0, board.currentMoveIndex - 1);
bool isLegal = board.MakeMove(board.moveList[dist(rng)]);
Move move = board.moveList[dist(rng)];
if (!isLegal) {
if (!board.IsLegal(move)) {
count--;
continue;
}
board.MakeMove(move);
ctx->positionHistory[board.positionIndex] = board.hashKey;
MOVEGEN::GenerateMoves<All>(board, true);
if (IsGameOver(board, ctx)) break;
+8 -4
View File
@@ -10,9 +10,11 @@ static U64 HelperPerft(Board board, int depth) {
U64 nodes = 0;
for (int i = 0; i < board.currentMoveIndex; i++) {
Move move = board.moveList[i];
if (!board.IsLegal(move)) continue;
Board copy = board;
bool isLegal = copy.MakeMove(board.moveList[i]);
if (!isLegal) continue;
copy.MakeMove(move);
nodes += HelperPerft(copy, depth - 1);
}
return nodes;
@@ -25,9 +27,11 @@ void Perft(Board &board, int depth) {
Stopwatch sw;
for (int i = 0; i < board.currentMoveIndex; i++) {
Move move = board.moveList[i];
if (!board.IsLegal(move)) continue;
Board copy = board;
bool isLegal = copy.MakeMove(board.moveList[i]);
if (!isLegal) continue;
copy.MakeMove(move);
U64 nodeCount = HelperPerft(copy, depth - 1);
totalNodes += nodeCount;
board.moveList[i].PrintMove();
+10 -8
View File
@@ -358,9 +358,11 @@ static SearchResults Quiescence(Board& board, int alpha, int beta, int ply, Sear
continue;
}
Move move = board.moveList[i];
if (!board.IsLegal(move)) continue;
Board copy = board;
int isLegal = copy.MakeMove(board.moveList[i]);
if (!isLegal) continue;
copy.MakeMove(move);
ctx->ss[ply].pieceType = board.GetPieceType(board.moveList[i].MoveFrom());
ctx->ss[ply].moveTo = board.moveList[i].MoveTo();
@@ -523,10 +525,10 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
if (!SEE(board, currMove, seeThreshold))
continue;
Board copy = board;
bool isLegal = copy.MakeMove(currMove);
if (!board.IsLegal(currMove)) continue;
if (!isLegal) continue;
Board copy = board;
copy.MakeMove(currMove);
if (copy.positionIndex >= ctx->positionHistory.size()) {
ctx->positionHistory.resize(copy.positionIndex + 100);
@@ -610,10 +612,10 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
continue;
}
Board copy = board;
bool isLegal = copy.MakeMove(currMove);
if (!board.IsLegal(currMove)) continue;
if (!isLegal) continue;
Board copy = board;
copy.MakeMove(currMove);
ctx->ss[ply].pieceType = board.GetPieceType(currMove.MoveFrom());
ctx->ss[ply].moveTo = currMove.MoveTo();