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.