2048.js
// Find an empty item's coordinate randomly.
Find an empty item's coordinate randomly.
Find an empty item ' s coordinate randomly .
function findEmptyItemCoord(matrix) {
        var len = 0, flag = 0, emptySet = [], size = matrix.length;
        for (i = 0; i < size; i++) {
            for (j = 0; j < size; j++) {
                if (matrix[i][j] === 0) {
                    emptySet.push([i, j]);
                }
            }
        }
        len = emptySet.length;
        if (len === 0) {
            return [];
        } else if (len === 1) {
            return emptySet[0];
        }
        flag = Math.floor(Math.random() * len);
        return emptySet[flag];
    }
function findEmptyItemCoord ( matrix ) { var len = 0 , flag = 0 , emptySet = [ ] , size = matrix . length ; for ( i = 0 ; i < size ; i ++ ) { for ( j = 0 ; j < size ; j ++ ) { if ( matrix [ i ] [ j ] === 0 ) { emptySet . push ( [ i , j ] ) ; } } } len = emptySet . length ; if ( len === 0 ) { return [ ] ; } else if ( len === 1 ) { return emptySet [ 0 ] ; } flag = Math . floor ( Math . random ( ) * len ) ; return emptySet [ flag ] ; }
Find an empty item's coordinate randomly.
Find an empty item ' s coordinate randomly .