Move statistic gathering and displaying logic to separate functions.
authorOleksandr Gavenko <gavenkoa@gmail.com>
Wed, 24 Sep 2014 18:45:46 +0300
changeset 128 673743a1290d
parent 127 4cc0421184ac
child 129 e0958e521716
Move statistic gathering and displaying logic to separate functions.
2048.html
--- a/2048.html	Wed Sep 24 17:59:14 2014 +0300
+++ b/2048.html	Wed Sep 24 18:45:46 2014 +0300
@@ -801,81 +801,43 @@
     ui.report = {};
     ui.report.statNo = 1;
 
-    ui.report.stat = function() {
-      /* console.profile(); */
+    ui.report.update = function(histo, brd, tsDelta, turn) {
+      var stat = board.score(brd);
+      var speed = turn * 1000 / tsDelta;
+      var score = stat.score;
+      var max = stat.max;
+      if ( ! histo[max]) {
+        var row = histo[max] = { n: 1 };
+        row.minScore = score; row.meanScore = score; row.maxScore = score;
+        row.minSpeed = speed; row.meanSpeed = speed; row.maxSpeed = speed;
+        row.minTurn = turn; row.meanTurn = turn; row.maxTurn = turn;
+      } else {
+        var row = histo[max];
+        row.n++;
+        row.minScore = Math.min(row.minScore, score);
+        row.meanScore += score;
+        row.maxScore = Math.max(row.maxScore, score);
+        row.minSpeed = Math.min(row.minSpeed, speed);
+        row.meanSpeed += speed;
+        row.maxSpeed = Math.max(row.maxSpeed, speed);
+        row.minTurn = Math.min(row.minTurn, turn);
+        row.meanTurn += turn;
+        row.maxTurn = Math.max(row.maxTurn, turn);
+      }
+    }
+    ui.report.fix = function(histo) {
+      for (var i in histo) {
+        var row = histo[i];
+        var n = row.n;
+        row.meanScore = row.meanScore / n;
+        row.meanTurn = row.meanTurn / n;
+        row.meanSpeed = row.meanSpeed / n;
+      }
+    }
+    ui.report.show = function(histo, gameCnt) {
       var scoreChecked = document.getElementById('report-score').checked;
       var turnChecked = document.getElementById('report-turn').checked;
       var speedChecked = document.getElementById('report-speed').checked;
-      var histo = {};
-      var cnt = parseInt(document.getElementById('stat-count-limit').value);
-      if (isNaN(cnt) || !isFinite(cnt) || cnt < 1)
-        cnt = 100;
-      var tsLimit = parseFloat(document.getElementById('stat-count-limit').value);
-      if (isNaN(tsLimit) || !isFinite(tsLimit) || tsLimit < 1 || tsLimit > 60)
-        tsLimit = 1000 * 10;
-      else
-        tsLimit = 1000 * tsLimit;
-      var safeBdr = board.create();
-      var tsLimitFrom = new Date().getTime();
-      for (var gameCnt = 0; gameCnt++ < cnt; void(0)) {
-        var turn = 0;
-        var brd = board.create();
-        board.putRandom(brd);
-        var tsFrom = new Date().getTime();
-        while (!board.gameOver(brd)) {
-          board.copy(brd, safeBdr);
-          var move = ui.ai.current.analyse(safeBdr);
-          if (ui.game.dirs.indexOf(move) < 0) {
-            ui.game.setMessage("I don't know how to move!");
-            return;
-          }
-          var updated = board.move[move].call(null, brd);
-          if (!updated) {
-            ui.game.setMessage("Wrong move!");
-            return;
-          }
-          board.putRandom(brd);
-          turn++;
-        }
-        var tsTo = new Date().getTime();
-        var stat = board.score(brd);
-        var speed = turn*1000/(tsTo - tsFrom);
-        var score = stat.score;
-        var max = stat.max;
-        if ( ! histo[max]) {
-          histo[max] = { n: 1, minSpeed: speed, meanSpeed: speed, maxSpeed: speed, minTurn: turn, meanTurn: turn, maxTurn: turn, minScore: score, meanScore: score, maxScore: score };
-        } else {
-          var row = histo[max];
-          row.n++;
-          if (scoreChecked) {
-            row.minScore = Math.min(row.minScore, score);
-            row.meanScore += score;
-            row.maxScore = Math.max(row.maxScore, score);
-          }
-        if (speedChecked) {
-            row.minSpeed = Math.min(row.minSpeed, speed);
-            row.meanSpeed += speed;
-            row.maxSpeed = Math.max(row.maxSpeed, speed);
-          }
-          if (turnChecked) {
-            row.minTurn = Math.min(row.minTurn, turn);
-            row.meanTurn += turn;
-            row.maxTurn = Math.max(row.maxTurn, turn);
-          }
-        }
-        if (tsTo - tsLimitFrom >= tsLimit)
-          break;
-      }
-      for (var i in histo) {
-        var row = histo[i];
-        var n = row.n;
-        if (scoreChecked)
-          row.meanScore = row.meanScore / n;
-        if (turnChecked)
-          row.meanTurn = row.meanTurn / n;
-        if (speedChecked)
-          row.meanSpeed = row.meanSpeed / n;
-      }
       var reportDom = document.createElement('div');
       reportDom.classList.add('option');
       var h5Dom = document.createElement('h5');
@@ -925,6 +887,47 @@
       var tableDom = ui.dom.table(tbl, tblCols, { tblClass: 'report-by-maxval', tblTitle: ui.ai.cfgTitle(ui.ai.currentName) });
       reportDom.appendChild(tableDom);
       reportsDom.insertBefore(reportDom, reportsDom.firstChild);
+    }
+    ui.report.stat = function() {
+      /* console.profile(); */
+      var histo = {};
+      var cnt = parseInt(document.getElementById('stat-count-limit').value);
+      if (isNaN(cnt) || !isFinite(cnt) || cnt < 1)
+        cnt = 100;
+      var tsLimit = parseFloat(document.getElementById('stat-count-limit').value);
+      if (isNaN(tsLimit) || !isFinite(tsLimit) || tsLimit < 1 || tsLimit > 60)
+        tsLimit = 1000 * 10;
+      else
+        tsLimit = 1000 * tsLimit;
+      var safeBdr = board.create();
+      var tsLimitFrom = new Date().getTime();
+      for (var gameCnt = 0; gameCnt++ < cnt; void(0)) {
+        var turn = 0;
+        var brd = board.create();
+        board.putRandom(brd);
+        var tsFrom = new Date().getTime();
+        while (!board.gameOver(brd)) {
+          board.copy(brd, safeBdr);
+          var move = ui.ai.current.analyse(safeBdr);
+          if (ui.game.dirs.indexOf(move) < 0) {
+            ui.game.setMessage("I don't know how to move!");
+            return;
+          }
+          var updated = board.move[move].call(null, brd);
+          if (!updated) {
+            ui.game.setMessage("Wrong move!");
+            return;
+          }
+          board.putRandom(brd);
+          turn++;
+        }
+        var tsTo = new Date().getTime();
+        ui.report.update(histo, brd, tsTo-tsFrom, turn);
+        if (tsTo - tsLimitFrom >= tsLimit)
+          break;
+      }
+      ui.report.fix(histo);
+      ui.report.show(histo, gameCnt);
       /* console.profileEnd(); */
     }
     var statisticBtn = document.getElementById('statistic');