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