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 694 additions and 602 deletions
Showing only changes of commit a9f466b37b - Show all commits

No files matched your search

+667 -582
View File
File diff suppressed because it is too large. Load diff
+4 -2
View File
@@ -65,11 +65,13 @@ 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);
bool MakeMove(Move move); bool MakeMove(Move move);
bool IsLegal(Move move);
bool InPossibleZug(); bool InPossibleZug();
Bitboard AttacksTo(int square, Bitboard occupancy); Bitboard AttacksTo(int square, Bitboard occupancy);
bool IsSquareAttackedBy(bool attackerSide, int square, Bitboard occupancy);
bool IsSquareThreatened(bool side, int square); 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; int moveSeen = 0;
for (int i = 0; i < board.currentMoveIndex; i++) { for (int i = 0; i < board.currentMoveIndex; i++) {
Board copy = board; if (!board.IsLegal(board.moveList[i])) continue;
bool isLegal = copy.MakeMove(board.moveList[i]);
if (!isLegal) continue;
moveSeen++; moveSeen++;
} }
@@ -227,12 +224,14 @@ static void PlayRandMoves(Board &board, SEARCH::SearchContext* ctx) {
MOVEGEN::GenerateMoves<All>(board, true); MOVEGEN::GenerateMoves<All>(board, true);
std::uniform_int_distribution<int> dist(0, board.currentMoveIndex - 1); 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--; count--;
continue; continue;
} }
board.MakeMove(move);
ctx->positionHistory[board.positionIndex] = board.hashKey; ctx->positionHistory[board.positionIndex] = board.hashKey;
MOVEGEN::GenerateMoves<All>(board, true); MOVEGEN::GenerateMoves<All>(board, true);
if (IsGameOver(board, ctx)) break; if (IsGameOver(board, ctx)) break;
+8 -4
View File
@@ -10,9 +10,11 @@ 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++) {
Move move = board.moveList[i];
if (!board.IsLegal(move)) continue;
Board copy = board; Board copy = board;
bool isLegal = copy.MakeMove(board.moveList[i]); copy.MakeMove(move);
if (!isLegal) continue;
nodes += HelperPerft(copy, depth - 1); nodes += HelperPerft(copy, depth - 1);
} }
return nodes; return nodes;
@@ -25,9 +27,11 @@ void Perft(Board &board, int depth) {
Stopwatch sw; Stopwatch sw;
for (int i = 0; i < board.currentMoveIndex; i++) { for (int i = 0; i < board.currentMoveIndex; i++) {
Move move = board.moveList[i];
if (!board.IsLegal(move)) continue;
Board copy = board; Board copy = board;
bool isLegal = copy.MakeMove(board.moveList[i]); copy.MakeMove(move);
if (!isLegal) continue;
U64 nodeCount = HelperPerft(copy, depth - 1); U64 nodeCount = HelperPerft(copy, depth - 1);
totalNodes += nodeCount; totalNodes += nodeCount;
board.moveList[i].PrintMove(); 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; continue;
} }
Move move = board.moveList[i];
if (!board.IsLegal(move)) continue;
Board copy = board; Board copy = board;
int isLegal = copy.MakeMove(board.moveList[i]); copy.MakeMove(move);
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 +525,10 @@ 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;
Board copy = board; if (!board.IsLegal(currMove)) continue;
bool isLegal = copy.MakeMove(currMove);
if (!isLegal) continue; Board copy = board;
copy.MakeMove(currMove);
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 +612,10 @@ SearchResults PVS(Board& board, int depth, int alpha, int beta, int ply, SearchC
continue; continue;
} }
Board copy = board; if (!board.IsLegal(currMove)) continue;
bool isLegal = copy.MakeMove(currMove);
if (!isLegal) continue; Board copy = board;
copy.MakeMove(currMove);
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();