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++) { |