ai.js
changeset 99 df4736e659f2
parent 74 93cb48b73b39
child 104 47d42234dd5c
equal deleted inserted replaced
98:eb4f89e2a001 99:df4736e659f2
   151 // for each free field.
   151 // for each free field.
   152 ////////////////////////////////////////////////////////////////
   152 ////////////////////////////////////////////////////////////////
   153 
   153 
   154 /**
   154 /**
   155  * Defines coefficient for linear resulted weight function.
   155  * Defines coefficient for linear resulted weight function.
   156  * @name ai.OneStepDeep.cfg
   156  * @name ai.OneStepAhead.cfg
   157  * @namespace
   157  * @namespace
   158  * @property {number} scoreCoef    multiplicator for score
   158  * @property {number} scoreCoef    multiplicator for score
   159  * @property {number} maxValCoef   multiplicator for max value
   159  * @property {number} maxValCoef   multiplicator for max value
   160  * @property {number} cornerBonus  bonus for max value at board corner
   160  * @property {number} cornerBonus  bonus for max value at board corner
   161  * @property {number} edgeBonus    bonus for max value at board edge
   161  * @property {number} edgeBonus    bonus for max value at board edge
   162  * @property {number} freeBonus    bonus foe each free cell
   162  * @property {number} freeBonus    bonus foe each free cell
   163  */
   163  */
   164 
   164 
   165 /** 1 step deep with * AI.
   165 /** 1 step deep with * AI.
   166  * @param {Board} brdEngine  board engine from board.js
   166  * @param {Board} brdEngine  board engine from board.js
   167  * @param {ai.OneStepDeep.cfg} cfg  configuration settings
   167  * @param {ai.OneStepAhead.cfg} cfg  configuration settings
   168  * @constructor */
   168  * @constructor */
   169 ai.OneStepDeep = function(brdEngine, cfg) {
   169 ai.OneStepAhead = function(brdEngine, cfg) {
   170     this.brdEngine = brdEngine;
   170     this.brdEngine = brdEngine;
   171     this.cfg = ai.copyObj(ai.OneStepDeep.bestCfg);
   171     this.cfg = ai.copyObj(ai.OneStepAhead.bestCfg);
   172     ai.copyObj(cfg, this.cfg);
   172     ai.copyObj(cfg, this.cfg);
   173 }
   173 }
   174 ai.OneStepDeep.bestCfg = {scoreCoef: 1, maxValCoef: 0, cornerBonus: 0, edgeBonus: 0, freeBonus: 0};
   174 ai.OneStepAhead.bestCfg = {scoreCoef: 1, maxValCoef: 0, cornerBonus: 0, edgeBonus: 0, freeBonus: 0};
   175 ai.OneStepDeep.prototype.weight = function(brd) {
   175 ai.OneStepAhead.prototype.weight = function(brd) {
   176     var weight = 0;
   176     var weight = 0;
   177     if (this.cfg.scoreCoef > 0)
   177     if (this.cfg.scoreCoef > 0)
   178         weight += this.cfg.scoreCoef * brd.score();
   178         weight += this.cfg.scoreCoef * brd.score();
   179     var max = brd.max();
   179     var max = brd.max();
   180     if (this.cfg.maxValCoef > 0)
   180     if (this.cfg.maxValCoef > 0)
   185         weight += this.cfg.edgeBonus;
   185         weight += this.cfg.edgeBonus;
   186     if (this.cfg.freeBonus > 0)
   186     if (this.cfg.freeBonus > 0)
   187         weight += this.cfg.freeBonus * brd.free();
   187         weight += this.cfg.freeBonus * brd.free();
   188     return weight;
   188     return weight;
   189 }
   189 }
   190 ai.OneStepDeep.prototype.analyse = function(brd) {
   190 ai.OneStepAhead.prototype.analyse = function(brd) {
   191     var origBrd = new this.brdEngine(brd);
   191     var origBrd = new this.brdEngine(brd);
   192     var nextBrd = new this.brdEngine();
   192     var nextBrd = new this.brdEngine();
   193     var maxWeight = -1;
   193     var maxWeight = -1;
   194     var bestDir;
   194     var bestDir;
   195     for (var i = 0; i < ai.dirs.length; i++) {
   195     for (var i = 0; i < ai.dirs.length; i++) {
   203         }
   203         }
   204     }
   204     }
   205     return bestDir;
   205     return bestDir;
   206 }
   206 }
   207 /* Mark that next board will be unrelated to previous, so any stored precompution can be cleared. */
   207 /* Mark that next board will be unrelated to previous, so any stored precompution can be cleared. */
   208 ai.OneStepDeep.prototype.cleanup = function() { }
   208 ai.OneStepAhead.prototype.cleanup = function() { }
   209 
   209 
   210 
   210 
   211 
   211 
   212 ////////////////////////////////////////////////////////////////
   212 ////////////////////////////////////////////////////////////////
   213 // N level deep on score value without random simulation.
   213 // N level deep on score value without random simulation.