What is the optimal algorithm for 2048 game?

2048 is a single-player complex puzzle game. It’s played on a 4*4 grid. With each move, a new tile appears, having either 22 or 44 on it, in a random position on the grid. We have to slide numbered tiles and the same ones combine to create a tile with a number that’s the added value of the numbers on the tiles combined. The game ends when there are no more moves left.

An illustration is given below for a better understanding of the game:

2048 game
2048 game
1 of 19

We intended solely to showcase the game’s mechanics. Despite the conclusion of the slides, the game could still be ongoing. It reaches its end only when all grid spots are occupied by blocks, with no feasible moves remaining.

This game is classified as an NP-hard problem. Finding an optimal solution that guarantees optimal moves in all situations is computationally challenging. However, an algorithm called Expectimax is considered the most efficient solution for this problem.

What is the Expectimax algorithm?

Expectimax is a decision-making algorithm mostly used in scenarios where uncertainty or chance is involved. It is an extension of the Minimax Algorithm. Unlike Minimax, which takes calculated moves, Expectimax takes risks and might end up with a higher utility value in the resulting state. But on the other hand, this risk-taking nature of Expectimax might lead to a state with a lower utility value.

How Expectimax works

It constructs a game tree that represents all the possible moves and chance outcomes, then evaluates the tree nodes to determine the best possible move for a specific game state. Use the illustration below to better understand the working of the Expectimax algorithm.

Game Tree
Game Tree
1 of 10

The slides above show a tree with 3 levels:

Leaf nodes are the utility nodes, and the nodes present above the utility nodes are called chance nodes. Chance nodes calculate the average of their children, the utility nodes, giving the expected utility as the value upon them. The maximizer is at the root node, so it moves toward the maximum expected utility node and afterward toward the maximum available utility node.

Expectimax in 2048 game

It is considered an efficient approach to solving the 2048 game problem. Its trick is to think a little into the future. Before making any move, we consider what would happen if the move is made.

It follows the following steps to optimize moves in this game:

  1. Firstly, the current state is represented by representing the values and positions of the tiles on the grid.

  2. A tree is constructed to represent all the possible moves from the current state.

  3. Since a new tile appears with every move, the chance node represents the appearance of a new one in an empty cell after the player’s move.

  4. Leaf nodes represent the “goodness” of a state (using the heuristic function (its explanation is beyond the scope of this answer)) by considering the following factors:

    1. arrangement of tiles

    2. values of tiles

    3. potential for tile merges

  5. So, from the root node, the algorithm takes the move, resulting in the best-expected state.

  6. The whole process is repeated recursively after each move.

In short, it calculates an expected score for each move by considering the probability of each tile appearing and the potential outcome of each move. It chooses the move that results in the highest expected score based on that calculated expected score.

It is important to note that this approach can help you achieve high scores and perform well in most scenarios by helping you make smarter choices for playing this game, but it is still not guaranteed to make optimal moves every time you use this approach.

Wrap up

To wrap this up, 2048 is a complex game, and finding an optimal solution is difficult. None of the algorithms guarantee optimal moves in every situation. However, we use algorithms like Expectimax to make smarter choices for playing this game.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved