ai newsDi., 1. Dez.

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

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.

dev.toSonntag 01:35 · 29.11.3 Min. Lesezeit
Bild: dev.to

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 ]; } Enter fullscreen mode Exit fullscreen mode Rolling back a move restores both the 16-cell grid and the exact PRNG state. That also enables two bonus features for free:

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:

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 ] Enter fullscreen mode Exit fullscreen mode Only Up and Down are legal via the vertical 2+2 in column 2:

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!

Quelle: dev.to (dev.to). Original öffnen