# HG changeset patch # User Oleksandr Gavenko # Date 1435381638 18000 # Node ID df8e645c3f366c787f345fdacfd78210c9b362c3 # Parent ae123e309e3199fbc4ec495ec343bdae2562c094 Refactoring: rename "brdEngine" to "brd". diff -r ae123e309e31 -r df8e645c3f36 ai.js --- a/ai.js Mon Jun 22 23:59:50 2015 -0500 +++ b/ai.js Sat Jun 27 00:07:18 2015 -0500 @@ -56,14 +56,14 @@ //////////////////////////////////////////////////////////////// /** Blind random AI. - * @param {Board} brdEngine board engine from board.js + * @param {Board} brd board engine from board.js * @constructor */ -ai.BlindRandom = function(brdEngine) { - this.brdEngine = brdEngine; +ai.BlindRandom = function(brd) { + this.brd = brd; } /** Select best direction for next step. */ ai.BlindRandom.prototype.analyse = function(brd2d) { - var origBrd = new this.brdEngine(brd2d); + var origBrd = new this.brd(brd2d); while (true) { var rnd = Math.floor(Math.random()*4); if (origBrd[ai.canFn[rnd]]()) @@ -89,11 +89,11 @@ */ /** Blind weight random AI. - * @param {Board} brdEngine board engine from board.js + * @param {Board} brd board engine from board.js * @param {ai.BlindWeightRandom.cfg} cfg configuration settings * @constructor */ -ai.BlindWeightRandom = function(brdEngine, cfg) { - this.brdEngine = brdEngine; +ai.BlindWeightRandom = function(brd, cfg) { + this.brd = brd; this.cfg = ai.copyObj(ai.BlindWeightRandom.bestCfg); ai.copyObj(cfg, this.cfg); var total = this.cfg.left + this.cfg.right + this.cfg.up + this.cfg.down; @@ -104,7 +104,7 @@ ai.BlindWeightRandom.bestCfg = { left: 1, right: 16, up: 4, down: 8 }; /** Select best direction for next step. */ ai.BlindWeightRandom.prototype.analyse = function(brd2d) { - var origBrd = new this.brdEngine(brd2d); + var origBrd = new this.brd(brd2d); while (true) { var rnd = Math.random(); if (rnd < this.threshold1) @@ -136,11 +136,11 @@ */ /** Blind cycle AI. - * @param {Board} brdEngine board engine from board.js + * @param {Board} brd board engine from board.js * @param {ai.BlindCycle.cfg} cfg configuration settings * @constructor */ -ai.BlindCycle = function(brdEngine, cfg) { - this.brdEngine = brdEngine; +ai.BlindCycle = function(brd, cfg) { + this.brd = brd; this.cfg = cfg || {}; this.cfg.whilePossible = this.cfg.whilePossible || false; this.cfg.clockwise = this.cfg.clockwise || false; @@ -155,7 +155,7 @@ } /** Select best direction for next step. */ ai.BlindCycle.prototype.analyse = function(brd2d) { - var origBrd = new this.brdEngine(brd2d); + var origBrd = new this.brd(brd2d); this.prevDir = this.prevDir || 0; if (!this.cfg.whilePossible) this.prevDir = this.nextDir(this.prevDir); @@ -190,11 +190,11 @@ */ /** 1 step deep with * AI. - * @param {Board} brdEngine board engine from board.js + * @param {Board} brd board engine from board.js * @param {ai.OneStepAhead.cfg} cfg configuration settings * @constructor */ -ai.OneStepAhead = function(brdEngine, cfg) { - this.brdEngine = brdEngine; +ai.OneStepAhead = function(brd, cfg) { + this.brd = brd; this.cfg = ai.copyObj(ai.OneStepAhead.bestCfg); ai.copyObj(cfg, this.cfg); } @@ -216,8 +216,8 @@ } /** Select best direction for next step. */ ai.OneStepAhead.prototype.analyse = function(brd2d) { - var origBrd = new this.brdEngine(brd2d); - var nextBrd = new this.brdEngine(); + var origBrd = new this.brd(brd2d); + var nextBrd = new this.brd(); var maxUtility = -1; var bestDir; for (var i = 0; i < ai.dirs.length; i++) { @@ -253,11 +253,11 @@ */ /** Deep merges AI without random simulation. - * @param {Board} brdEngine board engine from board.js + * @param {Board} brd board engine from board.js * @param {Object} cfg configuration settings * @constructor */ -ai.StaticDeepMerges = function(brdEngine, cfg) { - this.brdEngine = brdEngine; +ai.StaticDeepMerges = function(brd, cfg) { + this.brd = brd; this.cfg = ai.copyObj(ai.OneStepAhead.bestCfg); ai.copyObj(cfg, this.cfg); } @@ -279,8 +279,8 @@ } /** Select best direction for next step. */ ai.StaticDeepMerges.prototype.analyse = function(brd2d) { - var origBrd = new this.brdEngine(brd2d); - var nextBrd = new this.brdEngine(); + var origBrd = new this.brd(brd2d); + var nextBrd = new this.brd(); var prevScore = -1, nextScore = -1; var maxUtility = -1; var bestDir; @@ -305,7 +305,7 @@ ai.StaticDeepMerges.prototype.evalFn = function(brd) { var currScore = brd.score(); var maxUtility = currScore; - var nextBrd = new this.brdEngine(); + var nextBrd = new this.brd(); for (var i = 0; i < ai.dirs.length; i++) { if (brd[ai.dirs[i]](nextBrd)) { var score = nextBrd.score(); @@ -336,11 +336,11 @@ */ /** N level deep with random simulation. - * @param {Board} brdEngine board engine from board.js + * @param {Board} brd board engine from board.js * @param {ai.expectimax.cfg} cfg configuration settings * @constructor */ -ai.expectimax = function(brdEngine, cfg) { - this.brdEngine = brdEngine; +ai.expectimax = function(brd, cfg) { + this.brd = brd; this.cfg = ai.copyObj(ai.expectimax.bestCfg); ai.copyObj(cfg, this.cfg); if (this.cfg.balance <= 0) @@ -374,8 +374,8 @@ /** Select best direction for next step. */ ai.expectimax.prototype.analyse = function(brd2d) { this.brdCache = new ai.brdCache(); - var origBrd = new this.brdEngine(brd2d); - var nextBrd = new this.brdEngine(); + var origBrd = new this.brd(brd2d); + var nextBrd = new this.brd(); var maxW = -1; var bestDir; this.cleanup(); @@ -403,8 +403,8 @@ if (wCached) return wCached; var wMin = +Infinity; - var randBoard = new this.brdEngine(); - var nextBrd = new this.brdEngine(); + var randBoard = new this.brd(); + var nextBrd = new this.brd(); for (var i = 0; i < 3; i++) { for (var j = 0; j < 3; j++) { if (brd.get(i, j) === 0) { @@ -453,25 +453,25 @@ */ /** N level deep with random simulation. - * @param {Board} brdEngine board engine from board.js + * @param {Board} brd board engine from board.js * @param {ai.survive.cfg} cfg configuration settings * @constructor */ -ai.survive = function(brdEngine, cfg) { - this.brdEngine = brdEngine; +ai.survive = function(brd, cfg) { + this.brd = brd; this.cfg = ai.copyObj(ai.survive.bestCfg); ai.copyObj(cfg, this.cfg); if (this.cfg.freeCells <= 0) this.cfg.freeCells = ai.survive.bestCfg.freeCells; if (!this.cfg.maxDepth || this.cfg.maxDepth < 0 || 20 <= this.cfg.maxDepth) this.cfg.maxDepth = ai.survive.bestCfg.maxDepth; - this.cfg.altAI = new ai.StaticDeepMerges(brdEngine, ai.survive.altAICfg); + this.cfg.altAI = new ai.StaticDeepMerges(brd, ai.survive.altAICfg); } ai.survive.bestCfg = {freeCells: 8, maxDepth: 5}; ai.survive.altAICfg = {scoreCoef: 1, maxValCoef: 0, cornerBonus: 0, edgeBonus: 0, freeBonus: 0, utilityThreshold: 0}; /** Select best direction for next step. */ ai.survive.prototype.analyse = function(brd2d) { - var origBrd = new this.brdEngine(brd2d); - var nextBrd = new this.brdEngine(); + var origBrd = new this.brd(brd2d); + var nextBrd = new this.brd(); var bestW = -2; var bestDir; var freeCnt = origBrd.freeCnt(); @@ -495,8 +495,8 @@ if (depth >= this.cfg.maxDepth) return 0; var wMin = +Infinity; - var randBoard = new this.brdEngine(); - var nextBrd = new this.brdEngine(); + var randBoard = new this.brd(); + var nextBrd = new this.brd(); exit: for (var i = 0; i < 3; i++) { for (var j = 0; j < 3; j++) {