-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuperSnake.pde
More file actions
400 lines (316 loc) · 12.2 KB
/
SuperSnake.pde
File metadata and controls
400 lines (316 loc) · 12.2 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
//a supersnake which is fusion of 5 best snakes
class SuperSnake {
int len = 1;//the length of the snake
PVector pos;//position of the head of the snake
ArrayList<PVector> tailPositions; //all the positions of the tail of the snake
PVector vel;//the velocity of the snake i.e. direction it will move next
PVector temp; //just a temporary PVector which gets used a bunch
Food food;//the food that this snake needs to eat
float[] vision = new float[24]; //the inputs for the neural net
float[] decision; // the output of the neural net
int lifetime = 0;//how long the snake lived for
long fitness = 0;//the quality of this snake
int leftToLive = 500; //the number of moves left to live if this gets down to 0 the snake dies
//this is to prevent the snakes doing circles forever
int growCount = 0; //the amount the snake still needs to grow
boolean alive = true; //true if the snake is alive
NeuralNet[] brain; // the array of neural nets controlling the snake
SuperSnakeClone[] clones;
int brainToFollow = 0; //which brain to follow for the next few moves
boolean foodFound = false;//true if a clone has found the food safely
boolean sawFood = false; //true if a clone has seen the food
int movesToFollow = 0;//how many moves to follow that snake
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//constructor
SuperSnake(NeuralNet[] demBrains) {
//set intial position of head and add 3 tail positions
int x = 600;
int y = 200;
pos = new PVector(x, y);
vel = new PVector(10, 0);
tailPositions = new ArrayList<PVector>();
temp = new PVector(x-30, y);
tailPositions.add(temp);
temp = new PVector(x-20, y);
tailPositions.add(temp);
temp = new PVector(x-10, y);
tailPositions.add(temp);
len+=3;
food = new Food();
brain = demBrains;
clones = new SuperSnakeClone[demBrains.length];
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//calculates the best direction to go based on the foresight from all of its brains
void bestDecision() {
//for each brain create a clone and run it until it dies or is trapped or finds food
for (int i = 0; i < brain.length; i++) {
clones[i] = new SuperSnakeClone(this, brain[i]);
clones[i].runClone();
}
//if any clones found the food find the one which does it in the least number of moves
int min = 1000;
int cloneAteFood = -1;
for (int i = 0; i < brain.length; i++) {
if (clones[i].foodFound) {
if (clones[i].moveCount < min) {
min = clones[i].foodSeenAtCount;
cloneAteFood = i;
}
}
}
//if any of the clones ate the food
if (cloneAteFood != -1) {
//set this clone as the brain to follow
foodFound = true;
movesToFollow = clones[cloneAteFood].moveCount;
brainToFollow = cloneAteFood;
return;//we are done
}
//if you get to this point then none of the snakes found the food so lets test for the next best
//has any seen the food
min = 1000;
int cloneSeenFood = -1;
for (int i = 0; i < brain.length; i++) {
if (clones[i].seenFood) {
if (clones[i].foodSeenAtCount < min) {
min = clones[i].foodSeenAtCount;
cloneSeenFood = i;
}
}
}
//if any of the clones saw the food
if (cloneSeenFood != -1) {
sawFood = true;
movesToFollow = clones[cloneSeenFood].foodSeenAtCount;
brainToFollow = cloneSeenFood;
return;
}
//if you get to this point then no snake found the food nor did they see the food, damn
//follow the snake which made it the furthest without running out of moves (because they would probably be looping)
int max = 0;
int cloneLastedLongest = 0;
for (int i = 0; i < brain.length; i++) {
if (!clones[i].ranOut) {
if (clones[i].moveCount > max) {
max = clones[i].moveCount;
cloneLastedLongest = i;
}
}
}
brainToFollow = cloneLastedLongest;
movesToFollow = 1;
return;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//from an output array returns an int indicating the direction the snake should go
int getDirection(float[] netOutputs) {
float max = 0;
int maxIndex = 0;
for (int i = 0; i < netOutputs.length; i++) {
if (max < netOutputs[i]) {
max = netOutputs[i];
maxIndex = i;
}
}
return maxIndex;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
void setVelocity() {
look();
//if no more moves to follow then create clones to 'imagine' where to go
if (movesToFollow <= 0) {
sawFood = false;
foodFound = false;
bestDecision();
}
//get the direction from the brain
int direction = getDirection(brain[brainToFollow].output(vision));
//set the velocity based on this decision
switch(direction) {
case 0://left
vel = new PVector(-10, 0);
break;
case 1://up
vel = new PVector(0, -10);
break;
case 2://right
vel = new PVector(10, 0);
break;
case 3://down
vel = new PVector(0, 10);
break;
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//move the snake in direction of the vel PVector
void move() {
lifetime+=1;
leftToLive -=1;
movesToFollow -=1;
//if time left to live is up then kill the snake
if (leftToLive < 0) {
alive = false;
println("fuck1");
}
//if the snake hit itself or the edge then kill it
if (gonnaDie(pos.x + vel.x, pos.y + vel.y)) {
alive= false;
println("fuck2");
}
//if the snake is on the same position as the food then eat it
if (pos.x + vel.x == food.pos.x && pos.y + vel.y == food.pos.y) {
eat();
}
//snake grows 1 square at a time so if the snake has recently eaten then grow count will be greater than 0
if (growCount > 0) {
growCount --;
grow();
} else {//not growing then move all the tail positions to follow the head
for (int i = 0; i< tailPositions.size() -1; i++) {
temp = new PVector(tailPositions.get(i+1).x, tailPositions.get(i+1).y);
tailPositions.set(i, temp);
}
if (len>1) {
temp = new PVector(pos.x, pos.y);
tailPositions.set(len-2, temp);
}
}
//actually move the head of the snake
pos.add(vel);
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//the snake just ate some food
void eat() {
//reset the food so its not on the tail
food = new Food();
while (tailPositions.contains(food.pos)) {
food = new Food();
}
//let the snake live longer
leftToLive += 100;
growCount +=4;//the snake grows by 4
movesToFollow =0;//make sure the bestDecision() function is called before the next move
foodFound = false;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//show the super snake
void show() {
fill(255);
stroke(0);
//if a clone has found the food then show the snake as blue
if(foodFound){
fill(0,255,0);
}else if(sawFood){
fill(0,0,255);
}
for (int i = 0; i< tailPositions.size(); i++) {
rect(tailPositions.get(i).x, tailPositions.get(i).y, 10, 10);
}
rect(pos.x, pos.y, 10, 10);
food.show();
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//grows the snake by 1 square
void grow() {
//add the head to the tail list this simulates the snake growing as the head is the only thing which moves
temp = new PVector(pos.x, pos.y);
tailPositions.add(temp);
len +=1;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//returns true if the snake is going to hit itself or a wall
boolean gonnaDie(float x, float y) {
//check if hit wall
if (x < 400 || y < 0 || x >= 800 || y >= 400) {
return true;
}
//check if hit tail
return isOnTail(x, y);
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//returns true if the coordinates is on the snakes tail
boolean isOnTail(float x, float y) {
for (int i = 0; i < tailPositions.size(); i++) {
if (x == tailPositions.get(i).x && y == tailPositions.get(i).y) {
return true;
}
}
return false;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//looks in 8 directions to find food,walls and its tail
void look() {
vision = new float[24];
//look left
float[] tempValues = lookInDirection(new PVector(-10, 0));
vision[0] = tempValues[0];
vision[1] = tempValues[1];
vision[2] = tempValues[2];
//look left/up
tempValues = lookInDirection(new PVector(-10, -10));
vision[3] = tempValues[0];
vision[4] = tempValues[1];
vision[5] = tempValues[2];
//look up
tempValues = lookInDirection(new PVector(0, -10));
vision[6] = tempValues[0];
vision[7] = tempValues[1];
vision[8] = tempValues[2];
//look up/right
tempValues = lookInDirection(new PVector(10, -10));
vision[9] = tempValues[0];
vision[10] = tempValues[1];
vision[11] = tempValues[2];
//look right
tempValues = lookInDirection(new PVector(10, 0));
vision[12] = tempValues[0];
vision[13] = tempValues[1];
vision[14] = tempValues[2];
//look right/down
tempValues = lookInDirection(new PVector(10, 10));
vision[15] = tempValues[0];
vision[16] = tempValues[1];
vision[17] = tempValues[2];
//look down
tempValues = lookInDirection(new PVector(0, 10));
vision[18] = tempValues[0];
vision[19] = tempValues[1];
vision[20] = tempValues[2];
//look down/left
tempValues = lookInDirection(new PVector(-10, 10));
vision[21] = tempValues[0];
vision[22] = tempValues[1];
vision[23] = tempValues[2];
}
float[] lookInDirection(PVector direction) {
//set up a temp array to hold the values that are going to be passed to the main vision array
float[] visionInDirection = new float[3];
PVector position = new PVector(pos.x, pos.y);//the position where we are currently looking for food or tail or wall
boolean foodIsFound = false;//true if the food has been located in the direction looked
boolean tailIsFound = false;//true if the tail has been located in the direction looked
float distance = 0;
//move once in the desired direction before starting
position.add(direction);
distance +=1;
//look in the direction until you reach a wall
while (!(position.x < 400 || position.y < 0 || position.x >= 800 || position.y >= 400)) {
//check for food at the position
if (!foodIsFound && position.x == food.pos.x && position.y == food.pos.y) {
visionInDirection[0] = 1;
foodIsFound = true; // dont check if food is already found
}
//check for tail at the position
if (!tailIsFound && isOnTail(position.x, position.y)) {
visionInDirection[1] = 1/distance;
tailIsFound = true; // dont check if tail is already found
}
//look further in the direction
position.add(direction);
distance +=1;
}
//set the distance to the wall
visionInDirection[2] = 1/distance;
return visionInDirection;
}
}