2048bot

try to take over the world!

目前為 2022-06-22 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         2048bot
// @namespace    http://tampermonkey.net/
// @version      0.94
// @description  try to take over the world!
// @author       boynextdesk
// @match        https://play2048.co/*
// @icon         https://play2048.co/favicon.ico
// @grant        none
// @license      GNU General Public License v3.0
// ==/UserScript==

(function() {
    // class="game-message game-over"
    // todo: inspect whether game is over
    // todo: train strategy after game-over
    // todo: set speed through page
    // todo: beautify layout
    'use strict';
    const aboveBox = document.getElementsByClassName("above-game")[0];
    const btnTemplate = document.getElementsByClassName("restart-button")[0];
    var speed = 10
    var speedIndex = 0
    aboveBox.appendChild(btnTemplate.cloneNode(false));
    aboveBox.appendChild(btnTemplate.cloneNode(false));
    aboveBox.appendChild(btnTemplate.cloneNode(false));
    //
    const btns = document.getElementsByClassName("restart-button");
    //
    var btnStartAuto = btns[1]
    const startText = document.createTextNode("Start Auto");
    btnStartAuto.appendChild(startText)
    //
    var btnStopAuto = btns[2]
    const stopText = document.createTextNode("Stop Auto");
    btnStopAuto.appendChild(stopText)
    //
    var btnSpeedAuto = btns[3]
    const speedText = document.createTextNode("Change Speed");
    btnSpeedAuto.appendChild(speedText)
    //
    var moveOn = false
    var moveCnt = 0
    var moveOneKey = function () {
        if(!moveOn){
            console.log("Cancelled.")
            return
        }
        var eventUp = new KeyboardEvent('keydown', {
            key: "w",
            keyCode: 87,
            which: 87,
            code: "KeyW",
            location: 0,
            description: "w"
        })
        var eventLeft = new KeyboardEvent('keydown' , {
            key: "a",
            keyCode: 65,
            which: 65,
            code: "KeyA",
            location: 0,
            description: "a"
        })
        var eventRight = new KeyboardEvent('keydown' , {
            key: "d",
            keyCode: 68,
            which: 68,
            code: "KeyD",
            location: 0,
            description: "d"
        })
        var eventDown = new KeyboardEvent('keydown' , {
            key: "s",
            keyCode: 83,
            which: 83,
            code: "KeyS",
            location: 0,
            description: "s"
        })
        document.tagName = "not input"
        var num = Math.random()
        var dispatchee = null
        moveCnt += 1
        const rate = 1 - Math.min(24 / moveCnt, 0.8)
        const downl = 0.01 + 0.02 * rate
        const rightl = downl + 0.01 + 0.02 * rate;
        const leftl = rightl + 0.50 + 0.10 * (1 - rate)
        if (num < downl) {
            dispatchee = eventDown;
            console.log("down");
        }else if(num < rightl){
            dispatchee = eventRight;
            console.log("right");
        }else if(num < leftl){
            dispatchee = eventLeft;
            console.log("left");
        }else{
            dispatchee = eventUp;
            console.log("up");
        }
        document.dispatchEvent(dispatchee)
    }

    // https://zeit.co/blog/async-and-await
    function sleep (time) {
        return new Promise((resolve) => setTimeout(resolve, time));
    }

    const startListener = async function () {
        console.log("start")
        moveOn = true;
        while (moveOn) {
            await sleep(70 * speed).then(moveOneKey)
            await sleep(10 * speed).then(() => {
                //const elements = document.getElementsByClassName("game-message game-over")
                // moveOn |= elements[0] == null;
            })
        }
    };

    const stopListener = function () {
        moveOn = false;
        moveCnt = 0
    }
    const speedListener = function () {
        var speeds = [10, 5, 1]
        var words = ["switch to low speed", "switch to fast speed", "switch to super fast speed"]
        const len = 3
        speedIndex = (speedIndex + 1) % len
        speed = speeds[speedIndex]
        console.log(words[speedIndex])
    }

    btnStartAuto.addEventListener('click', startListener)
    btnStopAuto.addEventListener('click', stopListener)
    btnSpeedAuto.addEventListener('click', speedListener)

})();