A simple SpriteKit implementation of the famous "Fifteen Puzzle" game
To solve the implementation of this game we have to focus mainly on these issues:
- Generate a random series (NSArray) of 16 numbers from 0 to 15, where 0 is the empty tile.
You can find this implementation in the RandomNumberSeries class (an NSArray subclass) where in the initWithSeriesMax method will be added (in our case) 16 random numbers into an array. You can reuse this class for a different pourpose.
- Retrieve the “train” of tiles when the user is swiping his finger towards the empty tile.
To solve this issue (see getTrainTilesFromNode into the GameScene class), starting from the touched tile, you have to look toward in the swipe direction if thre is the empty tile.
All the tiles before the empty tile are the "train of tiles".
- Move the train of tiles from the first tile to the empty tile when the user swipe his finger.
I have solved this issue by moving the empty tile (last tile into the train of tiles) from its position to the position of the first tile of the train tiles collection.
I used a moveTo SKAction to have a pleasant swiping effect with also a sound effect.
Enjoy! ;-)