-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrexBot-collect-means.js
56 lines (45 loc) · 1.38 KB
/
trexBot-collect-means.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
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function calcMean(myArray) {
var sum = 0;
myArray.forEach(function(element) {
sum = sum + element;
});
return sum / myArray.length;
}
function calcVariance(myArray) {
var sum = 0;
var avg = calcMean(myArray);
myArray.forEach(function(element) {
var term = element - avg;
sum = sum + (term * term);
});
return sum / (myArray.length - 1);
}
function print() {
console.log("speed: ", calcMean(speed), Math.sqrt(calcVariance(speed)));
console.log("yPos: ", calcMean(yPos), Math.sqrt(calcVariance(yPos)));
console.log("obstacleXPos: ", calcMean(obstacleXPos), Math.sqrt(calcVariance(obstacleXPos)));
}
var fps = 15;
var speed = [];
var yPos = [];
var obstacleXPos = [];
async function algorithm() {
while(true) {
await sleep(1000 / fps);
if (Runner.instance_.playing === false && Runner.instance_.crashed === false) {
continue; // don't start yet, window is not active
}
if (Runner.instance_.crashed === true) {
continue;
}
speed.push(Runner.instance_.currentSpeed);
yPos.push(Runner.instance_.tRex.yPos);
if (Runner.instance_.horizon.obstacles.length > 0) {
obstacleXPos.push(Runner.instance_.horizon.obstacles[0].xPos);
}
}
}
algorithm();