ai.js
changeset 147 d4f9433e30b9
parent 140 438fd8c3c3ca
child 148 93c122e0ea90
equal deleted inserted replaced
146:a7b42ea88ac5 147:d4f9433e30b9
    57  * @param {Board} brdEngine  board engine from board.js
    57  * @param {Board} brdEngine  board engine from board.js
    58  * @constructor */
    58  * @constructor */
    59 ai.BlindRandom = function(brdEngine) {
    59 ai.BlindRandom = function(brdEngine) {
    60     this.brdEngine = brdEngine;
    60     this.brdEngine = brdEngine;
    61 }
    61 }
    62 ai.BlindRandom.prototype.analyse = function(brd) {
    62 /** Select best direction for next step. */
    63     var origBrd = new this.brdEngine(brd);
    63 ai.BlindRandom.prototype.analyse = function(brd2d) {
       
    64     var origBrd = new this.brdEngine(brd2d);
    64     while (true) {
    65     while (true) {
    65         var rnd = Math.floor(Math.random()*4);
    66         var rnd = Math.floor(Math.random()*4);
    66         if (origBrd[ai.canDirs[rnd]]())
    67         if (origBrd[ai.canDirs[rnd]]())
    67             return ai.dirs[rnd];
    68             return ai.dirs[rnd];
    68     }
    69     }
    97     this.threshold1 = this.cfg.left/total;
    98     this.threshold1 = this.cfg.left/total;
    98     this.threshold2 = (this.cfg.left + this.cfg.down)/total;
    99     this.threshold2 = (this.cfg.left + this.cfg.down)/total;
    99     this.threshold3 = (this.cfg.left + this.cfg.down + this.cfg.right)/total;
   100     this.threshold3 = (this.cfg.left + this.cfg.down + this.cfg.right)/total;
   100 }
   101 }
   101 ai.BlindWeightRandom.bestCfg = { left: 1, right: 16, up: 4, down: 8 };
   102 ai.BlindWeightRandom.bestCfg = { left: 1, right: 16, up: 4, down: 8 };
   102 ai.BlindWeightRandom.prototype.analyse = function(brd) {
   103 /** Select best direction for next step. */
   103     var origBrd = new this.brdEngine(brd);
   104 ai.BlindWeightRandom.prototype.analyse = function(brd2d) {
       
   105     var origBrd = new this.brdEngine(brd2d);
   104     while (true) {
   106     while (true) {
   105         var rnd = Math.random();
   107         var rnd = Math.random();
   106         if (rnd < this.threshold1)
   108         if (rnd < this.threshold1)
   107             var dir = 0;
   109             var dir = 0;
   108         else if (rnd < this.threshold2)
   110         else if (rnd < this.threshold2)
   147     if (this.cfg.clockwise)
   149     if (this.cfg.clockwise)
   148         return (dir + (4-1)) % 4;
   150         return (dir + (4-1)) % 4;
   149     else
   151     else
   150         return (dir + 1) % 4;
   152         return (dir + 1) % 4;
   151 }
   153 }
   152 ai.BlindCycle.prototype.analyse = function(brd) {
   154 /** Select best direction for next step. */
   153     var origBrd = new this.brdEngine(brd);
   155 ai.BlindCycle.prototype.analyse = function(brd2d) {
       
   156     var origBrd = new this.brdEngine(brd2d);
   154     this.prevDir = this.prevDir || 0;
   157     this.prevDir = this.prevDir || 0;
   155     if (!this.cfg.whilePossible)
   158     if (!this.cfg.whilePossible)
   156         this.prevDir = this.nextDir(this.prevDir);
   159         this.prevDir = this.nextDir(this.prevDir);
   157     while (true) {
   160     while (true) {
   158         if (origBrd[ai.BlindCycle.canDirs[this.prevDir]]())
   161         if (origBrd[ai.BlindCycle.canDirs[this.prevDir]]())
   207         weight += this.cfg.edgeBonus;
   210         weight += this.cfg.edgeBonus;
   208     if (this.cfg.freeBonus > 0)
   211     if (this.cfg.freeBonus > 0)
   209         weight += this.cfg.freeBonus * brd.free();
   212         weight += this.cfg.freeBonus * brd.free();
   210     return weight;
   213     return weight;
   211 }
   214 }
   212 ai.OneStepAhead.prototype.analyse = function(brd) {
   215 /** Select best direction for next step. */
   213     var origBrd = new this.brdEngine(brd);
   216 ai.OneStepAhead.prototype.analyse = function(brd2d) {
       
   217     var origBrd = new this.brdEngine(brd2d);
   214     var nextBrd = new this.brdEngine();
   218     var nextBrd = new this.brdEngine();
   215     var maxWeight = -1;
   219     var maxWeight = -1;
   216     var bestDir;
   220     var bestDir;
   217     for (var i = 0; i < ai.dirs.length; i++) {
   221     for (var i = 0; i < ai.dirs.length; i++) {
   218         var dir = ai.dirs[i];
   222         var dir = ai.dirs[i];
   269         weight += this.cfg.edgeBonus;
   273         weight += this.cfg.edgeBonus;
   270     if (this.cfg.freeBonus > 0)
   274     if (this.cfg.freeBonus > 0)
   271         weight += this.cfg.freeBonus * brd.free();
   275         weight += this.cfg.freeBonus * brd.free();
   272     return weight;
   276     return weight;
   273 }
   277 }
   274 ai.StaticDeepMerges.prototype.analyse = function(brd) {
   278 /** Select best direction for next step. */
   275     var origBrd = new this.brdEngine(brd);
   279 ai.StaticDeepMerges.prototype.analyse = function(brd2d) {
       
   280     var origBrd = new this.brdEngine(brd2d);
   276     var nextBrd = new this.brdEngine();
   281     var nextBrd = new this.brdEngine();
   277     var prevScore = -1, nextScore = -1;
   282     var prevScore = -1, nextScore = -1;
   278     var maxWeight = -1;
   283     var maxWeight = -1;
   279     var bestDir;
   284     var bestDir;
   280     for (var i = 0; i < ai.dirs.length; i++) {
   285     for (var i = 0; i < ai.dirs.length; i++) {
   362     }
   367     }
   363     if (cfg.freeBonus > 0)
   368     if (cfg.freeBonus > 0)
   364         score += cfg.freeBonus * brd.free();
   369         score += cfg.freeBonus * brd.free();
   365     return score;
   370     return score;
   366 }
   371 }
   367 ai.expectimax.prototype.analyse = function(brd) {
   372 /** Select best direction for next step. */
       
   373 ai.expectimax.prototype.analyse = function(brd2d) {
   368     this.brdCache = new ai.brdCache();
   374     this.brdCache = new ai.brdCache();
   369     var origBrd = new this.brdEngine(brd);
   375     var origBrd = new this.brdEngine(brd2d);
   370     var nextBrd = new this.brdEngine();
   376     var nextBrd = new this.brdEngine();
   371     var maxW = -1;
   377     var maxW = -1;
   372     var bestDir;
   378     var bestDir;
   373     this.cleanup();
   379     this.cleanup();
   374     this.depthLimit = this.cfg.depth;
   380     this.depthLimit = this.cfg.depth;