2048.html
changeset 128 673743a1290d
parent 127 4cc0421184ac
child 129 e0958e521716
equal deleted inserted replaced
127:4cc0421184ac 128:673743a1290d
   799     var reportsDom = document.getElementById('reports');
   799     var reportsDom = document.getElementById('reports');
   800 
   800 
   801     ui.report = {};
   801     ui.report = {};
   802     ui.report.statNo = 1;
   802     ui.report.statNo = 1;
   803 
   803 
   804     ui.report.stat = function() {
   804     ui.report.update = function(histo, brd, tsDelta, turn) {
   805       /* console.profile(); */
   805       var stat = board.score(brd);
       
   806       var speed = turn * 1000 / tsDelta;
       
   807       var score = stat.score;
       
   808       var max = stat.max;
       
   809       if ( ! histo[max]) {
       
   810         var row = histo[max] = { n: 1 };
       
   811         row.minScore = score; row.meanScore = score; row.maxScore = score;
       
   812         row.minSpeed = speed; row.meanSpeed = speed; row.maxSpeed = speed;
       
   813         row.minTurn = turn; row.meanTurn = turn; row.maxTurn = turn;
       
   814       } else {
       
   815         var row = histo[max];
       
   816         row.n++;
       
   817         row.minScore = Math.min(row.minScore, score);
       
   818         row.meanScore += score;
       
   819         row.maxScore = Math.max(row.maxScore, score);
       
   820         row.minSpeed = Math.min(row.minSpeed, speed);
       
   821         row.meanSpeed += speed;
       
   822         row.maxSpeed = Math.max(row.maxSpeed, speed);
       
   823         row.minTurn = Math.min(row.minTurn, turn);
       
   824         row.meanTurn += turn;
       
   825         row.maxTurn = Math.max(row.maxTurn, turn);
       
   826       }
       
   827     }
       
   828     ui.report.fix = function(histo) {
       
   829       for (var i in histo) {
       
   830         var row = histo[i];
       
   831         var n = row.n;
       
   832         row.meanScore = row.meanScore / n;
       
   833         row.meanTurn = row.meanTurn / n;
       
   834         row.meanSpeed = row.meanSpeed / n;
       
   835       }
       
   836     }
       
   837     ui.report.show = function(histo, gameCnt) {
   806       var scoreChecked = document.getElementById('report-score').checked;
   838       var scoreChecked = document.getElementById('report-score').checked;
   807       var turnChecked = document.getElementById('report-turn').checked;
   839       var turnChecked = document.getElementById('report-turn').checked;
   808       var speedChecked = document.getElementById('report-speed').checked;
   840       var speedChecked = document.getElementById('report-speed').checked;
       
   841       var reportDom = document.createElement('div');
       
   842       reportDom.classList.add('option');
       
   843       var h5Dom = document.createElement('h5');
       
   844       h5Dom.appendChild(document.createTextNode(ui.ai.currentName + " #" + ui.report.statNo++));
       
   845       reportDom.appendChild(h5Dom);
       
   846       var maxVals = Object.keys(histo).sort(function(a,b){return a - b});
       
   847       var tbl = [];
       
   848       for (var i = maxVals.length-1; i >= 0; i--) {
       
   849         var maxVal = maxVals[i];
       
   850         var row = histo[maxVals[i]];
       
   851         var perc = parseFloat((100 * row.n / gameCnt).toPrecision(3));
       
   852         var tblRow = [maxVal, row.n, perc];
       
   853         if (scoreChecked) {
       
   854           tblRow.push(row.minScore);
       
   855           var meanScore = parseFloat(row.meanScore.toPrecision(3));
       
   856           tblRow.push(meanScore);
       
   857           tblRow.push(row.maxScore);
       
   858         }
       
   859         if (turnChecked) {
       
   860           tblRow.push(row.minTurn);
       
   861           tblRow.push(Math.floor(row.meanTurn));
       
   862           tblRow.push(row.maxTurn);
       
   863         }
       
   864         if (speedChecked) {
       
   865           tblRow.push(parseFloat(row.minSpeed.toPrecision(3)));
       
   866           tblRow.push(parseFloat(row.meanSpeed.toPrecision(3)));
       
   867           tblRow.push(parseFloat(row.maxSpeed.toPrecision(3)));
       
   868         }
       
   869         tbl.push(tblRow);
       
   870       }
       
   871       var tblCols = ['maxVal', 'n', '%'];
       
   872       if (scoreChecked) {
       
   873         tblCols.push('min score');
       
   874         tblCols.push('mean score');
       
   875         tblCols.push('max score');
       
   876       }
       
   877       if (turnChecked) {
       
   878         tblCols.push('min turn');
       
   879         tblCols.push('mean turn');
       
   880         tblCols.push('max turn');
       
   881       }
       
   882       if (speedChecked) {
       
   883         tblCols.push('min speed');
       
   884         tblCols.push('mean speed');
       
   885         tblCols.push('max speed');
       
   886       }
       
   887       var tableDom = ui.dom.table(tbl, tblCols, { tblClass: 'report-by-maxval', tblTitle: ui.ai.cfgTitle(ui.ai.currentName) });
       
   888       reportDom.appendChild(tableDom);
       
   889       reportsDom.insertBefore(reportDom, reportsDom.firstChild);
       
   890     }
       
   891     ui.report.stat = function() {
       
   892       /* console.profile(); */
   809       var histo = {};
   893       var histo = {};
   810       var cnt = parseInt(document.getElementById('stat-count-limit').value);
   894       var cnt = parseInt(document.getElementById('stat-count-limit').value);
   811       if (isNaN(cnt) || !isFinite(cnt) || cnt < 1)
   895       if (isNaN(cnt) || !isFinite(cnt) || cnt < 1)
   812         cnt = 100;
   896         cnt = 100;
   813       var tsLimit = parseFloat(document.getElementById('stat-count-limit').value);
   897       var tsLimit = parseFloat(document.getElementById('stat-count-limit').value);
   836           }
   920           }
   837           board.putRandom(brd);
   921           board.putRandom(brd);
   838           turn++;
   922           turn++;
   839         }
   923         }
   840         var tsTo = new Date().getTime();
   924         var tsTo = new Date().getTime();
   841         var stat = board.score(brd);
   925         ui.report.update(histo, brd, tsTo-tsFrom, turn);
   842         var speed = turn*1000/(tsTo - tsFrom);
       
   843         var score = stat.score;
       
   844         var max = stat.max;
       
   845         if ( ! histo[max]) {
       
   846           histo[max] = { n: 1, minSpeed: speed, meanSpeed: speed, maxSpeed: speed, minTurn: turn, meanTurn: turn, maxTurn: turn, minScore: score, meanScore: score, maxScore: score };
       
   847         } else {
       
   848           var row = histo[max];
       
   849           row.n++;
       
   850           if (scoreChecked) {
       
   851             row.minScore = Math.min(row.minScore, score);
       
   852             row.meanScore += score;
       
   853             row.maxScore = Math.max(row.maxScore, score);
       
   854           }
       
   855         if (speedChecked) {
       
   856             row.minSpeed = Math.min(row.minSpeed, speed);
       
   857             row.meanSpeed += speed;
       
   858             row.maxSpeed = Math.max(row.maxSpeed, speed);
       
   859           }
       
   860           if (turnChecked) {
       
   861             row.minTurn = Math.min(row.minTurn, turn);
       
   862             row.meanTurn += turn;
       
   863             row.maxTurn = Math.max(row.maxTurn, turn);
       
   864           }
       
   865         }
       
   866         if (tsTo - tsLimitFrom >= tsLimit)
   926         if (tsTo - tsLimitFrom >= tsLimit)
   867           break;
   927           break;
   868       }
   928       }
   869       for (var i in histo) {
   929       ui.report.fix(histo);
   870         var row = histo[i];
   930       ui.report.show(histo, gameCnt);
   871         var n = row.n;
       
   872         if (scoreChecked)
       
   873           row.meanScore = row.meanScore / n;
       
   874         if (turnChecked)
       
   875           row.meanTurn = row.meanTurn / n;
       
   876         if (speedChecked)
       
   877           row.meanSpeed = row.meanSpeed / n;
       
   878       }
       
   879       var reportDom = document.createElement('div');
       
   880       reportDom.classList.add('option');
       
   881       var h5Dom = document.createElement('h5');
       
   882       h5Dom.appendChild(document.createTextNode(ui.ai.currentName + " #" + ui.report.statNo++));
       
   883       reportDom.appendChild(h5Dom);
       
   884       var maxVals = Object.keys(histo).sort(function(a,b){return a - b});
       
   885       var tbl = [];
       
   886       for (var i = maxVals.length-1; i >= 0; i--) {
       
   887         var maxVal = maxVals[i];
       
   888         var row = histo[maxVals[i]];
       
   889         var perc = parseFloat((100 * row.n / gameCnt).toPrecision(3));
       
   890         var tblRow = [maxVal, row.n, perc];
       
   891         if (scoreChecked) {
       
   892           tblRow.push(row.minScore);
       
   893           var meanScore = parseFloat(row.meanScore.toPrecision(3));
       
   894           tblRow.push(meanScore);
       
   895           tblRow.push(row.maxScore);
       
   896         }
       
   897         if (turnChecked) {
       
   898           tblRow.push(row.minTurn);
       
   899           tblRow.push(Math.floor(row.meanTurn));
       
   900           tblRow.push(row.maxTurn);
       
   901         }
       
   902         if (speedChecked) {
       
   903           tblRow.push(parseFloat(row.minSpeed.toPrecision(3)));
       
   904           tblRow.push(parseFloat(row.meanSpeed.toPrecision(3)));
       
   905           tblRow.push(parseFloat(row.maxSpeed.toPrecision(3)));
       
   906         }
       
   907         tbl.push(tblRow);
       
   908       }
       
   909       var tblCols = ['maxVal', 'n', '%'];
       
   910       if (scoreChecked) {
       
   911         tblCols.push('min score');
       
   912         tblCols.push('mean score');
       
   913         tblCols.push('max score');
       
   914       }
       
   915       if (turnChecked) {
       
   916         tblCols.push('min turn');
       
   917         tblCols.push('mean turn');
       
   918         tblCols.push('max turn');
       
   919       }
       
   920       if (speedChecked) {
       
   921         tblCols.push('min speed');
       
   922         tblCols.push('mean speed');
       
   923         tblCols.push('max speed');
       
   924       }
       
   925       var tableDom = ui.dom.table(tbl, tblCols, { tblClass: 'report-by-maxval', tblTitle: ui.ai.cfgTitle(ui.ai.currentName) });
       
   926       reportDom.appendChild(tableDom);
       
   927       reportsDom.insertBefore(reportDom, reportsDom.firstChild);
       
   928       /* console.profileEnd(); */
   931       /* console.profileEnd(); */
   929     }
   932     }
   930     var statisticBtn = document.getElementById('statistic');
   933     var statisticBtn = document.getElementById('statistic');
   931     statisticBtn.addEventListener("click", ui.report.stat, false);
   934     statisticBtn.addEventListener("click", ui.report.stat, false);
   932 
   935