Home / Showcases / 2048 tiles

Web game · One line

How does 2048 pick the next tile?

After every move a new tile appears. Players argue about how often it is a 4. The game's script settles it in one line.

Ask your agent

Your coding agent

›Use REP on the 2048 game page open in my Chrome. Where does a new tile get its value, and how often is it a 4? Show the code.

REP lists the scripts the page loaded and searches them by what they do: the function that runs after each move and writes a value into an empty cell is the spawner.

The rule in the game's script

game_manager.js · addRandomTileSource, lines 69-76 ↗
GameManager.prototype.addRandomTile = function () {
  if (this.grid.cellsAvailable()) {
    var value = Math.random() < 0.9 ? 2 : 4;
    var tile = new Tile(this.grid.randomAvailableCell(), value);
    this.grid.insertTile(tile);
  }
};

A new tile is a 2 nine times in ten and a 4 one time in ten, on any empty cell with equal chance. The game starts with two such tiles (startTiles = 2). Nothing depends on your score or the board: the odds never change.

Spawn some tiles

Tiles0
2—
4—

The same rule as the game. A thousand spawns land near 90 / 10; a single start shows two 4s about one time in a hundred.

Why it matters

Strategy guides tell you to plan for the 4. The code says how often: one spawn in ten. A bot that plays 2048 can now weigh every move by the real odds instead of a guess.

2048 is open source (MIT); the lines above are quoted from its public repository with a link to the exact revision.