2048.js
// Move item of matrix to make all numbers(>0) to go one side.
Move item of matrix to make all numbers(>0) to go one side.
Move item of matrix to make all numbers ( > 0 ) to go one side .
function move(matrix, d) {
        var filled = getFilledItems(matrix);
        var i = 0, j = 0, size = matrix.length, line = [], lineLength = 0;
        switch (d) {
            case 'left':
                for (i = 0; i < size; i++) {
                    line = filled.filter(function (item) {
                        return item.flag[0] === i;
                    });
                    lineLength = line.length;
                    for (j = 0; j < lineLength; j++) {
                        matrix[line[j].flag[0]][line[j].flag[1]] = 0;
                        matrix[i][j] = line[j].value;
                    }
                }
                break;
            case 'right':
                for (i = 0; i < size; i++) {
                    line = filled.filter(function (item) {
                        return item.flag[0] === i;
                    });
                    lineLength = line.length;
                    for (j = 0; j < lineLength; j++) {
                        matrix[line[lineLength - j - 1].flag[0]][line[lineLength - j - 1].flag[1]] = 0;
                        matrix[i][size - j - 1] = line[lineLength - j - 1].value;
                    }
                }
                break;
            case 'up':
                for (i = 0; i < size; i++) {
                    line = filled.filter(function (item) {
                        return item.flag[1] === i;
                    });
                    lineLength = line.length;
                    for (j = 0; j < lineLength; j++) {
                        matrix[line[j].flag[0]][line[j].flag[1]] = 0;
                        matrix[j][i] = line[j].value;
                    }
                }
                break;
            case 'down':
                for (i = 0; i < size; i++) {
                    line = filled.filter(function (item) {
                        return item.flag[1] === i;
                    });
                    lineLength = line.length;
                    for (j = 0; j < lineLength; j++) {
                        matrix[line[lineLength - j - 1].flag[0]][line[lineLength - j - 1].flag[1]] = 0;
                        matrix[size - j - 1][i] = line[lineLength - j - 1].value;
                    }
                }
                break;
        }
    }
function move ( matrix , d ) { var filled = getFilledItems ( matrix ) ; var i = 0 , j = 0 , size = matrix . length , line = [ ] , lineLength = 0 ; switch ( d ) { case 'left' : for ( i = 0 ; i < size ; i ++ ) { line = filled . filter ( function ( item ) { return item . flag [ 0 ] === i ; } ) ; lineLength = line . length ; for ( j = 0 ; j < lineLength ; j ++ ) { matrix [ line [ j ] . flag [ 0 ] ] [ line [ j ] . flag [ 1 ] ] = 0 ; matrix [ i ] [ j ] = line [ j ] . value ; } } break ; case 'right' : for ( i = 0 ; i < size ; i ++ ) { line = filled . filter ( function ( item ) { return item . flag [ 0 ] === i ; } ) ; lineLength = line . length ; for ( j = 0 ; j < lineLength ; j ++ ) { matrix [ line [ lineLength - j - 1 ] . flag [ 0 ] ] [ line [ lineLength - j - 1 ] . flag [ 1 ] ] = 0 ; matrix [ i ] [ size - j - 1 ] = line [ lineLength - j - 1 ] . value ; } } break ; case 'up' : for ( i = 0 ; i < size ; i ++ ) { line = filled . filter ( function ( item ) { return item . flag [ 1 ] === i ; } ) ; lineLength = line . length ; for ( j = 0 ; j < lineLength ; j ++ ) { matrix [ line [ j ] . flag [ 0 ] ] [ line [ j ] . flag [ 1 ] ] = 0 ; matrix [ j ] [ i ] = line [ j ] . value ; } } break ; case 'down' : for ( i = 0 ; i < size ; i ++ ) { line = filled . filter ( function ( item ) { return item . flag [ 1 ] === i ; } ) ; lineLength = line . length ; for ( j = 0 ; j < lineLength ; j ++ ) { matrix [ line [ lineLength - j - 1 ] . flag [ 0 ] ] [ line [ lineLength - j - 1 ] . flag [ 1 ] ] = 0 ; matrix [ size - j - 1 ] [ i ] = line [ lineLength - j - 1 ] . value ; } } break ; } }
Move item of matrix to make all numbers(>0) to go one side.
Move item of matrix to make all numbers ( > 0 ) to go one side .