Problem 2265. 2048 Next Move

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

48.79% Correct | 51.21% Incorrect
Last Solution submitted on Mar 07, 2024

Problem Comments

Solution Comments

Show comments

Problem Recent Solvers84

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!