webdevpuneet.com

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

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

CategoryGames

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.

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

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.

Copy & paste

Source Code

<div class="tm-fit" id="tm-fit"><div class="tm-wrap" id="tm-wrap">
  <div class="tm-panel" id="tm-panel">
    <div class="tm-hud">
      <div class="tm-hud-left">
        <span class="tm-kicker" id="tm-kicker">2048</span>
        <span class="tm-name" id="tm-name">Pick a level</span>
      </div>
      <div class="tm-score"><span class="tm-label">Score</span><b id="tm-score">0</b></div>
      <div class="tm-score"><span class="tm-label">Best</span><b id="tm-best">0</b></div>
      <div class="tm-hud-btns">
        <button class="tm-btn-sm" id="tm-levels-btn" type="button" title="Levels (Esc)">Levels</button>
        <button class="tm-btn-sm" id="tm-mute" type="button" aria-pressed="false">Sound on</button>
      </div>
    </div>

    <div class="tm-main">
      <div class="tm-board-col">
        <div class="tm-board" id="tm-board"><div class="tm-cells" id="tm-cells"></div><div class="tm-tiles" id="tm-tiles"></div><div class="tm-floats" id="tm-floats"></div><div class="tm-banner" id="tm-banner"></div></div>
      </div>
      <aside class="tm-side">
        <div class="tm-goal" id="tm-goal">
          <span class="tm-label">Goal</span>
          <b id="tm-goal-text">Reach 2048</b>
          <div class="tm-bar"><div class="tm-bar-fill" id="tm-bar"></div></div>
          <span class="tm-goal-sub" id="tm-goal-sub"></span>
        </div>
        <div class="tm-stats">
          <div><span class="tm-label">Moves</span><b id="tm-moves">0</b></div>
          <div><span class="tm-label">Highest tile</span><b id="tm-max">2</b></div>
          <div><span class="tm-label">Par</span><b id="tm-par">-</b></div>
        </div>
        <div class="tm-tools">
          <button class="tm-tool" id="tm-undo" type="button"><span class="tm-tool-ico">&#8630;</span>Undo <i id="tm-undo-count">3</i><em>U</em></button>
          <button class="tm-tool" id="tm-hammer" type="button" aria-pressed="false"><span class="tm-tool-ico">&#128296;</span>Smash <i id="tm-hammer-count">1</i><em>H</em></button>
        </div>
        <p class="tm-msg" id="tm-msg"></p>
        <p class="tm-keys">Arrow keys or WASD to slide. On a touch screen, swipe on the board.</p>
      </aside>
    </div>

    <div class="tm-overlay show" id="tm-menu">
      <p class="tm-logo">2048</p>
      <p class="tm-tagline">Slide the tiles, merge equal numbers and chase the goal of each level. Boards get bigger, stones get in the way, and the goals get harder.</p>
      <div class="tm-menu-row">
        <button class="tm-daily" id="tm-daily" type="button">
          <span class="tm-daily-title" id="tm-daily-title">Daily Game</span>
          <span class="tm-daily-sub" id="tm-daily-sub">The same tiles for everyone</span>
        </button>
        <button class="tm-daily tm-classic" id="tm-classic" type="button">
          <span class="tm-daily-title">Classic 2048</span>
          <span class="tm-daily-sub" id="tm-classic-sub">Endless, chase your best score</span>
        </button>
        <div class="tm-totals"><span><b id="tm-star-total">0</b> / 60 stars</span><span>Daily streak <b id="tm-streak">0</b></span><span id="tm-record"></span></div>
      </div>
      <div class="tm-tiers" id="tm-tiers"></div>
    </div>

    <div class="tm-overlay" id="tm-result">
      <p class="tm-result-title" id="tm-result-title">Level cleared!</p>
      <div class="tm-result-stars" id="tm-result-stars"><span>&#9733;</span><span>&#9733;</span><span>&#9733;</span></div>
      <p class="tm-result-sub" id="tm-result-sub"></p>
      <div class="tm-result-btns">
        <button class="tm-btn" id="tm-next" type="button">Next level</button>
        <button class="tm-btn tm-btn-ghost" id="tm-retry" type="button">Play again</button>
        <button class="tm-btn tm-btn-ghost" id="tm-share" type="button">Copy result</button>
        <button class="tm-btn tm-btn-ghost" id="tm-menu-back" type="button">Levels</button>
      </div>
    </div>
  </div>
</div></div>

Extend with AI

Take This Snippet Further With AI

Paste this snippet's HTML, CSS and JS into an AI coding assistant like Claude and ask it to explain how slide() processes a line from the destination side, how the merged flag prevents double merges, and how doMove() animates sliding and merging with transforms and timers. It is also a good base to extend: ask for a hint that suggests a move using a simple heuristic (corner strategy or expectimax), a replay of your game, a 6x6 board, a "time attack" mode, tiles that are not powers of two (Fibonacci), or an online leaderboard for the Daily Game. You can also ask it to review how undo rebuilds the tile elements and how ids are kept unique afterwards.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build a 2048 game with levels in plain HTML, CSS and JavaScript, with no libraries, images or audio files.

Requirements:
- An n by n board (3x3, 4x4 or 5x5) where each cell is empty, a stone, or a tile with a unique id and a value. One slide(grid, n, dir) function handles all four directions: it walks each row or column starting from the side the tiles move towards, merges a tile into the previous one only when the values are equal and that one has not merged in this move, treats a stone as a break between segments, and returns the new grid, the list of moves, the merges and the score gained. A new tile (2 with 90 percent, 4 with 10 percent) appears in a random empty cell after every move that changed the board. Detect game over when the board is full and no equal neighbours remain.
- Tiles are persistent DOM elements positioned with transform: translate and a CSS transition, so they slide. A tile merged into another slides there and fades, the survivor pops with its new value, big merges show sparks, floating "+score" text appears (with a combo label for three or more merges) and new tiles pop in after the slide. Colour tiles by value with a glow for 128 and above.
- 20 levels in 4 tiers defined as data: n, kind (reach a tile, or reach a score within a move limit), target, stones (fixed blocks on seeded positions, never in a corner). Show the goal with a progress bar, the moves, the highest tile and par moves (target / 2.2 * 1.3 for tile goals, 85 percent of the limit for score goals). Stars: 1 for clearing, +1 for finishing within par, +1 for no undo or smash. Clearing a level unlocks the next.
- Undo (3 per level, snapshots of the grid, score and moves, rebuilding the tile elements) and a smash tool (1 per level, click a tile to remove it); both mark the run as assisted. A Classic endless mode with a saved best score and a banner at 2048. A Daily Game with new tiles drawn from a seeded generator (mulberry32) based on the date, no undo or smash, stars for reaching 512, 1024 and 2048, a day streak and a copy result button. Save stars, best score, daily result and streak to localStorage inside try/catch.
- Controls: arrow keys and WASD, swipe on the board with pointer events, U undo, H smash, R restart, Esc menu. Web Audio synth sounds (merge pitch rising with the tile value) with a mute button, confetti on a win, and a result screen with stars.
- Scale the whole game to fit the available area (down to 40 percent) and stack the layout on narrow screens.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.