Skip to main content
ilovecalcs logoilovecalcs.

Solver · Live

2048 Solver — Expectimax AI calculates your next move.

Mirror your real 2048 board by clicking each cell and entering its tile value. Hit Calculate Best Move and the AI runs a depth-5 Expectimax search — accounting for all possible tile spawns — to find the statistically optimal swipe direction.

How it worksExpectimax AI

Score

0

Moves

0

Best Tile

Best Move

Set up your board first

Auto-Play

Click New Game to start with a random board, or manually set tiles.

Manual sync workflow

  1. 1.Click a cell → pick its tile value to mirror your real game.
  2. 2.Calculate → AI picks the best swipe direction.
  3. 3.Press Apply This Move to update the board automatically.
  4. 4.Click the newly spawned tile cell and set its value (2 or 4).
  5. 5.Repeat until 2048!

Algorithm guide

Why Expectimax beats Minimax for 2048 and how it works.

The problem with Minimax

Minimax is designed for adversarial two-player games like chess or checkers, where the opponent actively tries to minimise your score. In 2048, the tile spawner is not adversarial — it places tiles randomly (90% chance of a 2, 10% chance of a 4) at a uniformly random empty position. Assuming the worst case (as Minimax does) would produce overly defensive play and miss many opportunities.

Expectimax replaces the Minimax "minimiser" with a chance node that computes the expected value across all possible random outcomes, weighted by their probability. This correctly models the game's stochastic nature and produces dramatically better move quality.

The Expectimax search tree

The tree alternates between two types of nodes:

  • Max nodes (player's turn): The player chooses the move (up/down/left/right) that maximises the expected score. We evaluate all valid moves and take the maximum.
  • Chance nodes (tile spawn): After each player move, the game spawns a tile. We enumerate every empty cell (the position is equally likely) and both possible values (2 at 90%, 4 at 10%). The expected value is the probability-weighted average over all these outcomes.

At depth 5, the tree explores thousands of game states. To keep computation fast, this solver runs the search in a Web Worker (a background thread), so the page never freezes, and applies a probability cutoff: any branch whose cumulative probability drops below 0.01% is pruned, since its contribution to the expected value is negligible.

The four-pillar heuristic

When the search reaches a leaf node (maximum depth or probability cutoff), a heuristic function estimates the board's long-term value. This solver uses four components:

1. Empty tiles

More empty cells means more freedom to manoeuvre. Boards with many empty cells score significantly higher. The bonus grows logarithmically: going from 1 to 2 empty tiles matters more than going from 10 to 11. Weight: 270 × log(empty + 1).

2. Monotonicity

A good 2048 board has tiles arranged in a monotonically increasing or decreasing order along each row and column, so merges can cascade. We evaluate the board in all four directions and take the best-fitting orientation. Tiles that break the monotonic order are penalised by the log2 magnitude of the violation. Weight: 47 × monotonicity_score.

3. Snake pattern (corner heuristic)

The classic 2048 strategy is to build a snake: anchor the highest tile in one corner, then arrange tiles in decreasing order along a zigzag path (e.g., top-left → top-right → second-row-right → second-row-left). We score every cell by multiplying its log2 value by a positional weight from this snake matrix. All 8 orientations (4 rotations × 2 reflections) are tried, and the best-fitting one is used, so the solver adapts to whichever corner the board is biased toward.

4. Smoothness

Smooth boards have tiles of similar magnitude next to each other, which enables easy merges. For each adjacent pair, the penalty is |log2(a) − log2(b)|. Lower is better. Smoothness prevents the board from fragmenting into many small tiles that cannot merge. Weight: 0.1 × smoothness_score.

Practical 2048 strategy

Even without the solver, these principles will dramatically improve your score:

  • Pick a corner and stay there. Always keep your highest tile in one corner. Never swipe in the direction that would move it away from that corner.
  • Build a snake from your corner. Arrange tiles in descending order: 1024 → 512 → 256 → 128 along the top row, then 64 → 32 → 16 → 8 on the second row, and so on.
  • Only swipe away from your wall when forced. If your corner is in the top-left, almost all your moves should be Up or Left. Down and Right should be rare, emergency moves.
  • Keep the board sparse. Avoid filling the board. A cluttered board cuts off merge opportunities. If you have fewer than 4 empty cells, prioritise merges aggressively.
  • Build toward 2048, then keep going. Once you reach 2048, the game continues. Use the same strategy to build toward 4096 and beyond.

Why the AI sometimes makes surprising moves

Occasionally the solver will recommend a move that looks suboptimal to human eyes. This usually happens for one of two reasons:

  • Long-range planning: A move that creates a temporary gap may unlock a much higher-value merge two moves later. The depth-5 search sees this; a human looking one move ahead does not.
  • Expected value vs. guaranteed value: The solver maximises the average outcome over all tile spawns. A "risky" move may be the right choice if the expected gain outweighs the small probability of a bad spawn.

At depth 5, this solver achieves the 2048 tile in over 90% of games and regularly reaches 4096 and beyond. The algorithm is the same class used by the best open-source 2048 AIs, adapted for real-time interaction.