bugfix: castling rights were revoked after moving with a freshly promoted rook
This commit is contained in:
1 parent
34d689fda2
commit
9c787303f8
2 files changed
+16
-22
No files matched your search
+15
-21
@@ -291,17 +291,20 @@ void Board::RemovePiece(int piece, int square, bool color) {
|
|||||||
occupied.PopBit(square);
|
occupied.PopBit(square);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Updates castling rights on capturing (only if needed)
|
// Updates castling rights
|
||||||
static void UpdateCastlingRights(Board &board, int targetPiece, int targetSquare, int attackerColor) {
|
static void UpdateCastlingRights(Board &board, int square, int type, int color) {
|
||||||
if (targetPiece == Rook) {
|
if (type == Rook) {
|
||||||
int queenSideRook = attackerColor ? a1 : a8;
|
int queenSideRook = color ? a8 : a1;
|
||||||
int kingSideRook = attackerColor ? h1 : h8;
|
int kingSideRook = color ? h8 : h1;
|
||||||
|
|
||||||
if (targetSquare == queenSideRook) {
|
if (square == queenSideRook) {
|
||||||
board.castlingRights[!attackerColor * 2 + 1] = false;
|
board.castlingRights[color * 2 + 1] = false;
|
||||||
} else if (targetSquare == kingSideRook) {
|
} else if (square == kingSideRook) {
|
||||||
board.castlingRights[!attackerColor * 2] = false;
|
board.castlingRights[color * 2] = false;
|
||||||
}
|
}
|
||||||
|
} else if (type == King) {
|
||||||
|
board.castlingRights[color * 2 + 1] = false;
|
||||||
|
board.castlingRights[color * 2] = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -309,7 +312,7 @@ 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);
|
UpdateCastlingRights(*this, square, targetType, !color);
|
||||||
}
|
}
|
||||||
|
|
||||||
SetPiece(pieceType, square, color);
|
SetPiece(pieceType, square, color);
|
||||||
@@ -340,7 +343,7 @@ void Board::MakeMove(Move move) {
|
|||||||
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);
|
UpdateCastlingRights(*this, move.MoveTo(), targetPiece, !attackerColor);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case epCapture:
|
case epCapture:
|
||||||
@@ -402,16 +405,7 @@ void Board::MakeMove(Move move) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Removing the right to castle on king and rook movement
|
// Removing the right to castle on king and rook movement
|
||||||
if (attackerPiece == King) {
|
UpdateCastlingRights(*this, move.MoveFrom(), attackerPiece, attackerColor);
|
||||||
castlingRights[attackerColor * 2] = false;
|
|
||||||
castlingRights[attackerColor * 2 + 1] = false;
|
|
||||||
} else if (attackerPiece == Rook) {
|
|
||||||
if ((Bitboard::GetSquare(move.MoveFrom()) & files[A] & (ranks[r_1] | ranks[r_8])).GetBoard()) {
|
|
||||||
castlingRights[attackerColor * 2 + 1] = false;
|
|
||||||
} else if ((Bitboard::GetSquare(move.MoveFrom()) & files[H] & (ranks[r_1] | ranks[r_8])).GetBoard()) {
|
|
||||||
castlingRights[attackerColor * 2] = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
halfMoves++;
|
halfMoves++;
|
||||||
if (halfMoves & 2 == 0) fullMoves++;
|
if (halfMoves & 2 == 0) fullMoves++;
|
||||||
|
|||||||
+1
-1
@@ -10,7 +10,7 @@ int main() {
|
|||||||
|
|
||||||
Board board;
|
Board board;
|
||||||
board.SetByFen("r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1");
|
board.SetByFen("r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1");
|
||||||
Perft(board, 5);
|
Perft(board, 6);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user