introduced basic InCheck and IsLegal

This commit is contained in:
rektdie committed 2025-03-24 16:45:27 +01:00
1 parent d0ba47c956
commit 2943fccce6
4 files changed
+25 -9

No files matched your search

+17 -3
View File
@@ -132,6 +132,7 @@ void Board::ListMoves() {
for (int i = 0; i < currentMoveIndex; i++) {
std::cout << i+1 << ". ";
moveList[i].PrintMove();
std::cout << "( " << moveTypes[moveList[i].GetFlags()] << " )";
std::cout << std::endl;
}
}
@@ -149,11 +150,24 @@ int Board::GetPieceColor(int square) {
}
bool Board::InCheck() {
// TODO
int kingSquare = (pieces[King] & colors[sideToMove]).getLS1BIndex();
for (int type = Pawn; type <= Queen; type++) {
Bitboard moves = MOVEGEN::getPieceAttacks(kingSquare, type, sideToMove, occupied);
if (moves & colors[!sideToMove] & pieces[type]) return true;
}
return false;
}
bool Board::IsLegal(Move &move) {
// TODO
bool Board::IsLegal(Move move) {
Board copy = *this;
copy.MakeMove(move);
copy.sideToMove = sideToMove;
positionIndex--;
return !copy.InCheck();
}
void Board::SetPiece(int piece, int square, bool color) {
+1 -1
View File
@@ -44,7 +44,7 @@ public:
int GetPieceColor(int square);
bool InCheck();
bool IsLegal(Move &move);
bool IsLegal(Move move);
void SetPiece(int piece, int square, bool color);
void RemovePiece(int piece, int square, bool color);
+5 -4
View File
@@ -6,7 +6,8 @@ void GenPawnMoves(Board &board) {
int square = pawns.getLS1BIndex();
Bitboard pushes = getPawnPushes(square, board.sideToMove, board.occupied);
Bitboard captures = pawnAttacks[board.sideToMove][square] & board.colors[!board.sideToMove];
Bitboard attacks = pawnAttacks[board.sideToMove][square];
Bitboard captures = attacks & board.colors[!board.sideToMove];
Bitboard lastRank = board.sideToMove ? ranks[r_1] : ranks[r_8];
@@ -14,7 +15,7 @@ void GenPawnMoves(Board &board) {
pushes &= lastRank;
if (board.enPassantTarget != noEPTarget) {
if (captures.IsSet(board.enPassantTarget)) {
if (attacks.IsSet(board.enPassantTarget)) {
board.AddMove(Move(square, board.enPassantTarget, epCapture));
}
}
@@ -51,7 +52,7 @@ void GenPawnMoves(Board &board) {
} else {
// Checking for en passant
if (board.enPassantTarget != noEPTarget) {
if (captures.IsSet(board.enPassantTarget)) {
if (attacks.IsSet(board.enPassantTarget)) {
board.AddMove(Move(square, board.enPassantTarget, epCapture));
}
}
@@ -66,7 +67,7 @@ void GenPawnMoves(Board &board) {
board.AddMove(Move(square, pushSquare, rookPromotion));
board.AddMove(Move(square, pushSquare, queenPromotion));
} else {
if (abs(square - pushSquare) == 16) {
if (std::abs(square - pushSquare) == 16) {
board.AddMove(Move(square, pushSquare, doublePawnPush));
} else {
board.AddMove(Move(square, pushSquare, quiet));
+2 -1
View File
@@ -5,12 +5,12 @@
static U64 HelperPerft(Board board, int depth) {
MOVEGEN::GenerateMoves<All>(board);
if (depth == 1) return board.currentMoveIndex;
if (depth == 0) return 1ULL;
U64 nodes = 0;
for (int i = 0; i < board.currentMoveIndex; i++) {
if (!board.IsLegal(board.moveList[i])) continue;
Board copy = board;
copy.MakeMove(board.moveList[i]);
nodes += HelperPerft(copy, depth - 1);
@@ -26,6 +26,7 @@ void Perft(Board &board, int depth) {
Stopwatch sw;
for (int i = 0; i < board.currentMoveIndex; i++) {
if (!board.IsLegal(board.moveList[i])) continue;
Board copy = board;
copy.MakeMove(board.moveList[i]);
U64 nodeCount = HelperPerft(copy, depth - 1);