Skip to content

Instantly share code, notes, and snippets.

@JetFault
Last active August 29, 2015 13:57
Show Gist options
  • Save JetFault/9481202 to your computer and use it in GitHub Desktop.
Save JetFault/9481202 to your computer and use it in GitHub Desktop.
2048 maj0r h4x
function press(key) {
var eventObj = document.createEvent("Events");
eventObj.initEvent("keydown", true, true);
eventObj.which = key;
eventObj.keyIdentifier = 'Trash';
document.dispatchEvent(eventObj);
}
var qs = (document.querySelector).bind(document);
var max_score = 0;
var old_score = 0;
var old_numbers = null;
function haveToPressUp() {
var numbers = qs(".tile-container").textContent;
var score = parseInt(qs(".score-container").childNodes[0].textContent);
var ret = false;
if (old_score === score && numbers === old_numbers) {
ret = true;
}
old_score = score;
old_numbers = numbers;
return ret;
}
function runme(done) {
if (qs(".game-over")) {
qs(".retry-button").click();
}
function pressStuff(done) {
press(39); //right
press(40); //down
press(37); //left
press(40); //down
if(haveToPressUp()) {
press(38); //up
}
if(qs(".game-over")) {
var score = parseInt(qs(".score-container").childNodes[0].textContent);
if (score > max_score) {
max_score = score;
console.log("New Max Score: " + max_score);
qs('.title').textContent = max_score;
}
return done();
}
setTimeout(function() { pressStuff(done);}, 50);
}
pressStuff(done);
}
stop = false;
function looper() {
if(!stop) {
runme(looper);
}
}
runme(looper)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment