C snap

I wanted to learn more about the workings of C, so I decided to try and build a snap project using it. I chose to build my own variant, where snap is where the colour and shape match, crackle is for only the colour, and pop is for only the shape.

I started by using a website called textpaint to create a few shapes and use linux colour codes in order to colour them. I built a function then to print two random shapes and return and integer based on which two were printed.

I then used a library called termios which is basically a key press logger for linux. This was important so the user didn’t have to press enter after every snap etc.

Now was to program main game logic. I need to use threading for this as if I told the program to wait in the main thread it would freeze and the user would not be able to guess. C uses pthreads, which I think may not be available on every OS, but there was no apparent alternative unfortunately. Every turn (snappass) we start two threads, one which would be the timer and the other would be the guessing thread. When either thread finished, it would cancel the other running one and then run snappass again.

Finally, I added a how to play, score counter, a timer mechanism (60s to play game) and a message to tell you which you should have picked.

View project

Leave a Reply

Your email address will not be published.