The iOS version of 2048, made using SpriteKit

Overview

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!

Screenshot

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.
  • Three game modes: The original Power of 2, i.e. combining two tiles of the same value to produce their sum. The Power of 3, i.e. combining three consecutive tiles of the same value to produce their sum. Not surprisingly, this is pretty hard with the 3x3 board, although I found it pretty easy to get 81. 243 is a different story... And the Fibonacci sequence, i.e. combining two adjacent numbers in the sequence 2, 3, 5, 8, 13... (I omitted the two 1's in the beginning) to produce the one next to the larger value. This is pretty tricky. Try it out and you will know what I mean.
  • Three themes: I made a bright theme and a 'joyful' theme in addition to the original one. In case you wonder how to do themes in iOS. (There may be a better way, but themes are verbose in nature, because you have to specify all the colors, fonts, etc.)

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.

You should be able to download the source, and build and run without problem. However, please note that you may not want to run it in the simulator unless you don't have an Apple Developer account. SpriteKit does use OpenGL, and simulating that using CPU will cause your computer to take off.

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 M2GameManager class 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 M2Grid class 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 a forEach helper function.
  • The M2Cell class 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 M2Tile class 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 M2GlobalState class 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 M2Theme class 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 M2GridView class 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

Licence

MIT.

Comments
  • Add AI

    Add AI

    AI added! It can either give a hint (single step) or auto run until the game ends. A settings page for AI is also added. See readme.

    Actually merging to master branch may not seem to be a good idea. You can create a branch only for AI and I can push to that branch as you wish.

    Thank you for creating this 2048 iOS version and I learned a lot of SpriteKit from it.

    opened by DJBen 9
  • iOS Ad Bug

    iOS Ad Bug

    After playing for a few seconds on an iPhone 6, an ad comes up that blocks the whole screen with an image extending off the screen and no button to close it.

    opened by rothn 5
  • Scaling Problem Fix on iPhone 6 Plus and 6S Plus.

    Scaling Problem Fix on iPhone 6 Plus and 6S Plus.

    BUG : The problem happened in iPhone 6 Plus and 6S Plus as the scaling is hardcoded for 2x (0.5). So I'm getting the scale dynamically from the device and using.

    This fixes the Scaling Problem.

    opened by iranjith4 1
  • Copied on App Store

    Copied on App Store

    https://itunes.apple.com/us/app/2048-pro/id898612995?mt=8

    It appears that someone copied your code, line for line, and released the app on the app store. I don't see a single feature added, font changed, or anything. Just thought I would let you know.

    opened by kthaas 1
  • Move all user defaults to M2GlobalState and fix memory leak

    Move all user defaults to M2GlobalState and fix memory leak

    Moves setting of default user defaults to M2GlobalState, switching to use of -registerDefaults: method in the process, and using constants for default key names.

    Also fix a potential memory leak with a retained CGPathRef, as identified by XCode's static analysis.

    opened by scottmatthewman 0
  • restart or new game blank board

    restart or new game blank board

    (1) when you filled the board and get the game over overlay, clicking new game will remove all the tiles on board but will sometimes fail to initialize, adding two new tiles.

    (2) similar to previous problem, pressing restart in the beginning or middle of the game will remove all the tiles on board but will sometimes fail to initialize, adding two new tiles.

    -- i think the issue is related to iOS 8 being async with sprite kit run actions. both scenario described above runs the same block of code on M2GameManager.m

    opened by redgreenbluee 1
Owner
Danqing Liu
One of the two danqings
Danqing Liu
ShooterGame - An iOS App that is a shooter game. Made with SpriteKit and Swift

Shooter Game This project was created based on the challenge of Day 66 of the 10

António Pedro da Silva Rocha 0 Feb 1, 2022
2048 for Swift

swift-2048 A working port of iOS-2048 to Apple's new Swift language. Like the original Objective-C version, swift-2048 does not rely upon SpriteKit. S

Austin Zheng 3.1k Dec 27, 2022
A 2048 game writing with SwiftUI.

2048 Game (SwiftUI app) This is a simple game to demonstrate the new SwiftUI framework. Note that the game algorithm may have issues, and this is stil

Cyandev 645 Jan 3, 2023
ShellSlide - Play 2048 in your shell 🎮

ShellSlide - Play 2048 in your shell ?? Features Keep track of your overall highscore Easily save and resume your games Installation (macOS only) Down

Carl 1 Mar 28, 2022
A universal iOS Game using Swift and iOS SpriteKit

a universal iOS Game using Swift 4 and iOS SpriteKit.

KooFrank 455 Oct 22, 2022
iPhone and iPod Touch version of Skeleton Key: is an addictive and unique puzzle game in which you shift keys around the board unlocking treasure chests. Made with cocos2d-iphone.

Skeleton Key (iOS) Skeleton Key is an addictive and unique puzzle game in which you shift keys around the board unlocking treasure chests. It's availa

null 117 Jun 6, 2022
A Component Piece (a game) for a larger app (KoyaQuest) using SpriteKit

NyonindoChallenge This is a game that is intended for inclusion in a larger app (called KoyaQuest). I have created a separate version because of probl

null 0 Oct 25, 2021
Mergel - a match-and-merge game written in Swift, using SpriteKit

Mergel is a match-and-merge game written in Swift, using SpriteKit. It was created for the purpose of having some fun with SpriteKit and learning the Swift language.

Josh McKee 9 Nov 6, 2022
🐦 Flappy Bird reincarnation [Swift 5.3, GameplayKit, SpriteKit, iOS 12].

?? Flappy Bird reincarnation [Swift 5.3, GameplayKit, SpriteKit, iOS 12].

Astemir Eleev 290 Dec 27, 2022
iOS Swift Game - Push SpriteKit to the limit

iOS Swift Project - Legend Wings - EverWing's Mini Clone EverWing is a popular action game. Survive as much you can, earn gold, and upgrade/purchase n

Wong Guan 512 Dec 20, 2022
This project is a 2D game for iOS users built with Swift and SpriteKit.

PANDA CLICKER Description Panda Clicker is a 2D game and the aim is to touch the Panda image on the center of the screen. In each touch of the panda i

iremkaraoglu 6 Dec 21, 2022
An iOS app build with SpriteKit

Gravity Zen This is an iOS app build with SpriteKit. It shows a gravitationals f

Dominik Hauser 11 Apr 22, 2022
🕹 iOS game - classic Tic Tac Toe with AI and state machines [Swift + SpriteKit + GameplayKit].

?? iOS game - classic Tic Tac Toe with AI and state machines [Swift + SpriteKit + GameplayKit].

Astemir Eleev 60 Dec 14, 2022
Glide is a SpriteKit and GameplayKit based engine for building 2d games easily

Glide is a SpriteKit and GameplayKit based engine for building 2d games easily, with a focus on side scrollers. Glide is developed with Swift and works on iOS, macOS and tvOS.

null 433 Jan 6, 2023
Swift framework for working with Tiled assets in SpriteKit

SKTiled is a framework for integrating Tiled assets with Apple's SpriteKit, built from the ground up with Swift. This project began life as an exercis

Michael Fessenden 235 Dec 20, 2022
SpriteKit 2D Breakout Game on rotating 3D SceneKit cube almost no code

2D Breakout game playable as texture on a rotating cube An Xcode12 project for iOS that implements a simple 2D SpriteKit "Break Out" style game. The S

Erik M. Buck 0 Nov 6, 2021
A snake engine written in SpriteKit for all Apple devices.

A snake engine written in SpriteKit for all Apple devices. ⭐ Features Fully tested engine functionality. Framework based, super easy to integrate in d

Chris Jimenez 59 Dec 13, 2022
Hitting only the bad PinGUYS! Simple SpriteKit game

Whack-a-Penguin Hitting only the bad PinGUYS! Simple SpriteKit game. You have to

Pavel Surový 1 Feb 7, 2022
AngryBirdClone - Usage SpriteKit for create a AngryBird Game Clone

#AngryBird Clone SpriteKit The SpriteKit framework makes it easy to create high-

Hamit SEYREK 3 Feb 9, 2022