ai.js
changeset 152 07814b979a8a
parent 149 2839f8227a38
child 155 1f8df90bd338
equal deleted inserted replaced
151:bb5994be7eda 152:07814b979a8a
     4 
     4 
     5 /** @module */
     5 /** @module */
     6 var ai = {};
     6 var ai = {};
     7 /** Directions. @constant */
     7 /** Directions. @constant */
     8 ai.dirs = ["up", "right", "down", "left"];
     8 ai.dirs = ["up", "right", "down", "left"];
     9 /** Possible direction function names. @constant */
     9 /** Possible direction check function names ordered by ai.dirs. @constant */
    10 ai.canDirs = ["canUp", "canRight", "canDown", "canLeft"];
    10 ai.canFn = ["canUp", "canRight", "canDown", "canLeft"];
       
    11 /** Possible merge function names ordered by ai.dirs. @constant */
       
    12 ai.mergeFn = ["upMerges", "rightMerges", "downMerges", "leftMerges"];
    11 
    13 
    12 /** Create empty 'to' if argument missing. */
    14 /** Create empty 'to' if argument missing. */
    13 ai.copyObj = function(from, to) {
    15 ai.copyObj = function(from, to) {
    14     if (to == null || typeof to !== "object")
    16     if (to == null || typeof to !== "object")
    15         to = {};
    17         to = {};
    62 /** Select best direction for next step. */
    64 /** Select best direction for next step. */
    63 ai.BlindRandom.prototype.analyse = function(brd2d) {
    65 ai.BlindRandom.prototype.analyse = function(brd2d) {
    64     var origBrd = new this.brdEngine(brd2d);
    66     var origBrd = new this.brdEngine(brd2d);
    65     while (true) {
    67     while (true) {
    66         var rnd = Math.floor(Math.random()*4);
    68         var rnd = Math.floor(Math.random()*4);
    67         if (origBrd[ai.canDirs[rnd]]())
    69         if (origBrd[ai.canFn[rnd]]())
    68             return ai.dirs[rnd];
    70             return ai.dirs[rnd];
    69     }
    71     }
    70 }
    72 }
    71 /* Mark that next board will be unrelated to previous, so any stored precompution can be cleared. */
    73 /* Mark that next board will be unrelated to previous, so any stored precompution can be cleared. */
    72 ai.BlindRandom.prototype.cleanup = function() { }
    74 ai.BlindRandom.prototype.cleanup = function() { }
   111             var dir = 1;
   113             var dir = 1;
   112         else if (rnd < this.threshold3)
   114         else if (rnd < this.threshold3)
   113             var dir = 2;
   115             var dir = 2;
   114         else
   116         else
   115             var dir = 3;
   117             var dir = 3;
   116         if (origBrd[ai.canDirs[dir]]())
   118         if (origBrd[ai.canFn[dir]]())
   117             return ai.dirs[dir];
   119             return ai.dirs[dir];
   118     }
   120     }
   119 }
   121 }
   120 /* Mark that next board will be unrelated to previous, so any stored precompution can be cleared. */
   122 /* Mark that next board will be unrelated to previous, so any stored precompution can be cleared. */
   121 ai.BlindWeightRandom.prototype.cleanup = function() { }
   123 ai.BlindWeightRandom.prototype.cleanup = function() { }
   142     this.cfg = cfg || {};
   144     this.cfg = cfg || {};
   143     this.cfg.whilePossible = this.cfg.whilePossible || false;
   145     this.cfg.whilePossible = this.cfg.whilePossible || false;
   144     this.cfg.clockwise = this.cfg.clockwise || false;
   146     this.cfg.clockwise = this.cfg.clockwise || false;
   145 }
   147 }
   146 ai.BlindCycle.dirs = ["left", "down", "right", "up"];
   148 ai.BlindCycle.dirs = ["left", "down", "right", "up"];
   147 ai.BlindCycle.canDirs = ["canLeft", "canDown", "canRight", "canUp"];
   149 ai.BlindCycle.canFn = ["canLeft", "canDown", "canRight", "canUp"];
   148 ai.BlindCycle.prototype.nextDir = function(dir) {
   150 ai.BlindCycle.prototype.nextDir = function(dir) {
   149     if (this.cfg.clockwise)
   151     if (this.cfg.clockwise)
   150         return (dir + (4-1)) % 4;
   152         return (dir + (4-1)) % 4;
   151     else
   153     else
   152         return (dir + 1) % 4;
   154         return (dir + 1) % 4;
   156     var origBrd = new this.brdEngine(brd2d);
   158     var origBrd = new this.brdEngine(brd2d);
   157     this.prevDir = this.prevDir || 0;
   159     this.prevDir = this.prevDir || 0;
   158     if (!this.cfg.whilePossible)
   160     if (!this.cfg.whilePossible)
   159         this.prevDir = this.nextDir(this.prevDir);
   161         this.prevDir = this.nextDir(this.prevDir);
   160     while (true) {
   162     while (true) {
   161         if (origBrd[ai.BlindCycle.canDirs[this.prevDir]]())
   163         if (origBrd[ai.BlindCycle.canFn[this.prevDir]]())
   162             return ai.BlindCycle.dirs[this.prevDir];
   164             return ai.BlindCycle.dirs[this.prevDir];
   163         this.prevDir = this.nextDir(this.prevDir);
   165         this.prevDir = this.nextDir(this.prevDir);
   164     }
   166     }
   165 }
   167 }
   166 /* Mark that next board will be unrelated to previous, so any stored precompution can be cleared. */
   168 /* Mark that next board will be unrelated to previous, so any stored precompution can be cleared. */