rule.js
changeset 120 653ee00d2601
parent 119 e5f1930f0f19
equal deleted inserted replaced
119:e5f1930f0f19 120:653ee00d2601
    51 board.putRandom = function(brd) {
    51 board.putRandom = function(brd) {
    52     var cnt = board.freeCnt(brd);
    52     var cnt = board.freeCnt(brd);
    53     if (cnt === 0)
    53     if (cnt === 0)
    54         throw Error('You forget detect game over!');
    54         throw Error('You forget detect game over!');
    55     cnt = Math.floor(Math.random() * cnt)+1;
    55     cnt = Math.floor(Math.random() * cnt)+1;
    56     for (var i = 0; i < 4 && cnt > 0; i++) {
    56 exit:
    57         for (var j = 0; j < 4 && cnt > 0; j++) {
    57     for (var i = 0; i < 4; i++) {
       
    58         for (var j = 0; j < 4; j++) {
    58             if (brd[i][j] !== 0)
    59             if (brd[i][j] !== 0)
    59                 continue;
    60                 continue;
    60             if (cnt === 1)
    61             if (cnt === 1) {
    61                 brd[i][j] = (Math.random() > .9) ? 2 : 1;
    62                 brd[i][j] = (Math.random() > .9) ? 2 : 1;
       
    63                 break exit;
       
    64             }
    62             cnt--;
    65             cnt--;
    63         }
    66         }
    64     }
    67     }
    65 }
    68 }
    66 /* http://www.reddit.com/r/2048/comments/214njx/highest_possible_score_for_2048_warning_math */
    69 /* http://www.reddit.com/r/2048/comments/214njx/highest_possible_score_for_2048_warning_math */