try to take over the world!
当前为
// ==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)
})();