How to make a game like 2048
A sliding merge game is one function applied four ways. Write that function well, animate it honestly, and spend your creativity on the twist.

Key takeaways
- Write one function that slides and merges a single row to the left, then rotate or reverse the board for the other three directions.
- A tile may merge only once per move, and a move that changes nothing must not spawn a new tile.
- Tiles need stable ids so they can slide visibly. Teleporting numbers make the game feel broken even when the logic is right.
To make a game like 2048, build a square grid where every swipe slides all tiles as far as they can go in that direction, two equal tiles that collide merge into one tile of double the value, and a new small tile appears in a random empty cell after every move that changed the board. The game ends when the grid is full and no neighbors match. That is the sliding merge puzzle, and almost all of its quality comes from one carefully written function and good animation. The name 2048, its look and its specific presentation belong to its owner; the sliding merge genre is open, and the best versions change the rules.
Why sliding merge games are compulsive
- Every move is a small bet. A swipe helps one corner and disturbs another. The board is always a little out of control, which is where the tension comes from.
- Exponential numbers feel like progress. Each new highest tile takes roughly twice as many moves as the last. Players feel themselves getting further even when the rules never change.
- Strategy emerges from one rule. Nobody has to teach the corner strategy (keep the biggest tile in a corner, build a chain along one edge). Players discover it, and discovering it is the fun.
- Zero setup. Four directions, no menus, no tutorial needed after the first swipe.
The row-merge algorithm
Write a function that takes one row and returns the row slid to the left, plus the points earned. Everything else reuses it.
- 01Compress: remove empty cells, so [2, 0, 2, 4] becomes [2, 2, 4].
- 02Merge left to right: walk the list; when two neighbors are equal, replace them with one tile of their sum and skip past the new tile so it cannot merge again this move. [2, 2, 4] becomes [4, 4], not [8].
- 03Pad: add empty cells back on the right until the row is full length: [4, 4, 0, 0].
For a right swipe, reverse each row, run the function, and reverse back. For up and down, do the same on columns (or transpose the board). Compare the board before and after: if nothing changed, the move is invalid, nothing spawns, and the board gives a tiny shake. The score is usually the sum of every merged tile’s new value.
Spawning and ending
- Spawn odds: a common starting point is a 90% chance of the smallest tile and 10% of the next one up. Spawn in a uniformly random empty cell.
- Start state: two tiles on an empty board.
- Game over check: only when the board is full. Then look for any two horizontally or vertically adjacent equal tiles. If none exist, no move can change the board, and the game is over.
- Win state: reaching the target tile shows a celebration with a choice to keep going. Most players keep going, so make continuing the default button.
- Undo: optional but popular. Store the previous board and score; limit undos (one per move, or three per game) so they do not remove all tension.
Animation is half the game
If tiles teleport, the game feels broken even when it is correct. Give each tile a stable id. During a move, compute where each id ends up, then animate from old position to new over about 100 to 150 milliseconds. Merged tiles slide into the same cell, then the survivor pops (scale to about 1.15 and back over another 100 milliseconds) while the other disappears. New tiles grow in from zero after the slide finishes. Queue input during the animation instead of dropping it: fast players swipe again before tiles settle, and ignoring that swipe feels like lag.
Swipe detection on phones: treat a touch that moves more than about 30 pixels as a swipe and pick the direction by the larger axis. Support arrow keys and WASD on desktop. The touch controls guide has more on thresholds and dead zones.
Three twists that make it yours
Sliding merge games with a new skin are everywhere. A new rule is what gets noticed. Each of these changes the strategy, not just the look:
- 01Merge in threes. Tiles only combine when three equal tiles line up in a slide. Boards fill faster, so the grid should be bigger (5 by 5), and planning becomes about setting up triples.
- 02Blockers with a timer. Every so often a rock tile spawns that cannot move or merge and crumbles after a set number of moves. Players have to build around a hole that is temporary but predictable.
- 03Levels with goals. Instead of endless play, each level asks for a target (make two tiles of a certain tier in 40 moves). It turns an endless toy into a puzzle campaign, closer to a puzzle game people finish.
Theme is the other lever. Numbers are abstract; a ladder of things that grow (seeds to trees, pebbles to boulders, sparks to stars) gives players a reason to want the next tier. The merge game guide explores free-form merging on a board, which is a different genre with the same pull.
A brief for an original sliding merge game
A sliding merge puzzle called Moonstones, phone first, works with arrow keys on desktop. A 5 by 5 stone tablet. Each tile is a moon phase in a ladder of ten tiers, from a thin crescent to a glowing full moon and then rare eclipse tiers. Swipe to slide every tile as far as it goes; two equal phases that collide merge into the next phase, and a tile can merge only once per swipe. After any swipe that changed the board, a new crescent appears in a random empty cell (85%) or a half moon (15%). Twist: every 25 swipes a cloud tile drifts onto a random empty cell; it cannot move or merge and dissolves after 8 swipes, with a visible countdown on it. Tiles slide over 120 ms, merged tiles pop, new tiles fade in after the slide, and input during animation is queued. Score is the sum of merged tiers. One undo per game. Game over when the tablet is full and nothing can merge. Save the board and best score so a reload resumes. Night-sky palette, soft chimes that rise in pitch with tier.
Common mistakes
- Double merges in one move. The [4, 4, 8] case. Skip past a tile once it has merged.
- Spawning on invalid moves. It lets players fill the board by mashing a blocked direction. Compare before and after.
- Losing the game on reload. Save the board after every move. The saving progress guide covers what to store.
- Unreadable high tiers. Long numbers or ten similar colors blur together. Every tier needs a distinct color and a label that fits at small sizes.
- Grid movement that feels sticky. If moves are not snappy, the strategy never gets a chance. The grid-based movement guide covers tweening on a grid.
Frequently asked questions
Can I make my own version of 2048?
Sliding tile merge mechanics appear in many games. Give yours its own name, art and theme, do not copy the original’s look, and ideally change a rule so it plays differently.
How does the merge logic work in a sliding tile game?
Slide one row left by removing empty cells, merging equal neighbors once each from left to right, then padding with empty cells. Reverse or transpose the board to reuse that function for the other three directions.
When is the game over in a sliding merge puzzle?
When every cell is filled and no two horizontally or vertically adjacent tiles are equal. Only check this when the board is full, since an empty cell always allows a move.
What grid size should I use?
Four by four is the classic tight size. Five by five gives longer games and room for blockers or three-way merges. Smaller grids end fast and suit short challenge modes.

