Skip to content

Instantly share code, notes, and snippets.

@jeanbza
Last active July 9, 2023 03:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeanbza/9652870 to your computer and use it in GitHub Desktop.
Save jeanbza/9652870 to your computer and use it in GitHub Desktop.
Automate 2048

So I got to playing 2048 a decent amount, and starting getting the feeling that there was a 'pattern' that would win this game for me. Perhaps you've had the same thought! As a result, I wrote a little snippet of code that that cycles throw the keys Up, Right, Down, Left (feel free to create your own pattern).

Copy and paste this into your Chrome/Firefox console to automate 2048!

Firefox

function pressKey(i) {
  var evt = document.createEvent("KeyboardEvent");
  evt.initKeyEvent ("keydown", true, true, window, 0, 0, 0, 0, 37+i%4, 37+i%4);
  document.dispatchEvent(evt);

  window.setTimeout(function(){pressKey(i+1);}, 500);
}

pressKey(0);

Chrome

Note: Chrome has an initKeyboardEvent bug that causes KeyboardEvent.keyCode and KeyboardEvent.which to be readonly (e.g. always 0), so we overwrite it as below. This workaround taken from stackoverflow

Podium = {};
Podium.keydown = function(k) {
    var oEvent = document.createEvent('KeyboardEvent');

    // Chromium Hack
    Object.defineProperty(oEvent, 'keyCode', {
                get : function() {
                    return this.keyCodeVal;
                }
    });     
    Object.defineProperty(oEvent, 'which', {
                get : function() {
                    return this.keyCodeVal;
                }
    });     

    if (oEvent.initKeyboardEvent) {
        oEvent.initKeyboardEvent("keydown", true, true, document.defaultView, k, k, "", "", false, "");
    } else {
        oEvent.initKeyEvent("keydown", true, true, document.defaultView, false, false, false, false, k, 0);
    }

    oEvent.keyCodeVal = k;

    if (oEvent.keyCode !== k) {
        alert("keyCode mismatch " + oEvent.keyCode + "(" + oEvent.which + ")");
    }

    document.dispatchEvent(oEvent);
}

function pressKey(i) {
  Podium.keydown(37+i%4);

  window.setTimeout(function(){pressKey(i+1);}, 500);
}

pressKey(0);
@parkerlreed
Copy link

Firefox 46

TypeError: event.target.tagName is undefined
KeyboardInputManager.prototype.targetIsInput()
 keyboard_input_manager.js:152
KeyboardInputManager.prototype.listen/<()
 keyboard_input_manager.js:59
pressKey()
 debugger eval code:4
<anonymous>
 debugger eval code:9
WCA_evalWithDebugger()
 webconsole.js:1276
WCA_onEvaluateJS()
 webconsole.js:870
WCA_onEvaluateJSAsync()
 webconsole.js:840
WCA_onEvaluateJSAsync()
 self-hosted:736
DSC_onPacket()
 main.js:1643
ChildDebuggerTransport.prototype.receiveMessage()

TypeError: event.target.tagName is undefined
KeyboardInputManager.prototype.targetIsInput()
 keyboard_input_manager.js:152
KeyboardInputManager.prototype.listen/<()
 keyboard_input_manager.js:59
pressKey()
 debugger eval code:4
pressKey/<()

Copy link

ghost commented Jan 31, 2022

Thanks you

@nguyentoanxtn
Copy link

How can I speed up the game?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment