-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomBot.js
77 lines (69 loc) · 1.94 KB
/
randomBot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// config
const fps = 30;
// helpers
var randomNumber = function(min, max) {
return Math.floor((Math.random() * (max-min+1)) + min);
};
const KEY = {
DOWN: 40,
UP: 38
};
var triggerEvent = function(type, keyCode) {
var e = document.createEvent('HTMLEvents');
e.keyCode = keyCode;
e.initEvent(type, false, true);
document.dispatchEvent(e);
};
// key events (actions)
var duck = function() {
// console.info(">duck");
if (Runner.instance_.tRex.status === "DUCKING") {
triggerEvent('keyup', KEY.DOWN);
} else {
triggerEvent('keydown', KEY.DOWN);
}
};
var jump = function() {
// console.info(">jump");
if (Runner.instance_.tRex.status === "DUCKING") {
return;
}
triggerEvent('keydown', KEY.UP);
triggerEvent('keyup', KEY.UP);
};
var noop = function() {
// console.info(">noop");
return;
};
var actions = [duck, jump, noop];
var restart = function() {
Runner.instance_.restart();
Runner.instance_.tRex.xPos = 24
};
var iteration = 1;
var scores = [];
var runBot = function() {
if (Runner.instance_.tRex.status === "WAITING") {
return;
}
if (Runner.instance_.tRex.status === "CRASHED") {
var score = Runner.instance_.distanceRan / 40;
scores.push(score);
console.log("Iteration #", iteration, ": ", score);
++iteration;
if (iteration <= 100) {
restart();
} else {
window.clearInterval(intervalID);
}
return;
}
// read environment
// console.info(Runner.instance_.tRex.status, Runner.instance_.tRex.xPos, Runner.instance_.tRex.yPos);
// Runner.instance_.horizon.obstacles.forEach(function(obstacle) {
// console.info(obstacle.typeConfig.type, obstacle.xPos, obstacle.yPos, obstacle.width);
// });
// do action based on environment
actions[randomNumber(0, actions.length-1)]();
};
var intervalID = window.setInterval(runBot, 1000 / fps);