Most browser 2048 clones stop at basic slide logic. When building 2048 Crown, I wanted something closer to a chess post-game engine review: a zero-LLM, 100% browser-side Expectimax AI Coach that pinpoints the real turning point of a run and turns messy 0-empty boards into playable training drills.
Here are four surprisingly tricky engineering and UX problems I ran into — and how I solved each one in pure TypeScript.
1. Undo Without RNG Farming (Mulberry32 State Rollback)
In classic 2048, every move spawns a 2 (90%) or a 4 (10%) on a random empty cell. If you add an Undo button while keeping Math.random(), players can spam Undo until they get a lucky 4 in a corner.
Instead, every game stores a compact Mulberry32 integer seed right inside the immutable move snapshot:
export function nextRand(seed: number): [number, number] { let t = (seed + 0x6d2b79f5) | 0; t = Math.imul(t ^ (t >>> 15), t | 1); t ^= t + Math.imul(t ^ (t >>> 7), t | 61); const out = ((t ^ (t >>> 14)) >>> 0) / 4294967296; return [out, (seed + 0x6d2b79f5) | 0]; } Rolling back a move restores both the 16-cell grid and the exact PRNG state. That also enables two bonus features for free:
- Deterministic replay verification: a 300-move Daily Seed run encodes into ~75 bytes (2 bits per move) and replays deterministically server-side.
- Zero-latency post-game coaching: the coach reconstructs any move's exact board from
(seed, movesLog)without storing 300 full board snapshots.
2. Why Naive Blunder Detection Flags "Garbage-Time" Moves
My first coach version scored every move with depth-2 Expectimax and ranked mistakes by bestScore - userScore plus an end-of-game weight bonus.
It immediately produced a frustrating UX bug: on Move #222 (the final move of a doomed board), the user swiped Right and died immediately, while AI swiped Left, merged a single 2+2=4, gained +4 points, and also died one move later. Yet the coach proudly highlighted Move #222 as #1 Blunder instead of Move #208 where the game was actually lost!
The fix was a two-stage turning-point filter:
- Collect up to 8 spaced candidate mistakes (at least 5 moves apart).
- Simulate a 3-move Expectimax branch (
buildBranchFrames) from each candidate. - Heavily penalize (
-140) and deprioritize any move where AI also hitsover === truewithin 2 moves, while boosting real rescues (userDiedSoon && !aiAlsoDied,+90) and positions where AI keeps>= 2open cells.
Suddenly #1 jumped straight to the real mid-endgame turning point where changing one swipe actually broke the board open.
3. Catching 90% Instant-Death Forks on 0-Empty Boards
Consider this real 0-empty board captured during testing (Left and Right are completely locked):
[ 2, 8, 4, 2 ] [ 4, 128, 256, 8 ] [ 32, 2, 8, 4 ] [ 4, 2, 4, 2 ] Only Up and Down are legal via the vertical 2+2 in column 2:
- Swiping Up: merges
2+2=4atR3C2, openingR4C2inside[4, _, 4, 2]. When a2spawns there (90% chance), zero legal moves remain — instant Game Over. - Swiping Down: merges
2+2=4into[4, 4, 4, 2]while openingR1C2between2and4([2, _, 4, 2]). Whether a2or a4spawns (100% probability), the next horizontal swipe guarantees a double-row merge, leading toDown -> Right -> Up -> Left(4 open cells +128evacuated onto the left wall).
Because Expectimax explicitly evaluates chance nodes (0.9 * E(spawn=2) + 0.1 * E(spawn=4)), depth-3 search separates Down from Up instantly.
4. Turning Real Dead Boards Into Deterministic Playable Drills
Static strategy guides tell beginners "keep your biggest tile in a corner", which falls apart the moment a real board gets chaotic.
We built a declarative drill format supporting an optional deterministic spawnAfter: { index, value } field, paired with a real-time 4-direction analyzer (analyzeDrillStepDirections) that evaluates Up / Down / Left / Right live on every step so learners see why the trap direction loses before or after swiping.
Even better: after any 4x4 run in the browser, players can click Save to Endgame Lab inside the AI Coach to convert their own fatal turning point straight into a deterministic playable drill.
Try It Live & Grab the Open-Source Dataset
- Open-source Expectimax evaluator + 12 verified drills (MIT, zero deps): github.com/walle-liao/2048-expectimax-endgame-drills
- Play all 12 interactive rescue drills: 2048 Crown Strategy & Endgame Drills
- Test your own stuck board in the browser Expectimax solver: 2048 Crown AI Solver
If you've built Expectimax or N-tuple network evaluators for 2048, I'd love to hear which heuristic weights gave you the cleanest late-game corner discipline!

