         var endPiece = piece
         var capture = board[move.end]
         var captureSquare = move.end
+        let rights = castlingRights
         if piece.kind.isPawn {
             if move.end.rank == Rank(endFor: playerTurn) {
                 let promotion = promotion()


    /// The target move location for an en passant.
    public var enPassantTarget: Square? {
        guard let (move, piece, _, _, _) = _moveHistory.last where piece.kind.isPawn else {
            return nil
        }
        guard abs(move.rankChange) == 2 else {

        playerTurn.invert()
        attackersToKing = attackers
        self.halfmoves = halfmoves
        self.castlingRights = rights
        return move
    }

