Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kaeverens/82d4d1edf105ebc6c52d8ed8dbcbe4a3 to your computer and use it in GitHub Desktop.
Save kaeverens/82d4d1edf105ebc6c52d8ed8dbcbe4a3 to your computer and use it in GitHub Desktop.
script to play Kevin Lyda's 2048 game version
GameManager.prototype.actuateOld = GameManager.prototype.actuate;
GameManager.prototype.actuate = function() {
this.actuateOld();
var lowest= 0;
this.grid.eachCell(function(x, y, tile) {
if (tile && tile.value) lowest= lowest? Math.min(lowest, tile.value) : tile.value;
});
var dirBest = 0, dirGood=0;
for (var i = 0; i < this.grid.size; ++i) {
var c = [], dirHoriz=Math.random()>=0.5?1:3;
this.grid.cells.forEach(cv => {
if (cv[i]) {
c.push(cv[i].value)
};
});
c.forEach((cv, idx) => {
if (c[idx + 1] && c[idx + 1] == cv) {
dirGood = dirHoriz;
if (cv==lowest) dirBest=dirHoriz;
};
});
}
this.grid.cells.forEach(c => {
var cf = c.filter(d => {
return d
});
cf.forEach((cv, idx) => {
if (cf[idx + 1] && cf[idx + 1].value == cv.value) {
dirGood = 2;
if (cv.value==lowest) dirBest=2;
};
});
});
var $this = this, dirs=[2, 3, 1, 0], dirAt=0;
function makeMove() {
var dir=dirBest||dirGood||dirs[dirAt++];
var curState= JSON.stringify($this.serialize());
$this.move(dir);
if (JSON.stringify($this.serialize()) == curState) {
setTimeout(makeMove, 1);
}
}
setTimeout(makeMove, 10);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment