ai.js
changeset 164 cdde49008500
parent 163 87479ae56889
child 169 5a17638f0dca
equal deleted inserted replaced
163:87479ae56889 164:cdde49008500
   566     if (this.cfg.simulations <= 0)
   566     if (this.cfg.simulations <= 0)
   567         this.cfg.simulations = ai.MonteCarlo.bestCfg.simulations;
   567         this.cfg.simulations = ai.MonteCarlo.bestCfg.simulations;
   568     if (!this.cfg.maxDepth || this.cfg.maxDepth <= 0 || 20 <= this.cfg.maxDepth)
   568     if (!this.cfg.maxDepth || this.cfg.maxDepth <= 0 || 20 <= this.cfg.maxDepth)
   569         this.cfg.maxDepth = ai.MonteCarlo.bestCfg.maxDepth;
   569         this.cfg.maxDepth = ai.MonteCarlo.bestCfg.maxDepth;
   570 }
   570 }
   571 ai.MonteCarlo.bestCfg = {simulations: 1000, maxDepth: 20};
   571 ai.MonteCarlo.bestCfg = {simulations: 1000, maxDepth: 20, cornerBonus: 0, edgeBonus: 0, freeBonus: 0};
   572 /** Select best direction for next step. */
   572 /** Select best direction for next step. */
   573 ai.MonteCarlo.prototype.analyse = function(brd2d) {
   573 ai.MonteCarlo.prototype.analyse = function(brd2d) {
   574     var origBrd = new this.brd(brd2d);
   574     var origBrd = new this.brd(brd2d);
   575     var nextBrd = new this.brd();
   575     var nextBrd = new this.brd();
   576     var bestW = - this.cfg.simulations;
   576     var bestW = - this.cfg.simulations;
   592     }
   592     }
   593     return bestDir;
   593     return bestDir;
   594 }
   594 }
   595 ai.MonteCarlo.prototype.play = function(brd, depth) {
   595 ai.MonteCarlo.prototype.play = function(brd, depth) {
   596     if (depth <= 0) {
   596     if (depth <= 0) {
   597         return brd.freeCnt();
   597         return this.evalFn(brd);
   598     }
   598     }
   599     brd.rnd(1);
   599     brd.rnd(1);
   600     var dirs = ai.randDirs();
   600     var dirs = ai.randDirs();
   601     for (var i = 0; i < 4; i++) {
   601     for (var i = 0; i < 4; i++) {
   602         var dir = dirs[i];
   602         var dir = dirs[i];
   605             return this.play(nextBrd, depth-1);
   605             return this.play(nextBrd, depth-1);
   606         }
   606         }
   607     }
   607     }
   608     return -1;
   608     return -1;
   609 }
   609 }
       
   610 ai.MonteCarlo.prototype.evalFn = function(brd) {
       
   611     var w = 0;
       
   612     if (this.cfg.freeBonus > 0)
       
   613         w += this.cfg.freeBonus * brd.freeCnt();
       
   614     var max = brd.maxVal();
       
   615     if (max > 7) {
       
   616         if (this.cfg.cornerBonus > 0 && brd.atCorner(max))
       
   617             w += this.cfg.cornerBonus;
       
   618         if (this.cfg.edgeBonus > 0 && brd.atEdge(max))
       
   619             w += this.cfg.edgeBonus;
       
   620     }
       
   621     return w;
       
   622 }
   610 /* Mark that next board will be unrelated to previous, so any stored precompution can be cleared. */
   623 /* Mark that next board will be unrelated to previous, so any stored precompution can be cleared. */
   611 ai.MonteCarlo.prototype.cleanup = function() {
   624 ai.MonteCarlo.prototype.cleanup = function() {
   612 }
   625 }
   613 
   626