Let’s build a mobile puzzle game from scratch — Part II

Vlad Fedoseyev
2 min readDec 25, 2017

--

In this article, I’m going to highlight important parts of the code from Snowman: Winter Puzzle that could help you understand the game mechanics.

First, we set up the game board. It should be a 2-dimensional array of integers. It tells us what is the rank of a tile located in the i-th row and the j-th column on the game board.

In our game, we distribute ranks 1, 2 and 3 with their corresponding probabilities. For example, since r is an integer in [0..15), the probability of the new tile’s rank to be equal to 1 is 9/15.

This function is called when the user taps on a tile. We have to check if there are 3 or more tiles of the same rank in a group the selected cell belongs to. If true, we merge the group into a higher ranked tile and then apply the gravity force to move other tiles.

Using this function we extracted a group of similar tiles with the selected tile inside. I used dynamic programming technique here for an optimal solution.

This function updates the board when users make their moves. It returns the instructions on how to animate the transition between two board states.

Thanks for reading! Check out the first part of the series here if you want to learn more about the game design. Or check out the next part about Game Center integration.

Follow me on Dribbble or contact directly for collaboration

--

--

Vlad Fedoseyev

I help companies expand their desktop and web solutions to mobile devices | Dribbble: https://dribbble.com/vladfedoseyev