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

> Source: <https://dev.to/walleliao/building-a-browser-side-2048-expectimax-ai-coach-catching-90-death-traps-filtering-garbage-time-1o77>
> Published: 2026-11-29 00:35:00+00:00

**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 the playbook "Designing agent systems": <https://wpnews.pro/playbooks/agent-architecture>

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.

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:

``` js
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 moves`userDiedSoon && !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!
