Speedup score calculation.
authorOleksandr Gavenko <gavenkoa@gmail.com>
Tue, 23 Sep 2014 02:06:32 +0300
changeset 121 b7b5e4a03e4b
parent 120 653ee00d2601
child 122 4cc40a51742a
Speedup score calculation.
board.js
--- a/board.js	Tue Sep 23 01:33:10 2014 +0300
+++ b/board.js	Tue Sep 23 02:06:32 2014 +0300
@@ -838,41 +838,30 @@
     if (brd.da === 0) cnt++; if (brd.db === 0) cnt++; if (brd.dc === 0) cnt++; if (brd.dd === 0) cnt++;
     return cnt;
 }
+BoardObj.scoreLookup = [0]; // [ 0, 4, 16, 48, 128, 320, 768, 1792, 4096, 9216, 20480, 45056, 98304];
+(function() {
+    for (var i = 1; i < 16; i++)
+        BoardObj.scoreLookup[i] = (i-1)*(1 << i);
+})();
 BoardObj.prototype.score = function() {
-    var score = 0;
     var brd = this.brd;
-    var v = brd.aa;
-    if (v > 1) score += (v-1)*(1 << v);
-    var v = brd.ab;
-    if (v > 1) score += (v-1)*(1 << v);
-    var v = brd.ac;
-    if (v > 1) score += (v-1)*(1 << v);
-    var v = brd.ad;
-    if (v > 1) score += (v-1)*(1 << v);
-    var v = brd.ba;
-    if (v > 1) score += (v-1)*(1 << v);
-    var v = brd.bb;
-    if (v > 1) score += (v-1)*(1 << v);
-    var v = brd.bc;
-    if (v > 1) score += (v-1)*(1 << v);
-    var v = brd.bd;
-    if (v > 1) score += (v-1)*(1 << v);
-    var v = brd.ca;
-    if (v > 1) score += (v-1)*(1 << v);
-    var v = brd.cb;
-    if (v > 1) score += (v-1)*(1 << v);
-    var v = brd.cc;
-    if (v > 1) score += (v-1)*(1 << v);
-    var v = brd.cd;
-    if (v > 1) score += (v-1)*(1 << v);
-    var v = brd.da;
-    if (v > 1) score += (v-1)*(1 << v);
-    var v = brd.db;
-    if (v > 1) score += (v-1)*(1 << v);
-    var v = brd.dc;
-    if (v > 1) score += (v-1)*(1 << v);
-    var v = brd.dd;
-    if (v > 1) score += (v-1)*(1 << v);
+    var lookup = BoardObj.scoreLookup;
+    var score = lookup[brd.aa];
+    score += lookup[brd.ab];
+    score += lookup[brd.ac];
+    score += lookup[brd.ad];
+    score += lookup[brd.ba];
+    score += lookup[brd.bb];
+    score += lookup[brd.bc];
+    score += lookup[brd.bd];
+    score += lookup[brd.ca];
+    score += lookup[brd.cb];
+    score += lookup[brd.cc];
+    score += lookup[brd.cd];
+    score += lookup[brd.da];
+    score += lookup[brd.db];
+    score += lookup[brd.dc];
+    score += lookup[brd.dd];
     return score;
 }
 BoardObj.prototype.max = function() {