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

Solution 424882

Submitted on 1 Apr 2014 by Tim

Correct

109Size
Leading solution size is 94.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test
Code Input and Output
1
Pass
 
%%
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 ];
assert(isequal(nextMove(board,dir),newBoard))

                    
2
Pass
 
%%
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 ];
assert(isequal(nextMove(board,dir),newBoard))

                    
3
Pass
 
%%
board = [ ...
    0     8     0     4
    2    64    16     2
    2     8     8    16
    4     2     2     4 ];
dir = 'left';
newBoard = [ ...
    8     4     0     0
    2    64    16     2
    2    16    16     0
    4     4     4     0 ];
assert(isequal(nextMove(board,dir),newBoard))

                    
4
Pass
 
%%
 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 ]
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