# HG changeset patch # User Oleksandr Gavenko # Date 1435987999 18000 # Node ID 021cd45cb5ef6d72b10b3e05c73d0dd5fce4fcdd # Parent 6fce1391c2c04fe7857ae5043d04f2f6a6a6629b Tend to one direction strategy. diff -r 6fce1391c2c0 -r 021cd45cb5ef 2048.html --- a/2048.html Fri Jul 03 23:45:55 2015 -0500 +++ b/2048.html Sat Jul 04 00:33:19 2015 -0500 @@ -189,6 +189,10 @@
bling random
+
+ +
always up
+
bling weight random
@@ -794,6 +798,9 @@ "ai-blind-random": function() { return new ai.BlindRandom(ui.brdEngine); }, + "ai-always-up": function() { + return new ai.AlwaysUp(ui.brdEngine); + }, "ai-blind-weight-random": function(aiDom) { var cfg = ui.ai.parseCfg(aiDom); return new ai.BlindWeightRandom(ui.brdEngine, cfg); diff -r 6fce1391c2c0 -r 021cd45cb5ef ai.js --- a/ai.js Fri Jul 03 23:45:55 2015 -0500 +++ b/ai.js Sat Jul 04 00:33:19 2015 -0500 @@ -87,6 +87,51 @@ //////////////////////////////////////////////////////////////// +// Always up AI. +//////////////////////////////////////////////////////////////// + +/** Always up AI. + * @param {Board} brd board engine from board.js + * @constructor */ +ai.AlwaysUp = function(brd) { + this.brd = brd; +} +/** Select best direction for next step. */ +ai.AlwaysUp.prototype.analyse = function(brd2d) { + var origBrd = new this.brd(brd2d); + var nextBrd = new this.brd(); + var tmpBrd = new this.brd(); + if (origBrd.canUp()) + return "up"; + var canRight = origBrd.right(nextBrd); + if (canRight) { + var canRightUp = nextBrd.up(tmpBrd); + var rightScore = tmpBrd.score(); + } + var canLeft = origBrd.left(nextBrd); + if (canLeft) { + var canLeftUp = nextBrd.up(tmpBrd); + var leftScore = tmpBrd.score(); + } + if (canRight && canLeft) { + if (canRightUp && canLeftUp) + return (rightScore > leftScore) ? "right": "left"; + else if (canRightUp) + return "right"; + else + return "left"; + } else if (canRight) + return "right"; + else if (canLeft) + return "left"; + return "down"; +} +/* Mark that next board will be unrelated to previous, so any stored precompution can be cleared. */ +ai.AlwaysUp.prototype.cleanup = function() { } + + + +//////////////////////////////////////////////////////////////// // Blind weight random AI. ////////////////////////////////////////////////////////////////