Cody

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

75.0% Correct | 25.0% Incorrect
Last solution submitted on Mar 02, 2016

Problem Comments

Solution Comments

Tags

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!

Join the 15-year community celebration.

Play games and win prizes!

Learn more