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

47.9% Correct | 52.1% Incorrect
Last Solution submitted on Dec 04, 2024

Problem Comments

Solution Comments

Show comments

Problem Recent Solvers89

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