{"slug": "building-a-browser-side-2048-expectimax-ai-coach-catching-90-death-traps-garbage", "title": "Building a Browser-Side 2048 Expectimax AI Coach: Catching 90% Death Traps & Filtering Garbage-Time Blunders", "summary": "A developer built 2048 Crown, a browser-side, zero-LLM Expectimax AI Coach for the game 2048 that reviews runs like a chess post-game engine. The coach uses a seeded Mulberry32 PRNG stored in immutable move snapshots so undo restores exact game state, and a two-stage turning-point filter that fixed a bug where the final, doomed move was flagged as the top blunder instead of the real mid-endgame mistake. The project also converts fatal 0-empty boards into deterministic training drills with a live four-direction analyzer.", "body_md": "Most browser 2048 clones stop at basic slide logic. When building [2048 Crown](https://2048crown.com/), 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.\n\nHere are four surprisingly tricky engineering and UX problems I ran into — and how I solved each one in pure TypeScript.\n\nIn 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.\n\nInstead, every game stores a compact **Mulberry32** integer seed right inside the immutable move snapshot:\n\n``` js\nexport function nextRand(seed: number): [number, number] {\n  let t = (seed + 0x6d2b79f5) | 0;\n  t = Math.imul(t ^ (t >>> 15), t | 1);\n  t ^= t + Math.imul(t ^ (t >>> 7), t | 61);\n  const out = ((t ^ (t >>> 14)) >>> 0) / 4294967296;\n  return [out, (seed + 0x6d2b79f5) | 0];\n}\n```\n\nRolling back a move restores both the 16-cell grid **and** the exact PRNG state. That also enables two bonus features for free:\n\n`(seed, movesLog)` without storing 300 full board snapshots.\nMy first coach version scored every move with depth-2 Expectimax and ranked mistakes by `bestScore - userScore` plus an end-of-game weight bonus.\n\nIt 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!\n\nThe fix was a **two-stage turning-point filter**:\n\n`buildBranchFrames`) from each candidate.`-140`) and deprioritize any move where `over === true` within 2 moves`userDiedSoon && !aiAlsoDied`, `+90`) and positions where AI keeps `>= 2` open cells.\nSuddenly `#1` jumped straight to the real mid-endgame turning point where changing one swipe actually broke the board open.\n\nConsider this real 0-empty board captured during testing (`Left` and `Right` are completely locked):\n\n```\n[  2,   8,   4,   2 ]\n[  4, 128, 256,   8 ]\n[ 32,   2,   8,   4 ]\n[  4,   2,   4,   2 ]\n```\n\nOnly `Up` and `Down` are legal via the vertical `2+2` in column 2:\n\n`2+2=4` at `R3C2`, opening `R4C2` inside `[4, _, 4, 2]`. When a `2` spawns there (`[4, 4, 4, 2]` while opening `R1C2` between `4` (`[2, _, 4, 2]`). Whether a `Down -> Right -> Up -> Left` (4 open cells + `128` evacuated onto the left wall).\nBecause Expectimax explicitly evaluates chance nodes (`0.9 * E(spawn=2) + 0.1 * E(spawn=4)`), depth-3 search separates `Down` from `Up` instantly.\n\nStatic strategy guides tell beginners *\"keep your biggest tile in a corner\"*, which falls apart the moment a real board gets chaotic.\n\nWe 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.\n\nEven 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.\n\nIf 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!", "url": "https://wpnews.pro/news/building-a-browser-side-2048-expectimax-ai-coach-catching-90-death-traps-garbage", "canonical_source": "https://dev.to/walleliao/building-a-browser-side-2048-expectimax-ai-coach-catching-90-death-traps-filtering-garbage-time-1o77", "published_at": "2026-11-29 00:35:00+00:00", "updated_at": "2026-11-29 00:44:14.024303+00:00", "lang": "en", "topics": ["artificial-intelligence", "ai-tools", "developer-tools"], "entities": ["2048 Crown", "Mulberry32"], "also_reported_by": [], "takeaway": "Ranking 2048 mistakes by raw Expectimax score difference mislabels the final doomed move as the top blunder; adding a two-stage filter that penalizes moves where the game ends within two moves and rewards positions keeping at least two open cells surfaces the real turning point.", "recommendation": {"advice": "When scoring agent or search decisions, filter out terminal-state noise: penalize moves that end the episode within two steps and reward states that preserve optionality before ranking mistakes.", "playbook": "agent-architecture", "playbook_url": "https://wpnews.pro/playbooks/agent-architecture"}, "alternates": {"html": "https://wpnews.pro/news/building-a-browser-side-2048-expectimax-ai-coach-catching-90-death-traps-garbage", "markdown": "https://wpnews.pro/news/building-a-browser-side-2048-expectimax-ai-coach-catching-90-death-traps-garbage.md", "text": "https://wpnews.pro/news/building-a-browser-side-2048-expectimax-ai-coach-catching-90-death-traps-garbage.txt", "jsonld": "https://wpnews.pro/news/building-a-browser-side-2048-expectimax-ai-coach-catching-90-death-traps-garbage.jsonld"}}