-
Notifications
You must be signed in to change notification settings - Fork 15
/
bot.js
99 lines (82 loc) · 1.72 KB
/
bot.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
class Bot{
constructor(brain) {
this.r = 32;
this.y = height - this.r/2;
this.x = 64;
this.gravity = 0.9;
this.vy = 0;
this.vx = 0;
this.ax = 0;
this.ay = 0;
this.score = 0;
this.fitness = 0;
if (brain){
this.brain = brain.copy();
}
else{
this.brain = new NeuralNetwork(6, 8, 2);
}
}
dispose(){
this.brain.dispose();
}
show(){
stroke(81, 219, 146);
fill(81, 219, 146, 100);
ellipse(this.x, this.y, this.r, this.r);
}
mutate() {
this.brain.mutate(0.1);
}
think(bars) {
let closestBar = null;
let closestD = Infinity;
for (let i = 0; i < bars.length; i++) {
let d = bars[i].x + bars[i].w - this.x;
if (d < closestD && d > 0) {
closestBar = bars[i];
closestD = d;
}
}
let posBar = 0
let heiBar = 0
if (closestBar){
if(closestBar.x<=width){
posBar = closestBar.x;
heiBar = closestBar.top;
}
}
if(this.y == height - this.r/2){
let inputs = [];
inputs[0] = this.vx / 10;
inputs[1] = this.vy / 10;
inputs[2] = this.x/width;
inputs[3] = this.y/height;
inputs[4] = posBar/ width;
inputs[5] = heiBar/ height;
let output = this.brain.predict(inputs);
this.ax = output[0];
this.ay = output[1];
}
else{
this.ax = 0;
this.ay = 0;
}
}
offScreen(){
if(this.y < 0) return true;
if(this.x > width || this.x < 0) return true;
return false;
}
hitGround(){
if(this.y + this.r/2>=height) return true;
return false;
}
update(){
this.score++;
this.vy = this.vy+this.gravity-this.ay;
this.y+=this.vy;
this.vx+=this.ax;
this.x+=this.vx;
}
}