Don't calculate max value if it isn't used.
authorOleksandr Gavenko <gavenkoa@gmail.com>
Tue, 23 Sep 2014 02:24:13 +0300
changeset 122 4cc40a51742a
parent 121 b7b5e4a03e4b
child 123 fc6019d22a38
Don't calculate max value if it isn't used.
ai.js
--- a/ai.js	Tue Sep 23 02:06:32 2014 +0300
+++ b/ai.js	Tue Sep 23 02:24:13 2014 +0300
@@ -324,19 +324,22 @@
 ai.expectimax.bestCfg = {balance: .9, depth: 3, scoreCoef: 1, maxValCoef: 0, cornerBonus: 0, edgeBonus: 0, freeBonus: 0, weightThreshold: 10};
 ai.expectimax.prototype.weight = function(brd) {
     var score = 0;
-    if (this.cfg.scoreCoef > 0)
-        score += this.cfg.scoreCoef * brd.score();
-    var max = brd.max();
-    if (this.cfg.maxValCoef > 0)
-        score += this.cfg.maxValCoef * max;
-    if (this.cfg.cornerBonus > 0)
-        if (brd.atCorner(max))
-            score += this.cfg.cornerBonus;
-    if (this.cfg.edgeBonus > 0)
-        if (brd.atEdge(max))
-            score += this.cfg.edgeBonus;
-    if (this.cfg.freeBonus > 0)
-        score += this.cfg.freeBonus * brd.free();
+    var cfg = this.cfg;
+    if (cfg.scoreCoef > 0)
+        score += cfg.scoreCoef * brd.score();
+    if (cfg.maxValCoef > 0 || cfg.cornerBonus > 0 || cfg.edgeBonus > 0) {
+        var max = brd.max();
+        if (cfg.maxValCoef > 0)
+            score += cfg.maxValCoef * max;
+        if (cfg.cornerBonus > 0)
+            if (brd.atCorner(max))
+                score += cfg.cornerBonus;
+        if (cfg.edgeBonus > 0)
+            if (brd.atEdge(max))
+                score += cfg.edgeBonus;
+        if (cfg.freeBonus > 0)
+            score += cfg.freeBonus * brd.free();
+    }
     return score;
 }
 ai.expectimax.prototype.analyse = function(brd) {