2048.html
changeset 129 e0958e521716
parent 128 673743a1290d
child 130 28694f058ea4
equal deleted inserted replaced
128:673743a1290d 129:e0958e521716
   278   </div>
   278   </div>
   279 
   279 
   280   <div id="report-area" class="area">
   280   <div id="report-area" class="area">
   281     <h1>Reports</h1>
   281     <h1>Reports</h1>
   282     <div class="control">
   282     <div class="control">
   283       <div class="option"><input type="text" id="stat-count-limit" value="100"> times</div>
   283       <div class="option">up to <input type="text" id="stat-count-limit" value="100"> times</div>
   284       <div class="option">limit to <input type="text" id="stat-time-limit" value="10"> sec</div>
   284       <div class="option">up to <input type="text" id="stat-time-limit" value="10"> sec</div>
   285       <button id="statistic">Start</button>
   285       <button id="statistic-start">Start</button>
   286       <button id="statistic-clean">Clean</button>
   286       <button id="statistic-clean">Clean</button>
   287       <div class="option"><input type="checkbox" id="report-score"/> score</div>
   287       <div class="option"><input type="checkbox" id="report-score"/> score</div>
   288       <div class="option"><input type="checkbox" id="report-turn"/> turn</div>
   288       <div class="option"><input type="checkbox" id="report-turn"/> turn</div>
   289       <div class="option"><input type="checkbox" id="report-speed"/> speed</div>
   289       <div class="option"><input type="checkbox" id="report-speed"/> speed</div>
   290     </div>
   290     </div>
   886       }
   886       }
   887       var tableDom = ui.dom.table(tbl, tblCols, { tblClass: 'report-by-maxval', tblTitle: ui.ai.cfgTitle(ui.ai.currentName) });
   887       var tableDom = ui.dom.table(tbl, tblCols, { tblClass: 'report-by-maxval', tblTitle: ui.ai.cfgTitle(ui.ai.currentName) });
   888       reportDom.appendChild(tableDom);
   888       reportDom.appendChild(tableDom);
   889       reportsDom.insertBefore(reportDom, reportsDom.firstChild);
   889       reportsDom.insertBefore(reportDom, reportsDom.firstChild);
   890     }
   890     }
   891     ui.report.stat = function() {
   891     ui.report.start = function() {
   892       /* console.profile(); */
       
   893       var histo = {};
   892       var histo = {};
   894       var cnt = parseInt(document.getElementById('stat-count-limit').value);
   893       var gameCntLimit = parseInt(document.getElementById('stat-count-limit').value);
   895       if (isNaN(cnt) || !isFinite(cnt) || cnt < 1)
   894       if (isNaN(gameCntLimit) || !isFinite(gameCntLimit) || gameCntLimit < 1)
   896         cnt = 100;
   895         gameCntLimit = 100;
   897       var tsLimit = parseFloat(document.getElementById('stat-count-limit').value);
   896       var tsLimit = parseFloat(document.getElementById('stat-count-limit').value);
   898       if (isNaN(tsLimit) || !isFinite(tsLimit) || tsLimit < 1 || tsLimit > 60)
   897       if (isNaN(tsLimit) || !isFinite(tsLimit) || tsLimit < 1 || tsLimit > 60)
   899         tsLimit = 1000 * 10;
   898         tsLimit = 1000 * 10;
   900       else
   899       else
   901         tsLimit = 1000 * tsLimit;
   900         tsLimit = 1000 * tsLimit;
   902       var safeBdr = board.create();
   901       var safeBdr = board.create();
   903       var tsLimitFrom = new Date().getTime();
   902       function play() {
   904       for (var gameCnt = 0; gameCnt++ < cnt; void(0)) {
       
   905         var turn = 0;
   903         var turn = 0;
   906         var brd = board.create();
   904         var brd = board.create();
   907         board.putRandom(brd);
   905         board.putRandom(brd);
   908         var tsFrom = new Date().getTime();
   906         var tsFrom = new Date().getTime();
   909         while (!board.gameOver(brd)) {
   907         while (!board.gameOver(brd)) {
   910           board.copy(brd, safeBdr);
   908           board.copy(brd, safeBdr);
   911           var move = ui.ai.current.analyse(safeBdr);
   909           var move = ui.ai.current.analyse(safeBdr);
   912           if (ui.game.dirs.indexOf(move) < 0) {
   910           if (ui.game.dirs.indexOf(move) < 0) {
   913             ui.game.setMessage("I don't know how to move!");
   911             ui.game.setMessage("I don't know how to move!");
   914             return;
   912             throw Error("AI make illegal move '" + move + "'...");
   915           }
   913           }
   916           var updated = board.move[move].call(null, brd);
   914           var updated = board.move[move].call(null, brd);
   917           if (!updated) {
   915           if (!updated) {
   918             ui.game.setMessage("Wrong move!");
   916             ui.game.setMessage("Wrong move!");
   919             return;
   917             throw Error("AI make illegal move '" + move + "'...");
   920           }
   918           }
   921           board.putRandom(brd);
   919           board.putRandom(brd);
   922           turn++;
   920           turn++;
   923         }
   921         }
   924         var tsTo = new Date().getTime();
   922         var tsTo = new Date().getTime();
   925         ui.report.update(histo, brd, tsTo-tsFrom, turn);
   923         ui.report.update(histo, brd, tsTo-tsFrom, turn);
   926         if (tsTo - tsLimitFrom >= tsLimit)
   924       }
   927           break;
   925       var gameCnt = 0;
   928       }
   926       var tsFrom = new Date().getTime(), tsFrame = tsFrom;
   929       ui.report.fix(histo);
   927       function batch() {
   930       ui.report.show(histo, gameCnt);
   928         /* console.profile(); */
   931       /* console.profileEnd(); */
   929         while (gameCnt++ < gameCntLimit) {
   932     }
   930           play();
   933     var statisticBtn = document.getElementById('statistic');
   931           var tsTo = new Date().getTime();
   934     statisticBtn.addEventListener("click", ui.report.stat, false);
   932           if (tsTo - tsFrame > 7000) {
   935 
   933             tsFrame = tsTo;
   936     ui.report.statClean = function() {
   934             break;
       
   935           }
       
   936           if (tsTo - tsFrom >= tsLimit)
       
   937             break;
       
   938         }
       
   939         /* console.profileEnd(); */
       
   940         if (tsTo - tsFrom >= tsLimit || gameCnt >= gameCntLimit) {
       
   941           ui.report.fix(histo);
       
   942           ui.report.show(histo, gameCnt);
       
   943         } else {
       
   944           setTimeout(batch, 10);
       
   945         }
       
   946       }
       
   947       batch();
       
   948     }
       
   949     var statisticStartBtn = document.getElementById("statistic-start");
       
   950     statisticStartBtn.addEventListener("click", ui.report.start, false);
       
   951 
       
   952     ui.report.clean = function() {
   937       while (reportsDom.hasChildNodes()) {
   953       while (reportsDom.hasChildNodes()) {
   938         reportsDom.removeChild(reportsDom.lastChild);
   954         reportsDom.removeChild(reportsDom.lastChild);
   939       }
   955       }
   940     }
   956     }
   941     var statisticCleanBtn = document.getElementById('statistic-clean');
   957     var statisticCleanBtn = document.getElementById('statistic-clean');
   942     statisticCleanBtn.addEventListener("click", ui.report.statClean, false);
   958     statisticCleanBtn.addEventListener("click", ui.report.clean, false);
   943 
   959 
   944     ////////////////////////////////////////////////////////////////
   960     ////////////////////////////////////////////////////////////////
   945     // Initialise game.
   961     // Initialise game.
   946 
   962 
   947     if (localStorage.brdEngine) {
   963     if (localStorage.brdEngine) {