2048 Next Move - MATLAB Cody - MATLAB Central

Problem 2265. 2048 Next Move

Difficulty:Rate

Given a board in the game 2048 (see the game here: 2048) and a direction ('up','down','left', or 'right'), move the game forward one turn.

Move and merge blocks as required by the game's rules, but for simplicity do not insert a new 2.

Example 1

 board = [ ...
    0     0     0     2
    0     0     4     4
    0     0     0    16
    0     0     0    16]
 dir = 'up'
 newBoard = [ ...
    0     0     4     2
    0     0     0     4
    0     0     0    32
    0     0     0     0 ]

Example 2

 board = [ ...
    0     2   128     4
    0    16     4    32
    0     8     0     0
    0     0     2     0 ]
 dir = 'right'
 newBoard = [ ...
    0     2   128     4
    0    16     4    32
    0     0     0     8
    0     0     0     2 ]

Example 3. Resolving Ambiguity

If we think of the directions as defining the pull of gravity, then we resolve ambiguous cases by first merging the two "lowest" blocks. See below.

 board = [ ...
    0     4     4     4
    4     4     4     0
    2     2     2     2
    0     0     0     0 ]
 dir = 'left'
 newBoard = [ ...
    8     4     0     0
    8     4     0     0
    4     4     0     0
    0     0     0     0 ]

Inspired by a suggestion from Nick Howe.

Solution Stats

45.48% Correct | 54.52% Incorrect
Last Solution submitted on May 04, 2025

Problem Comments

Solution Comments

Show comments
LLMs with MATLAB updated to support the latest OpenAI Models
Large Languge model with MATLAB, a free add-on that lets you access...
2
4

Problem Recent Solvers89

Suggested Problems

More from this Author50

Problem Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Go to top of page