fixed a bug in previous commit + doublePawnPushes were not working

This commit is contained in:
rektdie committed 2024-04-14 15:11:22 +02:00
1 parent e4ca91d565
commit c97c3acb27
3 files changed
+25 -8

No files matched your search

+16 -7
View File
@@ -291,10 +291,25 @@ void Board::RemovePiece(int piece, int square, bool color) {
occupied.PopBit(square); occupied.PopBit(square);
} }
// Updates castling rights on capturing (only if needed)
static void UpdateCastlingRights(Board &board, int targetPiece, int targetSquare, int attackerColor) {
if (targetPiece == Rook) {
int queenSideRook = attackerColor ? a1 : a8;
int kingSideRook = attackerColor ? a8 : h8;
if (targetSquare == queenSideRook) {
board.castlingRights[!attackerColor * 2 + 1] = false;
} else if (targetSquare == kingSideRook) {
board.castlingRights[!attackerColor * 2] = false;
}
}
}
void Board::Promote(int square, int pieceType, int color, bool isCapture) { void Board::Promote(int square, int pieceType, int color, bool isCapture) {
if (isCapture) { if (isCapture) {
int targetType = GetPieceType(square); int targetType = GetPieceType(square);
RemovePiece(targetType, square, !color); RemovePiece(targetType, square, !color);
UpdateCastlingRights(*this, targetType, square, color);
} }
SetPiece(pieceType, square, color); SetPiece(pieceType, square, color);
@@ -322,13 +337,7 @@ void Board::MakeMove(Move move) {
case capture: case capture:
RemovePiece(targetPiece, move.MoveTo(), !attackerColor); RemovePiece(targetPiece, move.MoveTo(), !attackerColor);
SetPiece(attackerPiece, move.MoveTo(), attackerColor); SetPiece(attackerPiece, move.MoveTo(), attackerColor);
UpdateCastlingRights(*this, targetPiece, move.MoveTo(), attackerColor);
if (targetPiece == Rook) {
int queenSide = move.MoveTo() % 8 == 0;
castlingRights[!attackerColor * 2 + queenSide] = false;
}
break; break;
case epCapture: case epCapture:
RemovePiece(Pawn, move.MoveTo() - direction, !attackerColor); RemovePiece(Pawn, move.MoveTo() - direction, !attackerColor);
+1 -1
View File
@@ -9,7 +9,7 @@ int main() {
initSliderAttacks(); initSliderAttacks();
Board board; Board board;
board.SetByFen("rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8"); board.SetByFen("r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1");
Perft(board, 3); Perft(board, 3);
return 0; return 0;
+8
View File
@@ -537,9 +537,13 @@ static void GenPawnMoves(Board &board, bool color) {
board.AddMove(Move(square, pushSquare, bishopPromotion)); board.AddMove(Move(square, pushSquare, bishopPromotion));
board.AddMove(Move(square, pushSquare, rookPromotion)); board.AddMove(Move(square, pushSquare, rookPromotion));
board.AddMove(Move(square, pushSquare, queenPromotion)); board.AddMove(Move(square, pushSquare, queenPromotion));
} else {
if (abs(pushSquare - square) == 16) {
board.AddMove(Move(square, pushSquare, doublePawnPush));
} else { } else {
board.AddMove(Move(square, pushSquare, quiet)); board.AddMove(Move(square, pushSquare, quiet));
} }
}
pushes.PopBit(pushSquare); pushes.PopBit(pushSquare);
} }
@@ -884,7 +888,11 @@ static void GenCheckEvasions(Board &board, bool color) {
while (pushes.GetBoard()) { while (pushes.GetBoard()) {
int pushSquare = pushes.getLS1BIndex(); int pushSquare = pushes.getLS1BIndex();
if (type == Pawn && abs(pushSquare - square) == 16) {
board.AddMove(Move(square, pushSquare, doublePawnPush));
} else {
board.AddMove(Move(square, pushSquare, quiet)); board.AddMove(Move(square, pushSquare, quiet));
}
pushes.PopBit(pushSquare); pushes.PopBit(pushSquare);
} }