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

Problem 2265. 2048 Next Move

Created by Ned Gulley

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.

Tags

Problem Group

Solution Statistics

35 correct solutions 16 incorrect solutions
Last solution submitted on Dec 19, 2014

Problem Comments

Solution Comments