2048.js
// Merge adjacent items if they are the same and larger than 0.
Merge adjacent items if they are the same and larger than 0.
Merge adjacent items if they are the same and larger than 0 .
function merge(matrix, d, callback) {
        var i = 0, j = 0, size = matrix.length, singleStepScore = 0,
            isFun = (callback instanceof Function);
        switch (d) {
            case 'left':
                for (i = 0; i < size; i++) {
                    for (j = 0; j < size - 1; j++) {
                        if (matrix[i][j] > 0 && matrix[i][j] === matrix[i][j + 1]) {
                            matrix[i][j] *= 2;
                            singleStepScore += matrix[i][j];
                            matrix[i][j + 1] = 0;
                        }
                    }
                }
                break;
            case 'right':
                for (i = 0; i < size; i++) {
                    for (j = size - 1; j > 0; j--) {
                        if (matrix[i][j] > 0 && matrix[i][j] === matrix[i][j - 1]) {
                            matrix[i][j] *= 2;
                            singleStepScore += matrix[i][j];
                            matrix[i][j - 1] = 0;
                        }
                    }
                }
                break;
            case 'up':
                for (i = 0; i < size; i++) {
                    for (j = 0; j < size - 1; j++) {
                        if (matrix[j][i] > 0 && matrix[j][i] === matrix[j + 1][i]) {
                            matrix[j][i] *= 2;
                            singleStepScore += matrix[j][i];
                            matrix[j + 1][i] = 0;
                        }
                    }
                }
                break;
            case 'down':
                for (i = 0; i < size; i++) {
                    for (j = size - 1; j > 0; j--) {
                        if (matrix[j][i] > 0 && matrix[j][i] === matrix[j - 1][i]) {
                            matrix[j][i] *= 2;
                            singleStepScore += matrix[j][i];
                            matrix[j - 1][i] = 0;
                        }
                    }
                }
                break;
        }
        isFun ? callback(singleStepScore) : '';
    }
function merge ( matrix , d , callback ) { var i = 0 , j = 0 , size = matrix . length , singleStepScore = 0 , isFun = ( callback instanceof Function ) ; switch ( d ) { case 'left' : for ( i = 0 ; i < size ; i ++ ) { for ( j = 0 ; j < size - 1 ; j ++ ) { if ( matrix [ i ] [ j ] > 0 && matrix [ i ] [ j ] === matrix [ i ] [ j + 1 ] ) { matrix [ i ] [ j ] *= 2 ; singleStepScore += matrix [ i ] [ j ] ; matrix [ i ] [ j + 1 ] = 0 ; } } } break ; case 'right' : for ( i = 0 ; i < size ; i ++ ) { for ( j = size - 1 ; j > 0 ; j -- ) { if ( matrix [ i ] [ j ] > 0 && matrix [ i ] [ j ] === matrix [ i ] [ j - 1 ] ) { matrix [ i ] [ j ] *= 2 ; singleStepScore += matrix [ i ] [ j ] ; matrix [ i ] [ j - 1 ] = 0 ; } } } break ; case 'up' : for ( i = 0 ; i < size ; i ++ ) { for ( j = 0 ; j < size - 1 ; j ++ ) { if ( matrix [ j ] [ i ] > 0 && matrix [ j ] [ i ] === matrix [ j + 1 ] [ i ] ) { matrix [ j ] [ i ] *= 2 ; singleStepScore += matrix [ j ] [ i ] ; matrix [ j + 1 ] [ i ] = 0 ; } } } break ; case 'down' : for ( i = 0 ; i < size ; i ++ ) { for ( j = size - 1 ; j > 0 ; j -- ) { if ( matrix [ j ] [ i ] > 0 && matrix [ j ] [ i ] === matrix [ j - 1 ] [ i ] ) { matrix [ j ] [ i ] *= 2 ; singleStepScore += matrix [ j ] [ i ] ; matrix [ j - 1 ] [ i ] = 0 ; } } } break ; } isFun ? callback ( singleStepScore ) : '' ; }
Merge adjacent items if they are the same and larger than 0.
Merge adjacent items if they are the same and larger than 0 .