projects°•
let's go gambliiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiing!!1!!!!1!!
X X X oh dang it
hell yeah
hell yeah
hell yeah
• home
• about
• projects

Slot machine linux command

A few days ago I was with one of my friends from uni working on a project together. The project is from a subject we both don't really like, so we have a few strategies to keep ourselves motivated, and one of them is to work on something fun before and after working on the project.

We've been experimenting with different ways of manipulating the output of a program on the terminal, trying to change the color of the text, changing the position of the cursor, and things like that. We were screwing around when my friend had the idea of using what we've been learning to make a gambling command that spawns a slot machine on your teminal. The "let's go gambling meme" has been living rent free on our minds and I instantly loved the idea so we got straight to work.

Before we started coding, we had to first decide what symbols we were going to use for the slot machine. We made the decision to use unicode characters, because even if some terminals dont support them, they are way better for this job than plain ascii characters. We ended up using the symbols of the 4 suits of french-suited playing cards {♠,♥,♦,♣} to keep the gambling aesthetic, and then we chose to add the classic lucky 7 {❼} that for some reason has really been asociated with slot machines. For the color of the symbols we used ansi scape codes, wich are special sequences of characters you put before your text that you can use to format the output of your program.

The code itself it's very simple. We have a string array that stores the 5 symbols with their respective ansi codes and also one index for each slot that is initialized randomly.
Then we have the main loops, that cycle through the symbols and write to the standard output on each iteration. In order to prevent the program to write each iteration on a new line, we use the special character \r that moves the cursor to the beggining of the line.


                    for(int i = 0; i < 10; i++){
                        s1 = symbols[i1++%5];
                        s2 = symbols[i2++%5];
                        s3 = symbols[i3++%5];
                
                        cout << "| [" + s1 + "][" + s2 + "][" + s3 + "] |"  ;
                        flush(cout);
                        cout << "\r";
                
                        nanosleep(&t,NULL);
                    }
                   

We have three loops, in the first one we iterate all three slots, then two, and finally only one. When all three have finished, we look at which symbols we have pulled, and print a message accordingly. For the messages we just basicaly wrote the first things that came to our minds, but it was really fun.

The only thing left was to compile and paste the binary into the PATH so the operating system can recognize it as a command.
This was a really simple project but it was so much fun. If you want to gamble on your own computer or just take a look at the code, you can check the github repository. (It doesn't work on windows tho, so take it into consideration)