Past Projects: Commandline 2048 Game
Tags: coding C source
Having not done much interesting lately, here's a clone of the popular mobile game that I wrote in C.
Table of Contents [Show]
I haven't done a lot of super interesting work lately that I can share with the internet, so I though that I'd go about documenting some of the things that I've worked on previously. This is bound to be a mixed bag that reflects my interests and tools of the time.
A long, long time ago in the year 2014, before the appstore was dominated by monsterous, professionally-developed games based on micro-transaction, we had frivilous little indy games like Flappy Bird which where pushed out by one person over a single nerdy weekend. Among those was a sensation called 2048.
The rules are simple: swipe in one of the 4 cardinal directions, all of the tiles will move in that direction as far as they can. If they bump into a tile with the same value, they combine into one tile with double the value. Each turn a random blank tile will be turned into a 2, with a limited chance of being a 4, and the goal was to get to the 2048 tile. I played the game for a couple of hours but because I like building stuff more than I like playing stuff, and because I was working on my C at the time decided to implement a commandline version
This is not the pinnacle of programming achievement. I would have liked to have implemented it with a more responsive UI, like Curses, which would remove the need to click enter before every move (the conio.h header would also allow for this but it is not distributed with Unix systems). I also could have actually tracked the score, which probably makes it more interesting, but those weren't the parts that I found compelling. As the comments in the code suggest the algorithm with which tiles combine is actually pretty specific. I'd initially written it to make all possible combinations, such that 2 2 2 2 would become 0 0 0 8 which allows for the algorithm to work much more indescrimately. This does not require differentiation between moving across empty tiles, which can be done up to 3 times, and combining two like tiles which can be done only once. In the end, the logic as it is, is still fairly simple but did give me a moment of pause when it came to doing it right.
If you want to keep entertained momentarily, and you have a terminal and C compiler you can download the source here. You can use Ctrl+S to save if you are not prompted to. Warning: Don't run code that you find on the internet unless you can scan it and make sure that it is legitimate. I swear that this is safe, but don't take my word for it. The command `gcc 2048.c -o 2048.bin` will build the binary then `./2048.bin` will execute it. Don't run commands you find on random websites either. You can enter 'h' for help, but if the above instructions are not clear enough, this will not be of any use except to explain that the controls - slightly to my chagrin as a Dvorak typist - are w,a,s,d because capturing arrows keys with the standard input libraries for C was more than I was willing to wrestle with. Have fun!