And the logic is something I want to try to explain as well.

I had to redo this a couple of times to make it as efficient as I could, though I bet there is a better way to do it. I want to explain it because if you can understand it, I'd love to hear your input on how it could be more efficient still. Right now the game is still a little slow. I know that Valgo is working on 2048 as well so I'd love to see how he makes it work too

There are two main bits of logic in the game:

## Tile Movement and Combining

I was inspired after I saw this glitch while playing the real game (App Store version):

The zeros weren't meant to be there and I started thinking a way of how the game worked. I used the zeros in the logic for my own version so whenever a tile equals 0, it is hidden.
In the version I made, each tile is displayed using objects names A, B, C....P (since there are 16 tiles). And I coded for each situation separately. Like when you swipe right, each row works out what to do. So the row ABCD has its own rule, EFGH has its own rule, IJKL has its own rule and MNOP has its own rule. The same goes with the columns.

I'm going to use the example of the code I used in the event of swiping right for the tiles in the bottom row (which are the tiles MNOP that you might be able to glimpse in the picture above). In the picture I also split the rules in an effort to make it faster for the actual code.

So the basic principle of my version goes like this (this has been refined since):

```
If M=0, N=0, O=0 and P=0 then
do nothing
```

Since all the tiles are equal to 0, when you swipe right, nothing will happen.

Moving onto the next situation

```
If M≠0, N=0, O=0 and P=0 then
Set value P to M
Set M to 0
```

So here the tile at the very left (M) has a number. When you swipe right, you expect it to go all the way to the very right since there is nothing in between. So it will move to where P is, and this is replicated by setting P to M and then making M 0.

Since we've just done M=0 and M≠0, we'll move onto N=0 and N≠0. N=0 has already been done so far so we'll go onto N≠0.

```
If M=0, N≠0, O=0, P=0 then
Set value P to N
Set value N to 0
```

Like the previous situation, the tile at N moves all the way to the right to P.

Now we'll continue with checking If M≠0 again:

```
If M≠0, If N≠0, If O=0 and If P=0
Check If M=N
Set value P to (M+N)
Set value N to 0
Set value M to 0
Else
Set value P to N
Set value O to M
Set value N to 0
Set value M to 0
End
End
```

So here, the tiles at M and N have numbers. If they're the same, then we'll add them up and put the number at the very right (at P). Otherwise, we'll just move them to the right and keep them in their order (so N goes to the very right at P and M stays next to it and still to its left at O).

So that's the general principle -- checking whether or not the tiles are equal to 0 and if they aren't, then check if they're equal to each other and add them up, otherwise just move them as far as they go. I did this for every row left and right, and every column down and up.

In the actual code, I did it a little differently by going If P=0, If 0=0, If N=0 and If M=0 instead of the other way with M first. And I also found it was okay to not bother with checking if M=0.

And the tiles moving in the game is just an animation and it's not the actual numbers that are moving. The tiles are moved in each situation.

Here's an example of code from the first situation:

The first block with the █M value (its name should be █M_X) is what moves the square around the M tile 300 units to the right, making it 'look' like it's moving. The "Set value high_C to 1" plays the "ping" sound.
So anyway that's how I did it. I couldn't think of how to do it with collisions since you'd need to account whether or not there are zeroes and if there are numbers equal to each other. That's why I'm eager for input. I don't know how it was done in the original game either. That's my version and if you can see a way to improve it or make it more efficient, let me know

## Tile Spawning

First I had to check whether or not the player had actually made a move after swiping and I simply ended up doing that by checking if there was a sound played after I added in the sounds.

I had another way of doing tile spawning with Repeat Until, but it took a bit too much time for certain cases (and plus the app crashed after you looked at the rule at the time of this post, but not any more. A current method for doing Repeat Until involves putting an ability inside of itself underneath a Check Once If.)

I've had a bit more experience with coding since I first started on this project, so I thought of a way that was based on arrays (or at least similar to what arrays are, I think they're also called lists but I'm not sure). I am quite new to the concept so I can't explain it very well but basically it's like doing this:

` Set value Group to [apples, bananas, oranges]`

But instead, in my code I was thinking of this:

```
Make a list
If A = 0 then
Add A to the List
If B = 0 then
Add B to the List
If C= 0 then
Add C to the List
```

and so on....

This is because if a tile is equal to 0, then you can spawn another tile on it.

```
Set value RandomSpawner to [random(1 -"number of items in List")]
Spawn tile which is at "RandomSpawner" position in List
```

The value RandomSpawner will be a number chosen randomly. Let's say the number of tiles equal to 0 was 5. A number from 1-5 chosen randomly to equal RandomSpawner might be 3. So then, it will go to the 3rd item in the list, which might be the G tile if it was added as the third item, and that means we will spawn a tile at G.

Coding this in Hopscotch, I did this:

```
If A=0 then
Set value 1st_random to 1
Increase value Most to 1
```

So this is the same as adding A as the first position in the List, and increasing the number of items in the list by 1.

```
If B=0 then
If 1st_random≠0
Set value 1st_random to 2
Increase value Most to 1
Else
Set value 2nd_random to 2
Increase value Most to 1
End
End
```

This is the same as checking if there's an item already at the first position in the list. If there isn't, then add B to the first position (and I put 2 to represent B). If there is an item at the first position already, then add B to the second position.

```
If C=0 then
If 1st_random≠0
Set value 1st_random to 3
Increase value Most to 1
Else
If 2nd_random=0
Set value 2nd_random to 3
Increase value Most to 1
Else
Set value 3rd_random to 3
Increase value Most to 1
End
End
End
```

And I continued this pattern all the way to P. Sorry that it's not explained very well and again I'm not very familiar with arrays and lists. (And I think this was the ability that had 200+ blocks, which had a name starting with "Assign" so you know to avoid it if you happen to be looking around my code.)

Then I set the RandomSpawner value:

`Set value RandomSpawner to random(1-Most)`

and then I had a whole bunch of rules for this:

```
If RandomSpawner = 1
If 1st_random = 1
Set value A to SpawnedTile
End
If 1st_random = 2
Set value B to SpawnedTile
End
If 1st_random = 3
Set value C to SpawnedTile
End
If 1st_random = 4
Set value D to SpawnedTile
End
If 1st_random = 5
Set value E to SpawnedTile
End
etc
End
```

Then:

```
If RandomSpawner = 2
If 2nd_random = 2
Set value B to SpawnedTile
End
If 2nd_random = 3
Set value C to SpawnedTile
End
If 2nd_random = 4
Set value D to SpawnedTile
End
If 2nd_random = 5
Set value E to SpawnedTile
End
etc
End
```

"SpawnedTile" is just equal to 2 or 4 (chosen randomly).

And that's the tile spawning. Again sorry about it not being very clear but if you have suggestions for improvement please share!