-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.js
More file actions
215 lines (180 loc) · 6.25 KB
/
snake.js
File metadata and controls
215 lines (180 loc) · 6.25 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
const GRID_SIZE = 20;
const ROWS = 30; // 600 / 20
const COLS = 30;
class Snake {
constructor(brain) {
this.body = [
{ x: Math.floor(COLS / 2), y: Math.floor(ROWS / 2) },
{ x: Math.floor(COLS / 2), y: Math.floor(ROWS / 2) + 1 },
{ x: Math.floor(COLS / 2), y: Math.floor(ROWS / 2) + 2 }
];
this.dir = { x: 0, y: -1 }; // Start moving up
this.score = 0;
this.lifetime = 0;
this.timeLeft = 150; // Frames until starvation
this.isDead = false;
this.fitness = 0;
if (brain) {
this.brain = brain.copy();
} else {
// 24 inputs, window.hiddenLayerNodes hidden, 4 outputs
this.brain = new NeuralNetwork(24, window.hiddenLayerNodes || 16, 4);
}
// Random color, but with good saturation and lightness to be visible
let hue = Math.floor(Math.random() * 360);
this.color = `hsl(${hue}, 80%, 50%)`;
this.colorTransparent = `hsla(${hue}, 80%, 50%, 0.3)`;
this.spawnApple();
}
spawnApple() {
let valid = false;
while (!valid) {
this.apple = {
x: Math.floor(Math.random() * COLS),
y: Math.floor(Math.random() * ROWS)
};
valid = true;
for (let part of this.body) {
if (part.x === this.apple.x && part.y === this.apple.y) {
valid = false;
break;
}
}
}
}
lookDirection(dx, dy) {
let look = [0, 0, 0]; // [Wall, Apple, Body]
let currX = this.body[0].x;
let currY = this.body[0].y;
let distance = 0;
let foundApple = false;
let foundBody = false;
currX += dx;
currY += dy;
distance++;
while (currX >= 0 && currX < COLS && currY >= 0 && currY < ROWS) {
if (!foundApple && currX === this.apple.x && currY === this.apple.y) {
look[1] = 1; // 1 apple is in this direction
foundApple = true;
}
if (!foundBody) {
for (let i = 1; i < this.body.length; i++) {
if (currX === this.body[i].x && currY === this.body[i].y) {
look[2] = 1 / distance;
foundBody = true;
break;
}
}
}
currX += dx;
currY += dy;
distance++;
}
look[0] = 1 / distance; // Distance to wall
return look;
}
getInputs() {
let inputs = [];
// 8 directions
let dirs = [
{ x: 0, y: -1 }, { x: 1, y: -1 }, { x: 1, y: 0 }, { x: 1, y: 1 },
{ x: 0, y: 1 }, { x: -1, y: 1 }, { x: -1, y: 0 }, { x: -1, y: -1 }
];
for (let d of dirs) {
let look = this.lookDirection(d.x, d.y);
inputs.push(look[0]);
inputs.push(look[1]);
inputs.push(look[2]);
}
return inputs;
}
think() {
let inputs = this.getInputs();
let outputs = this.brain.predict(inputs);
// Find max output
let maxIndex = 0;
let maxVal = outputs[0];
for (let i = 1; i < outputs.length; i++) {
if (outputs[i] > maxVal) {
maxVal = outputs[i];
maxIndex = i;
}
}
// 0: Up, 1: Right, 2: Down, 3: Left
if (maxIndex === 0 && this.dir.y !== 1) this.dir = { x: 0, y: -1 };
else if (maxIndex === 1 && this.dir.x !== -1) this.dir = { x: 1, y: 0 };
else if (maxIndex === 2 && this.dir.y !== -1) this.dir = { x: 0, y: 1 };
else if (maxIndex === 3 && this.dir.x !== 1) this.dir = { x: -1, y: 0 };
}
update() {
if (this.isDead) return;
this.lifetime++;
this.timeLeft--;
if (this.timeLeft <= 0) {
this.isDead = true;
return;
}
let head = { x: this.body[0].x + this.dir.x, y: this.body[0].y + this.dir.y };
// Check Wall Collision
if (head.x < 0 || head.x >= COLS || head.y < 0 || head.y >= ROWS) {
this.isDead = true;
return;
}
// Check Body Collision
for (let i = 0; i < this.body.length - 1; i++) {
if (head.x === this.body[i].x && head.y === this.body[i].y) {
this.isDead = true;
return;
}
}
this.body.unshift(head);
// Check Apple
if (head.x === this.apple.x && head.y === this.apple.y) {
this.score++;
this.timeLeft += 100; // Bonus time for eating
if (this.timeLeft > 500) this.timeLeft = 500; // Cap
this.spawnApple();
} else {
this.body.pop();
}
}
calculateFitness() {
if (this.score === 0) {
let d = Math.abs(this.body[0].x - this.apple.x) + Math.abs(this.body[0].y - this.apple.y);
this.fitness = this.lifetime + (100 / (d + 1));
} else {
this.fitness = (this.score * this.score * 5000) + this.lifetime;
}
return this.fitness;
}
crossover(partner) {
let childBrain = this.brain.crossover(partner.brain, window.crossoverType || 'weight');
return new Snake(childBrain);
}
mutate(rate) {
this.brain.mutate(rate);
}
show(ctx, isBest) {
// Draw Apple
if (isBest) {
ctx.fillStyle = this.color;
} else {
ctx.fillStyle = this.colorTransparent;
}
// Make apple slightly smaller to differentiate from body
ctx.fillRect(this.apple.x * GRID_SIZE + 4, this.apple.y * GRID_SIZE + 4, GRID_SIZE - 8, GRID_SIZE - 8);
// Draw Snake
if (isBest) {
ctx.fillStyle = this.color; // Opaque for best
ctx.shadowBlur = 10;
ctx.shadowColor = this.color;
} else {
ctx.fillStyle = this.colorTransparent;
ctx.shadowBlur = 0;
}
for (let i = 0; i < this.body.length; i++) {
ctx.fillRect(this.body[i].x * GRID_SIZE + 1, this.body[i].y * GRID_SIZE + 1, GRID_SIZE - 2, GRID_SIZE - 2);
}
ctx.shadowBlur = 0; // reset
}
}