Last active
August 29, 2015 14:05
-
-
Save hongaar/9b2f26ae2faee1b2904e to your computer and use it in GitHub Desktop.
2048 kickstart for use on i.e. http://gabrielecirulli.github.io/2048/ (only tested in Chrome)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Sequence of keys to press | |
var sequence = "luuluuruuruuluuluuruuruu"; // averaging 4000 | |
var stalledKey = "d"; | |
// How many iterations you want to try? | |
var iterations = 100; | |
// Stop trying at minimum score of | |
var haltat = 5000; | |
// Some weird code | |
var map = { | |
"l": 37, | |
"u": 38, | |
"r": 39, | |
"d": 40 | |
}; | |
// Reset the game | |
var e = document.createEvent("Events"); | |
e.initEvent("click", true, true); | |
document.querySelector('.retry-button').dispatchEvent(e); | |
// Emulate key press function | |
function fireKey(direction) | |
{ | |
var key = map[direction]; | |
var e = document.createEvent("Events"); | |
e.initEvent("keydown", true, true); | |
e.which = key; | |
document.getElementsByTagName('body')[0].dispatchEvent(e); | |
} | |
// Init vars | |
sequence = sequence.split(""); | |
var index = 0; | |
var scores = []; | |
var left = iterations; | |
var running = true; | |
var score; | |
var stalled = []; | |
// Is the given score the same as the current one, press stalled key | |
var checkstalled = function(current_score) { | |
return function() { | |
if (current_score == score) { | |
console.log("stalled?"); | |
fireKey(stalledKey); | |
for (var i = 0, len = stalled.length; i < len; i++) { | |
if (stalled[i]) { | |
clearTimeout(stalled[i]); | |
stalled[i] = null; | |
} | |
} | |
} | |
}; | |
}; | |
// Do the loop | |
clearInterval(loop); | |
var loop = setInterval(function() { | |
if (index >= sequence.length) index = 0; | |
fireKey(sequence[index]); | |
score = parseInt(document.querySelector('.score-container').textContent, 10); | |
if (score >= haltat) { | |
clearInterval(loop); | |
return; | |
} | |
if (document.querySelector(".game-message.game-over")) { | |
if (!left && running) { | |
console.log('best score ' + Math.max.apply(Math, scores) + ", average " + (scores.reduce(function(a, b) { return a + b }) / scores.length) ); | |
running = false; | |
} else if (running) { | |
scores.push(score); | |
console.log("score " + score + ", average " + (scores.reduce(function(a, b) { return a + b }) / scores.length) ); | |
var e = document.createEvent("Events"); | |
e.initEvent("click", true, true); | |
document.querySelector('.retry-button').dispatchEvent(e); | |
left--; | |
} | |
} else if (!running) { | |
left = iterations; | |
running = true; | |
} else { | |
stalled.push(setTimeout(checkstalled(score), 250)); | |
} | |
index++; | |
}, 1); | |
console.log("started"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fireKey(a){var b=map[a],c=document.createEvent("Events");c.initEvent("keydown",!0,!0),c.which=b,document.getElementsByTagName("body")[0].dispatchEvent(c)}var sequence="luuluuruuruuluuluuruuruu",stalledKey="d",iterations=100,haltat=5e3,map={l:37,u:38,r:39,d:40},e=document.createEvent("Events");e.initEvent("click",!0,!0),document.querySelector(".retry-button").dispatchEvent(e),sequence=sequence.split("");var index=0,scores=[],left=iterations,running=!0,score,stalled=[],checkstalled=function(a){return function(){if(a==score){console.log("stalled?"),fireKey(stalledKey);for(var b=0,c=stalled.length;c>b;b++)stalled[b]&&(clearTimeout(stalled[b]),stalled[b]=null)}}};clearInterval(loop);var loop=setInterval(function(){if(index>=sequence.length&&(index=0),fireKey(sequence[index]),score=parseInt(document.querySelector(".score-container").textContent,10),score>=haltat)return clearInterval(loop),void 0;if(document.querySelector(".game-message.game-over")){if(!left&&running)console.log("best score "+Math.max.apply(Math,scores)+", average "+scores.reduce(function(a,b){return a+b})/scores.length),running=!1;else if(running){scores.push(score),console.log("score "+score+", average "+scores.reduce(function(a,b){return a+b})/scores.length);var a=document.createEvent("Events");a.initEvent("click",!0,!0),document.querySelector(".retry-button").dispatchEvent(a),left--}}else running?stalled.push(setTimeout(checkstalled(score),250)):(left=iterations,running=!0);index++},1);console.log("started"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment