Do You Want to Play “2048”?
You may or may not have heard about a popular new game called 2048.
We have some very exciting new work in our development builds—a new source code editor that I wanted to exercise.
Around the same time, I saw an article about the game called 2048 (click here). It is a simple, four-by-four grid with numbers that you slide around, as you try to achieve the goal of adding them up to…2048!
I downloaded a free version of 2048 onto my iPhone, and there is also a browser-based version here. I checked it out, and it looked pretty interesting, so I decided to implement a simple version of it as an “afternoon app.” An afternoon app is a simple, time-boxed implementation. Since Smalltalk lets you do a lot in a short amount of time, it is a great choice.
I built it and published it on the public repository. You can find it as TwentyFortyEight.
It is an MVC designed app with the following three classes.
- TwentyFortyEightApp
- TwentyFortyEightModel
- TwentyFortyEightView
I reverse-engineered it by observation. I tried a couple of simple ways to process the moves, and then I refined them. My #process: method takes an array of the four items in a row or column, and it looks like this:
process: array“process an array of the grid”self compress: array.self combine: array.self compress: array.^array#compress: slides the elements down to the far end of the array.#combine: adds adjacent cells of the same number, one pass.
The second compress is required in situations where there are two combines in the one pass.
Since you can move four directions (up, down, left and right), it gives #process the row or column for right and down respectively. For left and up, it reverses the array for #process and then flips it back for integration into the grid.
I made an assumption that turned out to process incorrectly in some circumstances. Rather than fix it, I left it as a challenge to find (not too hard, not to trivial) and change for those who want to tinker with the application. There is another needed fix (hint: the new value that appears), which is a very easy fix.
How Can the Implementation Be Improved?
Other than the needed fix mentioned above, the application does not detect when the game is over or allow you to restart it. These are easy fixes.
Another relatively easy improvement would be to add the running score. When numbers are combined, their value can be added to the score tally.
A big improvement that would take some more time would be to add animation. This would probably require model restructuring or changes to communicate in terms of the moves in order for the view to animate them. Visually, adding animation would be a big improvement. I welcome any developers to give it a try and share their results. I may give it a shot myself.
What Else Could You Do?
You could try using different algorithms for auto-playing the game, in order to discover or test heuristics in order to maximize your score.
If you have any more ideas or any questions, I would be happy to hear from you. You can reach me at athomas@cincom.com.
Best regards and happy Smalltalking!