ai.js
changeset 125 fa1f44d3888d
parent 124 4a2551dd179d
child 126 e634761d0432
equal deleted inserted replaced
124:4a2551dd179d 125:fa1f44d3888d
   319     if ( this.cfg.balance > 1)
   319     if ( this.cfg.balance > 1)
   320         this.cfg.balance = 1;
   320         this.cfg.balance = 1;
   321     if (!this.cfg.depth || this.cfg.depth < 0 || 8 <= this.cfg.depth)
   321     if (!this.cfg.depth || this.cfg.depth < 0 || 8 <= this.cfg.depth)
   322         this.cfg.depth = ai.expectimax.bestCfg.depth;
   322         this.cfg.depth = ai.expectimax.bestCfg.depth;
   323 }
   323 }
   324 ai.expectimax.bestCfg = {balance: .9, depth: 3, scoreCoef: 1, maxValCoef: 0, cornerBonus: 0, edgeBonus: 0, freeBonus: 0, weightThreshold: 10};
   324 ai.expectimax.bestCfg = {balance: .9, depth: 3, scoreCoef: 1, maxValCoef: 0, cornerBonus: 0, edgeBonus: 0, freeBonus: 0};
   325 ai.expectimax.prototype.weight = function(brd) {
   325 ai.expectimax.prototype.weight = function(brd) {
   326     var score = 0;
   326     var score = 0;
   327     var cfg = this.cfg;
   327     var cfg = this.cfg;
   328     if (cfg.scoreCoef > 0)
   328     if (cfg.scoreCoef > 0)
   329         score += cfg.scoreCoef * brd.score();
   329         score += cfg.scoreCoef * brd.score();
   343     return score;
   343     return score;
   344 }
   344 }
   345 ai.expectimax.prototype.analyse = function(brd) {
   345 ai.expectimax.prototype.analyse = function(brd) {
   346     var origBrd = new this.brdEngine(brd);
   346     var origBrd = new this.brdEngine(brd);
   347     var nextBrd = new this.brdEngine();
   347     var nextBrd = new this.brdEngine();
   348     var prevScore = -1, nextScore = -1;
   348     var maxW = -1;
   349     var maxWeight = -1;
       
   350     var bestDir;
   349     var bestDir;
   351     this.cleanup();
   350     this.cleanup();
   352     for (var i = 0; i < ai.dirs.length; i++) {
   351     for (var i = 0; i < ai.dirs.length; i++) {
   353         var dir = ai.dirs[i];
   352         var dir = ai.dirs[i];
   354         if (origBrd[dir](nextBrd)) {
   353         if (origBrd[dir](nextBrd)) {
   355             var weight = this.evalFn(nextBrd, 1);
   354             var w = this.evalFn(nextBrd, 1);
   356             var ok = (weight - maxWeight) > this.cfg.weightThreshold;
   355             if (w > maxW) {
   357             if ( ! ok && maxWeight <= weight) {
   356                 maxW = w;
   358                 nextScore = this.weight(nextBrd);
       
   359                 ok = prevScore < nextScore;
       
   360             }
       
   361             if (ok) {
       
   362                 prevScore = nextScore;
       
   363                 maxWeight = weight;
       
   364                 bestDir = dir;
   357                 bestDir = dir;
   365             }
   358             }
   366         }
   359         }
   367     }
   360     }
   368     this.cleanup();
   361     this.cleanup();