Fold code into loops.
authorOleksandr Gavenko <gavenkoa@gmail.com>
Mon, 08 Sep 2014 02:04:16 +0300
changeset 19 94a4201d27a3
parent 18 42d62e5123b2
child 20 ab294e8db00c
Fold code into loops.
ai.js
--- a/ai.js	Mon Sep 08 01:47:36 2014 +0300
+++ b/ai.js	Mon Sep 08 02:04:16 2014 +0300
@@ -1,6 +1,7 @@
 "use strict";
 
 var ai = {};
+ai.dirs = ["up", "down", "left", "right"];
 
 // Each strategy is a function that except current board position as 2d array and context from
 // previous call to share state/precomputed values between calls.
@@ -38,33 +39,18 @@
     var origBrd = new this.brdEngine(brd);
     var nextBrd = new this.brdEngine();
     var maxScore = -1;
-    var action;
-    if (origBrd.up(nextBrd)) {
-        maxScore = nextBrd.score();
-        action = "up";
-    }
-    if (origBrd.left(nextBrd)) {
-        var score = nextBrd.score();
-        if (maxScore < score) {
-            action = "left";
-            maxScore = score;
+    var bestDir;
+    for (var i = 0; i < ai.dirs.length; i++) {
+        var dir = ai.dirs[i];
+        if (origBrd[dir](nextBrd)) {
+            var score = nextBrd.score();
+            if (maxScore < score) {
+                bestDir = dir;
+                maxScore = score;
+            }
         }
     }
-    if (origBrd.down(nextBrd)) {
-        var score = nextBrd.score();
-        if (maxScore < score) {
-            action = "down";
-            maxScore = score;
-        }
-    }
-    if (origBrd.right(nextBrd)) {
-        var score = nextBrd.score();
-        if (maxScore < score) {
-            action = "right";
-            maxScore = score;
-        }
-    }
-    return action;
+    return bestDir;
 }
 /* Mark that next board will be unrelated to previous, so any stored precompution can be cleared. */
 ai.nextMaxScore.prototype.cleanup = function() { }
@@ -82,33 +68,18 @@
     var origBrd = new this.brdEngine(brd);
     var nextBrd = new this.brdEngine();
     var maxMax = -1;
-    var action;
-    if (origBrd.up(nextBrd)) {
-        maxMax = nextBrd.max();
-        action = "up";
-    }
-    if (origBrd.left(nextBrd)) {
-        var max = nextBrd.score();
-        if (maxMax < max) {
-            action = "left";
-            maxMax = max;
+    var bestDir;
+    for (var i = 0; i < ai.dirs.length; i++) {
+        var dir = ai.dirs[i];
+        if (origBrd[dir](nextBrd)) {
+            var max = nextBrd.score();
+            if (maxMax < max) {
+                maxMax = max;
+                bestDir = dir;
+            }
         }
     }
-    if (origBrd.down(nextBrd)) {
-        var max = nextBrd.max();
-        if (maxMax < max) {
-            action = "down";
-            maxMax = max;
-        }
-    }
-    if (origBrd.right(nextBrd)) {
-        var max = nextBrd.max();
-        if (maxMax < max) {
-            action = "right";
-            maxMax = max;
-        }
-    }
-    return action;
+    return bestDir;
 }
 /* Mark that next board will be unrelated to previous, so any stored precompution can be cleared. */
 ai.nextMaxScore.prototype.cleanup = function() { }
@@ -121,15 +92,14 @@
 ai.deepMaxScore = function(brdEngine) {
     this.brdEngine = brdEngine;
 }
-ai.deepMaxScore.dirs = ["up", "down", "left", "right"];
 ai.deepMaxScore.prototype.analyse = function(brd) {
     var origBrd = new this.brdEngine(brd);
     var nextBrd = new this.brdEngine();
     var prevScore = -1, nextScore = -1;
     var maxScore = -1;
     var bestDir;
-    for (var i = 0; i < ai.deepMaxScore.dirs.length; i++) {
-        var dir = ai.deepMaxScore.dirs[i];
+    for (var i = 0; i < ai.dirs.length; i++) {
+        var dir = ai.dirs[i];
         if (origBrd[dir](nextBrd)) {
             nextScore = nextBrd.score();
             var score = this.bestScore(nextBrd);
@@ -155,8 +125,8 @@
     var currScore = brd.score();
     var maxScore = currScore;
     var nextBrd = new this.brdEngine();
-    for (var i = 0; i < ai.deepMaxScore.dirs.length; i++) {
-        if (brd[ai.deepMaxScore.dirs[i]](nextBrd)) {
+    for (var i = 0; i < ai.dirs.length; i++) {
+        if (brd[ai.dirs[i]](nextBrd)) {
             var score = nextBrd.score();
             if (score > currScore)
                 maxScore = Math.max(maxScore, this.bestScore(nextBrd, seenBrds));
@@ -164,4 +134,6 @@
     }
     return maxScore;
 }
+/* Mark that next board will be unrelated to previous, so any stored precompution can be cleared. */
+ai.deepMaxScore.prototype.cleanup = function() { }