As part of an (unfortunately only formative) assignment for the Introduction to Programming module for my Music Technology Systems degree, I was tasked with creating a clone of the popular game 2048 by Gabriele Cirulli in the Python programming language.
The program I wrote was completed to (and beyond) the specifications of the assignment on the 19th of January 2017 and subsequently hosted here on Github. The program relies on the york_graphics module, written by Joel Fergusson, which, in itself, relies on the graphics module. Furthermore, I have moved all functions and variables into a separate file from the main code and created my own module for ease on my own part.
The specification we were given is as follows. All of these bar the timer are present in the program version handed in for marking.
- There must be a 4×4 grid of “tiles”. The value of each tile can either be a power of two or it can be blank.
- The grid must initially be filled by two twos placed in random squares in the grid. • Indicating a move in any direction (up, down, left or right) causes the tiles to “slide” in that direction until they can slide no more (either because they reach the edge of the grid or they reach a space occupied by another tile. If two tiles of the same value are “pressed together” by this sliding, they collapse into one tile of double the value. Note that this will free a square on the board. In the case where three tiles with the same pressed against each other on the same line, only the front are combined i.e. if a left slide is happening and a row consists of “4, 4, 4, 2”, the front (in this case left, since that’s the direction we are sliding) tiles combine, causing the row to become “8, 4, 2, -”.
- For each slide, a new tile must be placed in a random empty square. The number should be randomly chosen as having a 67% chance of being a 2 or a 33% chance of being a 4.
- There must be some sort of user interface.
There are some optional specifications that you can complete if you have time:
- Detect when a game is over. The game is over when the grid is full of numbers and no move will collapse any tiles.
- Have a graphics based interface rather than a text one.
- Have a timer that indicates how long play has been going for.
As well as all of this, my program also has an ‘autoplay’ feature. The player can set the game to play itself and it will use a simplistic algorithm to achieve a moderate score but likely not surpass a tile value of 256. The graphical interface can be seen below.
Some improvements are necessary to make the program an accurate clone of the original game and some are required to improve performance.
The first change will make the drawing and re-drawing of the grid more efficient. As it stands, the program re-draws every space of the grid after every turn. However, not every space will change with every move. I wish to change this such that a separate matrix holds every space in the main grid which has changed and only redraw those spaces.
The second change is to add rounded corners to each square of the interface. This would increase likeness to the original but, as of late, I haven’t been able to find a way to find the current position of the cursor, which would be necessary to create a rectangle with rounded corners by drawing several rectangles and circles in specific arrangements.
Following the additions to the original specification, I would also very much like to add a timer to the game. I’m aware of several timer functions and have imported the timer module to delay the welcome text which was present in the console version of my clone. This has since been commented out with the introduction of the graphical interface.
Similarly to the original, I would also like to introduce a saving function, which would save the top five scores along with a three-letter initial, much like an arcade machine would. This would require saving to a text file or online database and a new interface to display the scores.