Choose move that lead to maximum next score among all possible when next moves
authorOleksandr Gavenko <gavenkoa@gmail.com>
Mon, 08 Sep 2014 01:47:36 +0300
changeset 18 42d62e5123b2
parent 17 06ad7999b9f6
child 19 94a4201d27a3
Choose move that lead to maximum next score among all possible when next moves lead to same score.
ai.js
--- a/ai.js	Mon Sep 08 01:03:05 2014 +0300
+++ b/ai.js	Mon Sep 08 01:47:36 2014 +0300
@@ -115,43 +115,33 @@
 
 
 ////////////////////////////////////////////////////////////////
-// N level deep on score value without random.
+// N level deep on score value without random simulation.
 ////////////////////////////////////////////////////////////////
 
 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 action;
-    if (origBrd.up(nextBrd)) {
-        maxScore = this.bestScore(nextBrd);
-        action = "up";
-    }
-    if (origBrd.left(nextBrd)) {
-        var score = this.bestScore(nextBrd);
-        if (maxScore < score) {
-            action = "left";
-            maxScore = score;
+    var bestDir;
+    for (var i = 0; i < ai.deepMaxScore.dirs.length; i++) {
+        var dir = ai.deepMaxScore.dirs[i];
+        if (origBrd[dir](nextBrd)) {
+            nextScore = nextBrd.score();
+            var score = this.bestScore(nextBrd);
+            // console.log("dir: %o, prevScore: %o, nextScore: %o, maxScore: %o, score: %o", dir, prevScore, nextScore, maxScore, score);
+            if (maxScore < score || (maxScore === score && prevScore < nextScore)) {
+                prevScore = nextScore;
+                maxScore = score;
+                bestDir = dir;
+            }
         }
     }
-    if (origBrd.down(nextBrd)) {
-        var score = this.bestScore(nextBrd);
-        if (maxScore < score) {
-            action = "down";
-            maxScore = score;
-        }
-    }
-    if (origBrd.right(nextBrd)) {
-        var score = this.bestScore(nextBrd);
-        if (maxScore < score) {
-            action = "right";
-            maxScore = score;
-        }
-    }
-    return action;
+    return bestDir;
 }
 ai.deepMaxScore.prototype.bestScore = function(brd, seenBrds) {
     if (seenBrds) {
@@ -162,16 +152,16 @@
         seenBrds = [];
     }
     seenBrds.push(brd);
-    var maxScore = brd.score();
+    var currScore = brd.score();
+    var maxScore = currScore;
     var nextBrd = new this.brdEngine();
-    if (brd.up(nextBrd))
-        maxScore = Math.max(maxScore, this.bestScore(nextBrd, seenBrds) - 1);
-    if (brd.down(nextBrd))
-        maxScore = Math.max(maxScore, this.bestScore(nextBrd, seenBrds) - 1);
-    if (brd.left(nextBrd))
-        maxScore = Math.max(maxScore, this.bestScore(nextBrd, seenBrds) - 1);
-    if (brd.right(nextBrd))
-        maxScore = Math.max(maxScore, this.bestScore(nextBrd, seenBrds) - 1);
+    for (var i = 0; i < ai.deepMaxScore.dirs.length; i++) {
+        if (brd[ai.deepMaxScore.dirs[i]](nextBrd)) {
+            var score = nextBrd.score();
+            if (score > currScore)
+                maxScore = Math.max(maxScore, this.bestScore(nextBrd, seenBrds));
+        }
+    }
     return maxScore;
 }