webdevpuneet.com

Tile Merge 2048 Puzzle with 20 Levels, Stones & Daily Game

Tile Merge 2048 Puzzle · Games · Plain HTML, CSS & JS · Live preview

CategoryGames

For supporters

Source Code

Want the source code?

Play the game for free. Become a WebDevPuneet supporter to unlock the complete HTML, CSS and JavaScript source code, the exports (HTML, React, Vue, Angular, Tailwind) and Fork.

Play freeSource code: Patreon

Extend with AI

Take This Snippet Further With AI

The AI prompt for this one is for Patreon supporters ($3 a month), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.

· Become a supporter

What's included

Features

Tiles are persistent elements that slide with CSS transforms, merge with a pop, and spawn with a delay, instead of being redrawn on every move
One slide() function for all four directions that merges each pair once per move and treats stones as segment breaks
Three board sizes (3x3, 4x4, 5x5) and fixed stone blocks on seeded positions
20 levels in four tiers with tile goals and score-in-limited-moves goals, par moves calculated from the target
Undo (three snapshots per level) and a smash tool that removes a tile, both marked as assists for the stars
Daily Game with a seeded tile sequence, no assists, three stars for 512, 1024 and 2048, and a streak
Classic endless mode with a saved best score and a banner when 2048 is reached
Sparks for big merges, floating score and combo text, glowing high tiles, a goal progress bar and Web Audio sounds that rise in pitch with the merge
Arrow keys, WASD and swipe, with a tap-to-smash on touch screens, and scaling to short preview areas
Progress, stars and best score saved to localStorage inside try/catch, so it works in a sandboxed iframe

About this UI Snippet

Tile Merge 2048 — Sliding Animation, 20 Levels, Bigger Boards, Stones, Undo, Smash and a Daily Game

Screenshot of the Tile Merge 2048 Puzzle snippet rendered live

2048 is a sliding puzzle: every move slides all tiles as far as they can go, equal neighbours merge into one tile of double the value, and a new tile appears. This version keeps that simple rule and adds a game around it: tiles that actually slide, 20 levels with different goals and board sizes, stones that get in the way, undo and a smash tool, a Daily Game with the same tiles for everyone, and the classic endless mode. It is plain HTML, CSS and JavaScript with no libraries, images or audio files.

Move logic that handles any board

The board is an n by n grid whose cells are empty, a stone, or a tile with an id and a value. slide() handles all four directions with one function: lineOf() lists the cells of a row or column starting at the side the tiles move towards, and each line is processed from that side. Tiles are placed one after another, and a tile merges into the previous one only if their values are equal and the previous one has not merged yet in this move, which is the rule that stops 2, 2, 4 becoming 8. A stone splits a line into separate segments, so tiles stop against it and never merge across it. The same code runs a 3x3, 4x4 and 5x5 board.

Tiles that really slide

The old way to draw a 2048 board is to redraw every tile after each move, which makes tiles jump. Here every tile is a persistent element with an id, positioned with a CSS transform. A move only changes the transform of each element, so the browser slides it. The tile that is merged into another slides to the same cell and fades, then the survivor pops with the new value, a spark burst shows for big merges and a floating "+score" text appears (with a combo label when three or more pairs merge at once). A new tile pops in after the slide finishes.

Levels with real goals

The LEVELS array lists 20 levels in four tiers. Warm-up levels use the 4x4 board with targets from 64 to 512, including a score target with a move limit. Big boards use 5x5 with goals up to 2048. Stones adds one to three fixed blocks on seeded positions (never in a corner). Master has tiny 3x3 boards and a 4096 goal. A level is cleared the moment the goal is reached. The par number of moves is calculated from the target, because a tile of value T needs at least about T divided by 2.2 moves, as the average spawn is 2.2.

Undo, smash and stars

Each level gives three undos (a snapshot stack of the grid, the score and the move count) and one smash that removes any tile. Both are optional helpers that cost the unassisted star. Stars are one for clearing the level, one for finishing within the par number of moves and one for using neither undo nor smash. Clearing a level unlocks the next.

Daily game, classic mode and saving

The Daily Game draws its new tiles from a seeded random generator (mulberry32), so everyone who plays the same moves gets the same game, and it has no undo or smash. Reach 512, 1024 or 2048 for one, two or three stars, and a streak builds when you play it on consecutive days. Classic mode is the endless game with your best score. All progress is saved to localStorage inside try/catch, so the game also runs in a sandboxed iframe where storage is blocked.

Controls and feel

Arrow keys or WASD slide the tiles, U undoes, H starts the smash tool and R restarts. On a touch screen you swipe on the board, and a tap removes a tile when the smash tool is active. Sounds come from a Web Audio synth (merge pitch rises with the tile value), big tiles glow, and the whole game scales to short preview areas.

Step by step

How to Use

  1. 1
    Pick a level, Classic or the Daily GameThe menu shows 20 levels in four tiers with their goals. Level 1 is open at first, and clearing a level unlocks the next. Classic is the endless game, and the Daily Game uses the same tiles for everyone.
  2. 2
    Slide the tilesUse the arrow keys or WASD, or swipe on a touch screen. Every tile slides as far as it can in that direction, and two tiles with the same number merge into one with double the value. A new tile appears after every move.
  3. 3
    Reach the goalThe goal card shows what to do, for example reach a 256 tile, or score 2500 points in 160 moves, with a progress bar. The moment you reach it the level is cleared.
  4. 4
    Watch out for stonesOn the Stones levels the grey blocks never move. Tiles stop against them and cannot merge across them, so plan your lines carefully.
  5. 5
    Use undo and smash when you are stuckUndo (U) takes back your last move, three times per level. Smash (H) removes any one tile you click. Both help, but using either costs you the unassisted star.
  6. 6
    Earn stars and chase the dailyOne star for clearing the level, one for finishing within the par number of moves, one for using no undo or smash. In the Daily Game, reach 512, 1024 or 2048 for one, two or three stars and keep your streak going.

Real-world uses

Common Use Cases

Teaching a clean slide-and-merge algorithm
The single-pass rule that merges each pair only once is the heart of 2048, and getting it right in all four directions with one function is a good exercise. Adding stones shows how a small change in the data model changes a whole game.
Animating a grid without a library
Persistent elements with a transform transition are the simplest way to animate sliding pieces. The same idea works for any tile-based game, a kanban board or a sortable list.
A classic with progression for a games page
Levels with different goals, a daily game and a streak turn a well-known game into something people return to, with no account or backend.
Reference for undo with snapshots
A stack of cloned grids, scores and move counts is the simplest reliable undo. It also shows how to rebuild the DOM from state after an undo.
Seeded randomness for fair daily challenges
Drawing every new tile from a seeded generator makes the same game for everyone on a given day, which is easy to share and compare.
Related: Sliding Number Puzzle (15-Puzzle)
See the Sliding Number Puzzle for another sliding tile puzzle with a different goal.
Related: Minesweeper Puzzle Game
See the Minesweeper Puzzle Game for another logic game with levels and a daily board.

Got questions?

Frequently Asked Questions

slide() merges a tile into the previous one only if the previous one has not merged in the same move. The two 2s merge into a 4, which is marked as merged, so the next tile (a 4) cannot merge into it in the same move. The next move can then merge the two 4s into an 8. This single-merge rule is what makes 2048 playable.

A stone is a cell that never moves and never holds a tile. When a line is processed, each stone ends the current segment, and the tiles on either side of it are slid and merged separately. So tiles stop right next to a stone and cannot merge across it. Stones are placed with a seeded random generator, never in a corner.

One star for reaching the goal, one for doing it in no more than the par number of moves, one for not using undo or smash. For a tile goal, par is the target divided by 2.2 (the average value of a new tile) times 1.3, because the board has to receive at least that much value from new tiles. For a score goal, par is 85 percent of the move limit.

New tiles come from a mulberry32 generator seeded with the day number. Every spawn uses the next numbers of that sequence, so if two players make the same moves they get the same tiles. The daily game has no undo or smash, so the result depends only on your moves.

Add an object to the LEVELS array: n is the board size, kind is tile or score, target is the tile or score to reach, limit is the move limit for score levels and stones is the number of fixed blocks. The menu shows five levels per tier, so add levels in fives or edit TIERS. Changing BOARD_PX or GAP changes the size of the board and the spacing.

Yes. All localStorage access is wrapped in try/catch, so a blocked storage only means that progress and best score are not saved, and every sound call is wrapped too. Swipes on the board play the game, and a tap removes a tile when the smash tool is active. On narrow screens the side panel moves under the board.