ai.js
changeset 172 021cd45cb5ef
parent 171 6fce1391c2c0
equal deleted inserted replaced
171:6fce1391c2c0 172:021cd45cb5ef
    81             return ai.dirs[rnd];
    81             return ai.dirs[rnd];
    82     }
    82     }
    83 }
    83 }
    84 /* Mark that next board will be unrelated to previous, so any stored precompution can be cleared. */
    84 /* Mark that next board will be unrelated to previous, so any stored precompution can be cleared. */
    85 ai.BlindRandom.prototype.cleanup = function() { }
    85 ai.BlindRandom.prototype.cleanup = function() { }
       
    86 
       
    87 
       
    88 
       
    89 ////////////////////////////////////////////////////////////////
       
    90 // Always up AI.
       
    91 ////////////////////////////////////////////////////////////////
       
    92 
       
    93 /** Always up AI.
       
    94  * @param {Board} brd  board engine from board.js
       
    95  * @constructor */
       
    96 ai.AlwaysUp = function(brd) {
       
    97     this.brd = brd;
       
    98 }
       
    99 /** Select best direction for next step. */
       
   100 ai.AlwaysUp.prototype.analyse = function(brd2d) {
       
   101     var origBrd = new this.brd(brd2d);
       
   102     var nextBrd = new this.brd();
       
   103     var tmpBrd = new this.brd();
       
   104     if (origBrd.canUp())
       
   105         return "up";
       
   106     var canRight = origBrd.right(nextBrd);
       
   107     if (canRight) {
       
   108         var canRightUp = nextBrd.up(tmpBrd);
       
   109         var rightScore = tmpBrd.score();
       
   110     }
       
   111     var canLeft = origBrd.left(nextBrd);
       
   112     if (canLeft) {
       
   113         var canLeftUp = nextBrd.up(tmpBrd);
       
   114         var leftScore = tmpBrd.score();
       
   115     }
       
   116     if (canRight && canLeft) {
       
   117         if (canRightUp && canLeftUp)
       
   118             return (rightScore > leftScore) ? "right": "left";
       
   119         else if (canRightUp)
       
   120             return "right";
       
   121         else
       
   122             return "left";
       
   123     } else if (canRight)
       
   124         return "right";
       
   125     else if (canLeft)
       
   126         return "left";
       
   127     return "down";
       
   128 }
       
   129 /* Mark that next board will be unrelated to previous, so any stored precompution can be cleared. */
       
   130 ai.AlwaysUp.prototype.cleanup = function() { }
    86 
   131 
    87 
   132 
    88 
   133 
    89 ////////////////////////////////////////////////////////////////
   134 ////////////////////////////////////////////////////////////////
    90 // Blind weight random AI.
   135 // Blind weight random AI.