Skip to content

Commit

Permalink
Fixed bug in which castling was not possible on the queen side
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-bovet committed Aug 1, 2021
1 parent 984cf71 commit 7a9953d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
30 changes: 25 additions & 5 deletions BChessTests/MovesTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,22 @@ TEST_F(MovesTests, WhiteKingCastling) {
}, "e1"
);

// Black rook attacking b1 (so no queen side castling possible)
// Black rook attacking b1 (queen side castling possible)
assertMoves("1r2k3/8/8/8/8/8/3PPP2/R3K2R w KQ - 0 1",
{
"1r2k3/8/8/8/8/8/3PPP2/R4RK1 b - - 1 1", // King side
"1r2k3/8/8/8/8/8/3PPP2/R2K3R b - - 1 1",
"1r2k3/8/8/8/8/8/3PPP2/R4K1R b - - 1 1"
"1r2k3/8/8/8/8/8/3PPP2/R2K3R b - - 1 1", // Queen side
"1r2k3/8/8/8/8/8/3PPP2/R4K1R b - - 1 1", // King side
"1r2k3/8/8/8/8/8/3PPP2/2KR3R b - - 1 1", // Queen side
}, "e1"
);

// Black rook attacking b2 (no queen side castling possible)
assertMoves("2r1k3/8/8/8/8/8/3PPP2/R3K2R w KQ - 0 1",
{
"2r1k3/8/8/8/8/8/3PPP2/R2K3R b - - 1 1", // Queen side
"2r1k3/8/8/8/8/8/3PPP2/R4K1R b - - 1 1", // King side
"2r1k3/8/8/8/8/8/3PPP2/R4RK1 b - - 1 1", // King side
}, "e1"
);
}
Expand Down Expand Up @@ -203,12 +213,22 @@ TEST_F(MovesTests, BlackKingCastling) {
}, "e8"
);

// White rook attacking b8 (so no queen side castling possible)
// White rook attacking b8 (queen side castling possible)
assertMoves("r3k2r/3ppp2/8/8/8/8/8/1R2K3 b kq - 0 1",
{
"r4rk1/3ppp2/8/8/8/8/8/1R2K3 w - - 1 2", // King side
"r4k1r/3ppp2/8/8/8/8/8/1R2K3 w - - 1 2",
"r2k3r/3ppp2/8/8/8/8/8/1R2K3 w - - 1 2"
"r2k3r/3ppp2/8/8/8/8/8/1R2K3 w - - 1 2", // Queen side
"2kr3r/3ppp2/8/8/8/8/8/1R2K3 w - - 1 2"
}, "e8"
);

// White rook attacking c8 (so no queen side castling possible)
assertMoves("r3k2r/3ppp2/8/8/8/8/8/2R1K3 b kq - 0 1",
{
"r2k3r/3ppp2/8/8/8/8/8/2R1K3 w - - 1 2", // Queen side
"r4k1r/3ppp2/8/8/8/8/8/2R1K3 w - - 1 2", // King side
"r4rk1/3ppp2/8/8/8/8/8/2R1K3 w - - 1 2"
}, "e8"
);
}
Expand Down
4 changes: 2 additions & 2 deletions Shared/Engine/ChessMoveGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ void ChessMoveGenerator::generateKingsMoves(ChessBoard &board, Color color, Move
}
if (board.whiteCanCastleQueenSide) {
Bitboard kingMoves = 1 << (square-1) | 1 << (square-2) | 1 << (square-3);
if ((kingMoves & emptySquares) == kingMoves && !board.isAttacked(square-1, otherColor) && !board.isAttacked(square-2, otherColor) && !board.isAttacked(square-3, otherColor)) {
if ((kingMoves & emptySquares) == kingMoves && !board.isAttacked(square-1, otherColor) && !board.isAttacked(square-2, otherColor)) {
moveList.addMove(board, createCastling(square, square-2, color, KING));
if (mode == Mode::firstMoveOnly && moveList.count > 0) return;
}
Expand All @@ -296,7 +296,7 @@ void ChessMoveGenerator::generateKingsMoves(ChessBoard &board, Color color, Move
}
if (board.blackCanCastleQueenSide) {
Bitboard kingMoves = 1UL << (square-1) | 1UL << (square-2) | 1UL << (square-3);
if ((kingMoves & emptySquares) == kingMoves && !board.isAttacked(square-1, otherColor) && !board.isAttacked(square-2, otherColor) && !board.isAttacked(square-3, otherColor)) {
if ((kingMoves & emptySquares) == kingMoves && !board.isAttacked(square-1, otherColor) && !board.isAttacked(square-2, otherColor)) {
moveList.addMove(board, createCastling(square, square-2, color, KING));
if (mode == Mode::firstMoveOnly && moveList.count > 0) return;
}
Expand Down

0 comments on commit 7a9953d

Please sign in to comment.