2048 was created by Gabriele Cirulli in March 2014 over a weekend, based loosely on Threes! by Asher Vollmer and Greg Wohlwend. Within weeks it had received 4 million visits. The game's appeal is its clean elegance — a 4×4 grid, powers of two, a single merge rule — combined with surprising strategic depth. Understanding how 2048 works from a programming perspective reveals data structures, game state management, and algorithm design in a form that is immediately tangible. This guide examines the mathematics and computer science behind the game.
The Core Rules and Mathematics
The board is a 4×4 grid of 16 cells. Each cell is either empty or contains a power of 2: 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, or 2048. On each turn, the player slides all tiles in one of four directions (up, down, left, right). When two tiles with the same value collide during a slide, they merge into a single tile with double the value. After each move, a new tile (value 2 or 4) appears in a random empty cell. The game ends when the board is full and no valid moves remain, or when a 2048 tile is created.
The "2048" in the name is 2^11 — the eleventh power of two. Reaching it from an initial 2-tile requires at least 11 doublings. Since each doubling requires a merge, and each merge requires two identical tiles, the minimum number of moves to reach 2048 starting from a single 2-tile involves a complex series of merges. In practice, the grid fills with many lower-value tiles simultaneously, and the path to 2048 involves managing the entire board state rather than focusing on a single tile's progression.
The maximum possible score (if you filled every cell with the maximum values before merging) would involve a 131072 tile (2^17), which is the theoretical maximum in the 4×4 grid. Reaching 2048 is achievable by most players who understand basic strategy. Reaching 4096 or higher requires significant skill and favourable random tile placement. Scores above 100,000 are considered excellent; the current world record is over 3 million.
The Merge Algorithm
The core computational problem in 2048 is implementing the tile-sliding and merging correctly. Consider the "left" slide operation on a single row. The algorithm must: compact all non-zero values to the left (removing gaps), then merge adjacent equal values (once per turn), then compact again to fill any gaps left by merges.
A critical rule: each tile can merge at most once per move. A row of [2, 2, 2, 2] slides left to produce [4, 4, 0, 0], not [8, 0, 0, 0]. The first pair merges, then the second pair merges, but the resulting 4s cannot merge again in the same move. Implementing this requires tracking which tiles have already merged in the current turn.
The slide for other directions (right, up, down) is implemented by transforming the grid, applying the left-slide algorithm, and transforming back. Right slide: reverse each row, apply left-slide, reverse each row back. Up slide: transpose the matrix, apply left-slide, transpose back. Down slide: transpose and reverse each column, apply left-slide, transpose and reverse back. This approach means only one directional algorithm needs to be implemented correctly, and all four directions derive from it through transformation.
Grid State Representation
The game state is a 4×4 array of integers. Zero represents empty cells. Non-zero values are always powers of two. Many implementations store the exponent rather than the value — storing 3 instead of 8, 11 instead of 2048 — which saves memory and makes certain computations easier (the exponent of a merged tile is simply the exponent + 1). The display layer converts exponents back to values for rendering.
State representation affects algorithm efficiency. For AI players using search algorithms, a compact state representation that fits in a 64-bit integer (4 bits per cell × 16 cells = 64 bits) enables highly efficient state comparison, hashing, and storage. Each 4-bit nibble stores the exponent value (0–15), supporting tiles from 0 to 2^15 = 32768. This bitboard representation allows the full board state to be encoded in a single 64-bit integer and compared with a single operation.
Winning Strategy: The Corner Strategy
The most reliable human strategy for reaching 2048 is the "corner strategy": keep your highest-value tile in one corner and build a decreasing sequence of values extending from that corner in an L or snake pattern. This ensures that high-value merges are always possible without disrupting the overall board order. The fundamental principle is that the highest tile should never need to move from its corner — all building happens around it.
The strategy breaks down when a new tile appears in the wrong location (which happens randomly). Good players minimise this risk by avoiding moves that expose the corner to random tile placement. This means preferring slide directions that keep the highest tile safely in its corner and treating diagonal arrangements as unrecoverable situations to be mitigated rather than exploited.
AI Strategies for 2048
Solving 2048 optimally is a difficult computational problem because tile placement is random. The game tree has a stochastic element — after each player move, a random tile appears, with 90% probability of being a 2 and 10% probability of being a 4, at a random empty cell. This means the game tree is an expectimax tree, not a standard minimax tree: instead of minimising an opponent's choices, you are computing the expected value across all possible random tile placements.
The strongest 2048 AI implementations use expectimax search with a carefully designed evaluation function. The evaluation function scores board states based on: the maximum tile value, the number of empty cells, the smoothness of the gradient (how monotonically values decrease from the corner), and the mergeability of adjacent tiles. Combining these factors into a single score allows the expectimax search to identify the move that maximises expected board quality several steps ahead.
Simple heuristic AIs (without search depth) can reach 2048 reliably using only the gradient and empty-cell metrics. Deep expectimax search with optimised evaluation can reach the 4096 tile reliably and occasionally reach 8192 or higher. The randomness of tile placement means even perfect play cannot guarantee winning every game — the best AIs win approximately 90–95% of games to the 2048 tile and around 70% to the 4096 tile.
Implementation in JavaScript
A basic 2048 implementation in JavaScript requires a grid data structure (a 2D array or flat array with index arithmetic), a slide-and-merge function for each direction, a random tile spawner that selects empty cells and places 2 or 4, a win/lose detection function, and a rendering function that updates the DOM or canvas based on the current grid state.
The entire game logic fits in under 150 lines of clean JavaScript. CSS handles the visual presentation — CSS custom properties or computed class names for tile colors, CSS transitions for the sliding animation. Keyboard event listeners capture arrow key inputs; touch event listeners capture swipe gestures for mobile. Our browser-based 2048 game implements all of this with no external dependencies — pure HTML, CSS, and JavaScript that runs immediately in any browser.