Lie back and watch dumb bot play 2048!
当前为
// ==UserScript==
// @name 2048bot
// @namespace http://tampermonkey.net/
// @version 0.9
// @description Lie back and watch dumb bot play 2048!
// @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
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 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
if (num < 0.05) {
dispatchee = eventDown;
console.log("down");
}else if(num < 0.10){
dispatchee = eventRight;
console.log("right");
}else if(num < 0.55){
dispatchee = eventLeft;
console.log("left");
}else{
dispatchee = eventUp;
console.log("up");
}
document.dispatchEvent(dispatchee)
}
//thanks to 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;
}
const speedListener = function () {
if (speed == 1){
console.log("switch to low speed")
speed = 10
}else{
console.log("switch to fast speed")
speed = 1
}
}
btnStartAuto.addEventListener('click', startListener)
btnStopAuto.addEventListener('click', stopListener)
btnSpeedAuto.addEventListener('click', speedListener)
})();