Gather game statistic.
authorOleksandr Gavenko <gavenkoa@gmail.com>
Mon, 15 Sep 2014 21:19:37 +0300
changeset 64 96e997158562
parent 63 251c39718d67
child 65 b8028f3e30fb
Gather game statistic.
2048.html
--- a/2048.html	Mon Sep 15 20:54:44 2014 +0300
+++ b/2048.html	Mon Sep 15 21:19:37 2014 +0300
@@ -228,8 +228,8 @@
   <div id="report-area" class="area">
     <h1>Reports</h1>
     <div class="settings">
-      <div class="setting"><input type="text" id="count-limit" value="10"> times</div>
-      <div class="setting">limit to <input type="text" id="time-limit" value="10"> sec</div>
+      <div class="setting"><input type="text" id="stat-count-limit" value="10"> times</div>
+      <div class="setting">limit to <input type="text" id="stat-time-limit" value="10"> sec</div>
       <button id="statistic">Start</button>
       <div class="clearfix"></div>
     </div>
@@ -629,7 +629,43 @@
     // Reports and statistic.
 
     function statistic() {
-      
+      console.log('xxx');
+      var stat = [];
+      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 tmpBrd = board.create();
+      var tsFrom = new Date().getTime();
+      for (var i = 0; i < cnt; i++) {
+        var turn = 0;
+        var brd = board.create();
+        board.putRandom(brd);
+        while (!board.gameOver(brd)) {
+          board.copy(brd, tmpBrd);
+          var move = ui.ai.current.analyse(tmpBrd);
+          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();
+        stat.push(board.score(brd));
+        if (tsTo - tsFrom >= tsLimit)
+          break;
+      }
+      console.log(stat);
     }
 
     var statisticBtn = document.getElementById('statistic');