ai.js
changeset 126 e634761d0432
parent 125 fa1f44d3888d
child 135 935a500cea3b
equal deleted inserted replaced
125:fa1f44d3888d 126:e634761d0432
   316     ai.copyObj(cfg, this.cfg);
   316     ai.copyObj(cfg, this.cfg);
   317     if (this.cfg.balance <= 0)
   317     if (this.cfg.balance <= 0)
   318         this.cfg.balance = ai.expectimax.bestCfg.balance;
   318         this.cfg.balance = ai.expectimax.bestCfg.balance;
   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 || 9 <= 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};
   324 ai.expectimax.bestCfg = {balance: .9, depth: 5, 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();
   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 maxW = -1;
   348     var maxW = -1;
   349     var bestDir;
   349     var bestDir;
   350     this.cleanup();
   350     this.cleanup();
       
   351     this.depthLimit = this.cfg.depth;
       
   352     var free = origBrd.free();
       
   353     if (free >= 6)
       
   354         this.depthLimit = Math.min(this.depthLimit, 6 - free/3);
   351     for (var i = 0; i < ai.dirs.length; i++) {
   355     for (var i = 0; i < ai.dirs.length; i++) {
   352         var dir = ai.dirs[i];
   356         var dir = ai.dirs[i];
   353         if (origBrd[dir](nextBrd)) {
   357         if (origBrd[dir](nextBrd)) {
   354             var w = this.evalFn(nextBrd, 1);
   358             var w = this.evalFn(nextBrd, 1);
   355             if (w > maxW) {
   359             if (w > maxW) {
   360     }
   364     }
   361     this.cleanup();
   365     this.cleanup();
   362     return bestDir;
   366     return bestDir;
   363 }
   367 }
   364 ai.expectimax.prototype.evalFn = function(brd, depth) {
   368 ai.expectimax.prototype.evalFn = function(brd, depth) {
   365     if (depth >= this.cfg.depth)
   369     if (depth >= this.depthLimit)
   366         return this.weight(brd);
   370         return this.weight(brd);
   367     if (this.cache[depth]) {
   371     if (this.cache[depth]) {
   368         var cache = this.cache[depth];
   372         var cache = this.cache[depth];
   369         for (var i = cache.length-1; i >= 0; i--) {
   373         for (var i = cache.length-1; i >= 0; i--) {
   370             if (brd.equals(cache[i].brd))
   374             if (brd.equals(cache[i].brd))