diff --git a/cpp/src/board.cpp b/cpp/src/board.cpp index 6d9afa8..251f4d8 100644 --- a/cpp/src/board.cpp +++ b/cpp/src/board.cpp @@ -201,8 +201,8 @@ Board Board::make_move(Move move) const { } // -- Handle castling (just move the rook over) + Coords c = Coords::from_index(move.source_square); if (move.castle_side & KingSide) { - Coords c = Coords::from_index(move.source_square); Coords rook_source{7, c.y}; int8_t old_rook = ret.squares[rook_source.to_index()]; ret.squares[rook_source.to_index()] = Piece::None; @@ -217,6 +217,49 @@ Board Board::make_move(Move move) const { ret.squares[rook_dest.to_index()] = old_rook; } + // -- Check for castling rights + ret.w_castle_rights = w_castle_rights; + ret.b_castle_rights = b_castle_rights; + if (white_to_play) { + if ((squares[move.source_square] & 0b111) == King) + ret.w_castle_rights = NeitherSide; + + if ((squares[move.source_square] & 0b111) == Rook) { + if (c.x == 0 && (ret.w_castle_rights & QueenSide)) + ret.w_castle_rights &= ~(QueenSide); + if (c.x == 7 && (ret.w_castle_rights & KingSide)) + ret.w_castle_rights &= ~(KingSide); + } + + Coords target = Coords::from_index(move.target_square); + if (move.is_capturing && c.y == 7 + && (squares[move.target_square] & 0b111) == Rook) { + if (target.x == 0 && (ret.b_castle_rights & QueenSide)) + ret.b_castle_rights &= ~(QueenSide); + if (target.x == 7 && (ret.b_castle_rights & KingSide)) + ret.b_castle_rights &= ~(KingSide); + } + } else { + if ((squares[move.source_square] & 0b111) == King) + ret.b_castle_rights = NeitherSide; + + if ((squares[move.source_square] & 0b111) == Rook) { + if (c.x == 0 && (ret.w_castle_rights & QueenSide)) + ret.b_castle_rights &= ~(QueenSide); + if (c.x == 7 && (ret.w_castle_rights & KingSide)) + ret.b_castle_rights &= ~(KingSide); + } + + Coords target = Coords::from_index(move.target_square); + if (move.is_capturing && c.y == 7 + && (squares[move.target_square] & 0b111) == Rook) { + if (target.x == 0 && (ret.w_castle_rights & QueenSide)) + ret.w_castle_rights &= ~(QueenSide); + if (target.x == 7 && (ret.w_castle_rights & KingSide)) + ret.w_castle_rights &= ~(KingSide); + } + } + return ret; }