# HG changeset patch # User Oleksandr Gavenko # Date 1411714430 -10800 # Node ID cab72d83a6e60419c7b3f070ecc092486cc370f6 # Parent 2839f8227a3881c07c23fec3a0d914edbf2c4beb# Parent bf3d47ecd0ddf3ca5fe7a49a33ce9602b1a75e89 merged diff -r bf3d47ecd0dd -r cab72d83a6e6 2048.html --- a/2048.html Fri Sep 26 00:57:00 2014 +0300 +++ b/2048.html Fri Sep 26 09:53:50 2014 +0300 @@ -504,7 +504,7 @@ return false; } if ( ! board.move[move].call(null, tmpBrd)) { - ui.game.setMessage("AI move "+move+" is ivalid!"); + ui.game.setMessage("AI move '"+move+"' is ivalid!"); return false; } return true; diff -r bf3d47ecd0dd -r cab72d83a6e6 README.rst --- a/README.rst Fri Sep 26 00:57:00 2014 +0300 +++ b/README.rst Fri Sep 26 09:53:50 2014 +0300 @@ -28,5 +28,5 @@ ============= * http://ov3y.github.io/2048-AI/ - online JS AI. - * http://sztupy.github.io/2048-Hard/ - online JS AI with difficulty level. + * http://sztupy.github.io/2048-Hard/ - online JS AI with difficulty levels. diff -r bf3d47ecd0dd -r cab72d83a6e6 ai.js --- a/ai.js Fri Sep 26 00:57:00 2014 +0300 +++ b/ai.js Fri Sep 26 09:53:50 2014 +0300 @@ -59,8 +59,9 @@ ai.BlindRandom = function(brdEngine) { this.brdEngine = brdEngine; } -ai.BlindRandom.prototype.analyse = function(brd) { - var origBrd = new this.brdEngine(brd); +/** Select best direction for next step. */ +ai.BlindRandom.prototype.analyse = function(brd2d) { + var origBrd = new this.brdEngine(brd2d); while (true) { var rnd = Math.floor(Math.random()*4); if (origBrd[ai.canDirs[rnd]]()) @@ -99,8 +100,9 @@ this.threshold3 = (this.cfg.left + this.cfg.down + this.cfg.right)/total; } ai.BlindWeightRandom.bestCfg = { left: 1, right: 16, up: 4, down: 8 }; -ai.BlindWeightRandom.prototype.analyse = function(brd) { - var origBrd = new this.brdEngine(brd); +/** Select best direction for next step. */ +ai.BlindWeightRandom.prototype.analyse = function(brd2d) { + var origBrd = new this.brdEngine(brd2d); while (true) { var rnd = Math.random(); if (rnd < this.threshold1) @@ -149,8 +151,9 @@ else return (dir + 1) % 4; } -ai.BlindCycle.prototype.analyse = function(brd) { - var origBrd = new this.brdEngine(brd); +/** Select best direction for next step. */ +ai.BlindCycle.prototype.analyse = function(brd2d) { + var origBrd = new this.brdEngine(brd2d); this.prevDir = this.prevDir || 0; if (!this.cfg.whilePossible) this.prevDir = this.nextDir(this.prevDir); @@ -198,7 +201,7 @@ var weight = 0; if (this.cfg.scoreCoef > 0) weight += this.cfg.scoreCoef * brd.score(); - var max = brd.max(); + var max = brd.maxVal(); if (this.cfg.maxValCoef > 0) weight += this.cfg.maxValCoef * max; if (this.cfg.cornerBonus > 0 && brd.atCorner(max)) @@ -206,11 +209,12 @@ if (this.cfg.edgeBonus > 0 && brd.atEdge(max)) weight += this.cfg.edgeBonus; if (this.cfg.freeBonus > 0) - weight += this.cfg.freeBonus * brd.free(); + weight += this.cfg.freeBonus * brd.freeCnt(); return weight; } -ai.OneStepAhead.prototype.analyse = function(brd) { - var origBrd = new this.brdEngine(brd); +/** Select best direction for next step. */ +ai.OneStepAhead.prototype.analyse = function(brd2d) { + var origBrd = new this.brdEngine(brd2d); var nextBrd = new this.brdEngine(); var maxWeight = -1; var bestDir; @@ -260,7 +264,7 @@ var weight = 0; if (this.cfg.scoreCoef > 0) weight += this.cfg.scoreCoef * brd.score(); - var max = brd.max(); + var max = brd.maxVal(); if (this.cfg.maxValCoef > 0) weight += this.cfg.maxValCoef * max; if (this.cfg.cornerBonus > 0 && brd.atCorner(max)) @@ -268,11 +272,12 @@ if (this.cfg.edgeBonus > 0 && brd.atEdge(max)) weight += this.cfg.edgeBonus; if (this.cfg.freeBonus > 0) - weight += this.cfg.freeBonus * brd.free(); + weight += this.cfg.freeBonus * brd.freeCnt(); return weight; } -ai.StaticDeepMerges.prototype.analyse = function(brd) { - var origBrd = new this.brdEngine(brd); +/** Select best direction for next step. */ +ai.StaticDeepMerges.prototype.analyse = function(brd2d) { + var origBrd = new this.brdEngine(brd2d); var nextBrd = new this.brdEngine(); var prevScore = -1, nextScore = -1; var maxWeight = -1; @@ -350,7 +355,7 @@ if (cfg.scoreCoef > 0) score += cfg.scoreCoef * brd.score(); if (cfg.maxValCoef > 0 || cfg.cornerBonus > 0 || cfg.edgeBonus > 0) { - var max = brd.max(); + var max = brd.maxVal(); if (cfg.maxValCoef > 0) score += cfg.maxValCoef * max; if (cfg.cornerBonus > 0) @@ -361,20 +366,21 @@ score += cfg.edgeBonus; } if (cfg.freeBonus > 0) - score += cfg.freeBonus * brd.free(); + score += cfg.freeBonus * brd.freeCnt(); return score; } -ai.expectimax.prototype.analyse = function(brd) { +/** Select best direction for next step. */ +ai.expectimax.prototype.analyse = function(brd2d) { this.brdCache = new ai.brdCache(); - var origBrd = new this.brdEngine(brd); + var origBrd = new this.brdEngine(brd2d); var nextBrd = new this.brdEngine(); var maxW = -1; var bestDir; this.cleanup(); this.depthLimit = this.cfg.depth; - var free = origBrd.free(); - if (free >= 6) - this.depthLimit = Math.min(this.depthLimit, 6 - free/3); + var freeCnt = origBrd.freeCnt(); + if (freeCnt >= 6) + this.depthLimit = Math.min(this.depthLimit, 6 - freeCnt/3); for (var i = 0; i < ai.dirs.length; i++) { var dir = ai.dirs[i]; if (origBrd[dir](nextBrd)) { diff -r bf3d47ecd0dd -r cab72d83a6e6 board.js --- a/board.js Fri Sep 26 00:57:00 2014 +0300 +++ b/board.js Fri Sep 26 09:53:50 2014 +0300 @@ -64,7 +64,7 @@ return brd; } /** Number of free cell. */ -BoardArr.prototype.free = function() { +BoardArr.prototype.freeCnt = function() { var cnt = 0; for (var i = 0; i < 16; i++) if (this.brd[i] === 0) @@ -80,7 +80,7 @@ } return score; } -BoardArr.prototype.max = function() { +BoardArr.prototype.maxVal = function() { var max = 0; for (var i = 0; i < 4; i++) { for (var j = 0; j < 4; j++) { @@ -170,7 +170,7 @@ return brd; } /** Number of free cell. */ -BoardArr2d.prototype.free = function() { +BoardArr2d.prototype.freeCnt = function() { var cnt = 0; for (var i = 0; i < 4; i++) for (var j = 0; j < 4; j++) @@ -189,7 +189,7 @@ } return score; } -BoardArr2d.prototype.max = function() { +BoardArr2d.prototype.maxVal = function() { var max = 0; for (var i = 0; i < 4; i++) { for (var j = 0; j < 4; j++) { @@ -883,7 +883,7 @@ return brd; } /** Number of free cell. */ -BoardObj.prototype.free = function() { +BoardObj.prototype.freeCnt = function() { var cnt = 0; var brd = this.brd; if (brd.aa === 0) cnt++; if (brd.ab === 0) cnt++; if (brd.ac === 0) cnt++; if (brd.ad === 0) cnt++; @@ -918,7 +918,7 @@ score += lookup[brd.dd]; return score; } -BoardObj.prototype.max = function() { +BoardObj.prototype.maxVal = function() { var brd = this.brd; var max = brd.aa; max = Math.max(max, brd.ab);