2048.html
changeset 94 8bbd7570f437
parent 88 97acd9f4288b
child 95 804e751a3b7f
equal deleted inserted replaced
91:5691aa26d0c8 94:8bbd7570f437
    77       margin: 1px 4px;
    77       margin: 1px 4px;
    78       padding: 2px;
    78       padding: 2px;
    79       border: 1px solid tan;
    79       border: 1px solid tan;
    80       border-radius: 4px;
    80       border-radius: 4px;
    81     }
    81     }
    82     div.ai > div.option > input.positive, div.settings > div.setting > input {
    82     div.ai > div.option > input.positive, div.settings > div.setting > input, div.ai-control input.positive {
    83       text-align: right;
    83       text-align: right;
    84       max-width: 4em;
    84       max-width: 4em;
    85       margin-right: 2px;
    85       margin-right: 2px;
    86     }
    86     }
    87     .clearfix {
    87     .clearfix {
   159     </div>
   159     </div>
   160     <div class="ai-control">
   160     <div class="ai-control">
   161       <button id="suggest">Suggest</button>
   161       <button id="suggest">Suggest</button>
   162       <button id="step">Step</button>
   162       <button id="step">Step</button>
   163       <button id="finish">Finish</button>
   163       <button id="finish">Finish</button>
       
   164     </div>
       
   165     <div class="ai-control">
       
   166       <button id="until">Continue</button>
       
   167       until <input type="text" class="positive" id="until-score" value="10000"> score
       
   168       and <input type="text" class="positive" id="until-max-value" value="9"> max value
   164     </div>
   169     </div>
   165     <div class="clearfix"></div>
   170     <div class="clearfix"></div>
   166     <div class="move-control">
   171     <div class="move-control">
   167       <table>
   172       <table>
   168         <tr>
   173         <tr>
   482       ui.game.clearMessage();
   487       ui.game.clearMessage();
   483       if (ui.game.checkGameOver())
   488       if (ui.game.checkGameOver())
   484         return false;
   489         return false;
   485       return true;
   490       return true;
   486     }
   491     }
   487     ui.game.finishStep = function() {
   492     ui.game.refresh = function() {
   488       board.putRandom(ui.board.position);
       
   489       ui.board.turn++;
       
   490       ui.board.update(ui.board.position);
   493       ui.board.update(ui.board.position);
   491       ui.score.update(ui.board.position, ui.board.turn);
   494       ui.score.update(ui.board.position, ui.board.turn);
   492       localStorage.savedBoard = JSON.stringify(ui.board.position);
   495       localStorage.savedBoard = JSON.stringify(ui.board.position);
   493       localStorage.savedTurn = ui.board.turn;
   496       localStorage.savedTurn = ui.board.turn;
       
   497     }
       
   498     ui.game.finishStep = function() {
       
   499       board.putRandom(ui.board.position);
       
   500       ui.board.turn++;
       
   501       ui.game.refresh();
   494     }
   502     }
   495 
   503 
   496     ////////////////////////////////////////////////////////////////
   504     ////////////////////////////////////////////////////////////////
   497     // Actions.
   505     // Actions.
   498 
   506 
   610       ui.score.speed(parseFloat((step*1000.0/(tsTo-tsFrom)).toPrecision(3)), step);
   618       ui.score.speed(parseFloat((step*1000.0/(tsTo-tsFrom)).toPrecision(3)), step);
   611       ui.game.setMessage("Game over!");
   619       ui.game.setMessage("Game over!");
   612       ui.ai.current && ui.ai.current.cleanup();
   620       ui.ai.current && ui.ai.current.cleanup();
   613     }
   621     }
   614     document.getElementById("finish").addEventListener("click", ui.action.finish, false);
   622     document.getElementById("finish").addEventListener("click", ui.action.finish, false);
       
   623 
       
   624     ui.action.until = function() {
       
   625       if ( ! ui.ai.current) {
       
   626         ui.game.setMessage('Select AI!');
       
   627         return;
       
   628       }
       
   629       if ( ! ui.game.beginStep())
       
   630         return;
       
   631       var step = 0;
       
   632       var safeBdr = board.create();
       
   633       var tsFrom = new Date().getTime();
       
   634       var scoreLimit = parseInt(document.getElementById("until-score").value);
       
   635       if (!isFinite(scoreLimit) || scoreLimit < 0) {
       
   636         scoreLimit = 1;
       
   637         document.getElementById("until-score").value = scoreLimit;
       
   638       }
       
   639       var maxValLimit = parseInt(document.getElementById("until-max-value").value);
       
   640       if (!isFinite(maxValLimit) || maxValLimit < 0 || maxValLimit > 13) {
       
   641         maxValLimit = 1;
       
   642         document.getElementById("until-max-value").value = maxValLimit;
       
   643       }
       
   644       while (true) {
       
   645         if (board.gameOver(ui.board.position)) {
       
   646           ui.game.setMessage("Game over!");
       
   647           break;
       
   648         }
       
   649         var stat = board.score(ui.board.position);
       
   650         if (stat.score >= scoreLimit && stat.max >= maxValLimit)
       
   651           break;
       
   652         board.copy(ui.board.position, safeBdr);
       
   653         var move = ui.ai.current.analyse(safeBdr);
       
   654         if (typeof move === 'undefined') {
       
   655           ui.game.setMessage("I don't know how to move!");
       
   656           return;
       
   657         }
       
   658         var updated = board.move[move].call(null, ui.board.position);
       
   659         if (updated) {
       
   660           board.putRandom(ui.board.position);
       
   661         } else {
       
   662           ui.game.finishStep();
       
   663           ui.game.setMessage("Wrong move!");
       
   664           return;
       
   665         }
       
   666         step++; 
       
   667       }
       
   668       var tsTo = new Date().getTime();
       
   669       ui.game.refresh();
       
   670       ui.score.speed(parseFloat((step*1000.0/(tsTo-tsFrom)).toPrecision(3)), step);
       
   671       ui.ai.current && ui.ai.current.cleanup();
       
   672     }
       
   673     document.getElementById("until").addEventListener("click", ui.action.until, false);
   615 
   674 
   616     ////////////////////////////////////////////////////////////////
   675     ////////////////////////////////////////////////////////////////
   617     // Register AIs.
   676     // Register AIs.
   618 
   677 
   619     ui.ai = {};
   678     ui.ai = {};