cd /news/artificial-intelligence/building-a-browser-side-2048-expecti… · home › topics › artificial-intelligence › article
[ARTICLE · art-177935] src=dev.to ↗ pub= topic=artificial-intelligence verified=true sentiment=↑ positive

Building a Browser-Side 2048 Expectimax AI Coach: Catching 90% Death Traps & Filtering Garbage-Time Blunders

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.

recommendation

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.

more in playbook: Designing agent systems
by read3 min views5 publishedNov 29, 2026

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.

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:

(seed, movesLog) without storing 300 full board snapshots. 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:

buildBranchFrames) from each candidate.-140) and deprioritize any move where over === true within 2 movesuserDiedSoon && !aiAlsoDied, +90) and positions where AI keeps >= 2 open cells. Suddenly #1 jumped straight to the real mid-endgame turning point where changing one swipe actually broke the board open.

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:

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). Because Expectimax explicitly evaluates chance nodes (0.9 * E(spawn=2) + 0.1 * E(spawn=4)), depth-3 search separates Down from Up instantly.

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.

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!

── more in #artificial-intelligence 4 stories · sorted by recency
── more on @2048 crown 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/building-a-browser-s…] indexed:0 read:3min 2026-11-29 · —