ai.js
changeset 12 a9a44cfc3e08
parent 10 70ece7f758a0
child 14 9b49e710f5a7
--- a/ai.js	Sun Sep 07 00:33:47 2014 +0300
+++ b/ai.js	Sun Sep 07 03:33:33 2014 +0300
@@ -5,6 +5,7 @@
 // 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.
 
+
 
 ////////////////////////////////////////////////////////////////
 // Random AI.
@@ -14,25 +15,31 @@
     this.brdEngine = brdEngine;
 }
 ai.random.prototype.analyse = function(brd) {
-    var tmpBrd = new this.brdEngine(brd);
+    var origBrd = new this.brdEngine(brd);
     while (true) {
         var rnd = Math.floor(Math.random()*4);
-        if (tmpBrd[["canUp", "canDown", "canLeft", "canRight"][rnd]]())
+        if (origBrd[["canUp", "canDown", "canLeft", "canRight"][rnd]]())
             return ["up", "down", "left", "right"][rnd];
     }
 }
+/* Mark that next board will be unrelated to previous, so any stored precompution can be cleared. */
+ai.random.prototype.cleanup = function() { }
 
+
 
 ////////////////////////////////////////////////////////////////
 // 1 level deep on max scores.
 ////////////////////////////////////////////////////////////////
 
-ai.nextMaxScore = function(brd) {
-    var tmpBrd = board.create();
-    board.copy(brd, tmpBrd);
+ai.nextMaxScore = function(brdEngine) {
+    this.brdEngine = brdEngine;
+}
+ai.nextMaxScore.prototype.analyse = function(brd) {
+    var origBrd = new this.brdEngine(brd);
+    var nextBrd = new this.brdEngine();
     var maxScore = -1;
     var action;
-    if (board.move.up(tmpBrd)) {
+    if (origBrd.up(nextBrd)) {
         maxScore = board.score(tmpBrd).score;
         action = "up";
     }
@@ -62,13 +69,19 @@
     }
     return action;
 }
+/* Mark that next board will be unrelated to previous, so any stored precompution can be cleared. */
+ai.nextMaxScore.prototype.cleanup = function() { }
 
+
 
 ////////////////////////////////////////////////////////////////
 // 1 level deep on max value.
 ////////////////////////////////////////////////////////////////
 
-ai.nextMaxValue = function(brd) {
+ai.nextMaxValue = function(brdEngine) {
+    this.brdEngine = brdEngine;
+}
+ai.nextMaxValue.prototype.analyse = function(brd) {
     var tmpBrd = board.create();
     board.copy(brd, tmpBrd);
     var maxMax = -1;
@@ -103,4 +116,6 @@
     }
     return action;
 }
+/* Mark that next board will be unrelated to previous, so any stored precompution can be cleared. */
+ai.nextMaxScore.prototype.cleanup = function() { }