Turning your phone into a virtual-joystick

Image
Update: I kept working on this and I have released it as a package for Windows, Linux and macOS. Check it out: https://github.com/zenineasa/joystick/releases/tag/v1.0.0 -- During the days when I was pursying my master's programme, my friends and I used to occasionally go to a classroom in the university, turn on a projector, connect devices like Nintento Switch or Gaming computers and loads of joysticks, and play different simple multiplayer games; MarioKart was my favourite. From time-to-time, when I get together with people, I ponder that it would be a good idea if I bought such devices. Indeed, I do have a laptop, which could easily run such games; SuperTuxKart is similar enough to MarioKart and it can run on Linux, Windows and Mac. However, I do not have joysticks with me at the moment. Therefore, I think it would be a good idea if I simply worked on a project that would enable using our phones as joysticks. From a high-level, the plan is to host APIs on a NodeJS server that wo...

My attempt to create a Quine in MATLAB

A few weeks ago, I watched a video on YouTube which talked about quine. A quine is a program that outputs it's own code. For instance, in JavaScript, the following code would output itself.

!function $(){console.log('!'+$+'()')}()

As I watched the video, I was sure that creating a quine is not very easy. But, I found it interesting to attempt to write one on my own, maybe in MATLAB.

During the initial five minutes, I sat on my easy chair and started thinking about how to approach it. I drank a cup of coffee and opened up MATLAB Online and started trying out random things, hoping that I would eventually get some idea. I did not want to cheat by using operations to read from the currently opened file. That can be easily achieved by using the 'type' function or 'fileread' function.

As I was not able to get any idea by just thinking, I decided to have a look at different quines written by people in other languages. I thought perhaps something from JavaScript, Python or C++ would inspire me. I read through the Wikipedia page talking about quine, which has some examples.

After trying and trying for about an hour, I arrived at the following, which is not quite what we need yet,

function y=quine();y='function y=quine();y=REP;y=strrep(y,"REE",y);';y=strrep(y,"REP",['''' y '''']);

If you run the above, the following output would be produced,

function y=quine();y='function y=quine();y=REP;y=strrep(y,"REE",y);';y=strrep(y,"REE",y);

You should be able to see a few issues here. The first and foremost issue is with the inverted commas. That one is really hard to tackle. If I try to add an escape character and attempt to print it at one place, then I would have to do it twice at the other end, which would require me to add two more at the original place, and so on and so forth. The other issue is with 'strrep' trying to replace all occurances of "REP" rather than just the first. To showcase it here, I had changed "REP" to "REE" at one place to avoid multiple replacement.

Now that we know that the issue in this approach is to be able to replace just one "REP" and add an inverted comma effectively, it seems logical to attempt to use 'fprintf' function. However, my attempt was to create a function that would output itself as a function output, rather than simply displaying it. Therefore, I can't use that.

But then, I found a function very similar to 'fprintf', but can store the value to a variable. It's called 'sprintf'. Initially I arrived at the following.

function y=quine();y='function y=quine();y=%s;y=sprintf(y,y);';y=sprintf(y,y);

This one produced the following output:

function y=quine();y=function y=quine();y=%s;y=sprintf(y,y);;y=sprintf(y,y);

It appears to be almost right. Except for the inverted comma to show that 'y' is a string, everything is find here. The only option that I could think of was to use the character code of the inverted comma. The following MATLAB command helped be to get that.

>> char(39)

Intergrating that with the quine code, I got the following.

function y=quine();y='function y=quine();y=%s;y=sprintf(y,[char(39) y char(39)]);';y=sprintf(y,[char(39) y char(39)]);

And yes, that is a quine.








Comments

Popular posts from this blog

First impression of Lugano - Mindblowing

Thinking about developing an opensource P2P social network

Created a video to help people get started with HexHoot