The iOS version of 2048, made using SpriteKit
2048
This is a derivative and the iOS version of the game 2048. In the very unlikely case that you don't know what it is, you can check it out here.
Made just for fun!
AI
An AI is added, thanks to DJBen! Tap "Hint" to show hint (e.g. Move left/right/up/down); tap "Auto Run" to run AI automatically. Check it out in the AI branch. You can also check out this demo video on YouTube.
Thanks to ov3y's Javascript version that gave me (DJBen, that is) inspiration.
Issues and pending improvements
Currently the searching depth is 2 and it fails sometimes. You can increase the number to 3 or more by changing the return value of - (NSInteger)maximumSearchingDepth in M2GlobalState+AI.h.
Ideally, the AI should search from 0 depth to infinity and have a time out, it records the current best move when finish searching the current depth and stops immediately when timed out and return the best move so far. However, I have a little bit of trouble when dealing with NSOperationQueue so I didn't do it this way. Now the AI only searches at the specified -maximumSearchingDepth.
The Game
Since it is a derivative of the original 2048, it is not the same. Specifically, it has the following additions:
- Three board sizes: 3x3, 4x4 and 5x5. The smaller the board is, the fewer cells you have, and the harder the game is.
The Technology
This version of 2048 is built using SpriteKit, the new 2-D game engine Apple introduced to iOS 7. As a result, it requires iOS 7 to run. On the other hand, this app has the following two great properties:
It does not rely on any* third-party library. Not that Cocos-2D is not great, but the fact it's using SpriteKit means that it does not have any dependencies.
- It does not have any images. That's right. The entire UI is done either via UIKit, or by Core Graphics. Check out the related files to see how that is done, if you are curious.
The Code
First off, the best thing about the code is that it's pretty well documented. Most methods have the Apple-style documentation, which means that you can triple-click on the method name to get its documentation.
The code started to resemble the structure of the original 2048. So for example, it has a game manager, a board class, a tile class, etc. I at least tried to stick to MVC as much as possible. Here is a brief summary to get you started:
- The
M2GameManagerclass controls the game logic. There is only one action in the game: move. So the majority of that class is handling the move. The rest is checking whether you've won or died, etc. - The
M2Gridclass is the data structure for the board. The original 2048 used a 1-D array, but heck, 2-D array doesn't seem to be too bad here! ...except looping it is a bit ugly, so I made aforEachhelper function. - The
M2Cellclass is the "slot"s. They are not the tiles themselves. The benefit of having this class is that the cells never move, so they are good references and they don't mess stuff up. - The
M2Tileclass is the actual tile, and this is the actual SpriteKit class. If all you want is some sample code for SpriteKit, here it is. I believe my animations are smoother than the other 2048 on the App Store, and are closer to the original animation. - The
M2GlobalStateclass is a global class accessible from anywhere in the universe. Well, global stuff is evil, right? At least so we are told. But, it is at least better to encapsulate the global stuff into one single object (namespace), and that's a singleton right there. - The
M2Themeclass and its subclasses control the theme. - There are also some controller classes and view classes. It's probably a better idea to do the Game Over scene in SpriteKit, but I was lazy so I faked it using a view. The
M2GridViewclass is the one that draws the board, btw.
Contributing
Any contributions are more than welcome! If you do make improvements to it, remember to put yourself in the "About 2048" page to get yourself credit.
Contributors
- Danqing Liu (me)
- Scott Matthewman
- Sihao Lu
Licence
MIT.