-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuperSnakeClone.pde
More file actions
377 lines (324 loc) · 13.5 KB
/
SuperSnakeClone.pde
File metadata and controls
377 lines (324 loc) · 13.5 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
//a clone or thought which is used to plan ahead
class SuperSnakeClone {
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
NeuralNet brain; // the neural net controlling the snake
float[] vision = new float[24]; //the inputs for the neural net
float[] decision; // the output of the neural net
ArrayList<PVector> blanks = new ArrayList<PVector>();//all the blank spaces that are enclosed by the snake used to tell if the snake is trapped
int leftToLive = 200; //the number of moves left ot live if this gets down to 0 the snake dies
//this is to prevent the snakes doing circles forever
int moveCount = 0; //the amount of moves the clone has done
boolean alive = true; //true if the snake is alive
boolean foodFound = false; // true if the snake found the food
boolean trapped = false;//true if the snake is trapped
boolean seenFood = false;//whether the snake saw the food
int foodSeenAtCount = 300;
boolean ranOut = false; //true if after 500 moves the clone hasnt died, gotten trapped or eaten the food, so its probably looping
int growCount = 0;
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//constructor
SuperSnakeClone(SuperSnake original, NeuralNet chosenBrain) {
//copy the position, tailPositions, length, brain, time to live and food from the original
pos = new PVector(original.pos.x, original.pos.y);
tailPositions = (ArrayList)original.tailPositions.clone();
len = original.len;
food = original.food.clone();
brain = chosenBrain.clone();
leftToLive = original.leftToLive;
growCount = original.growCount;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//runs the clone until it dies, finds the food is trapped or takes over 500 moves to do any of these things
void runClone() {
for (int i = 0; i< 500; i++) {
//update clone
look();
setVelocity();
move();
if (!alive || foodFound || trapped) {//if anything interesting happened then stop the clone
return;
}
}
ranOut = true; // the snake is probably in a loop
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//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;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//set the velocity from the output of the neural network
void setVelocity() {
//get the output of the neural network
int direction = getDirection(brain.output(vision));
//get the maximum position in the output array and use this as the decision on which direction to go
//set the velocity based on this decision
switch(direction) {
case 0:
vel = new PVector(-10, 0);
break;
case 1:
vel = new PVector(0, -10);
break;
case 2:
vel = new PVector(10, 0);
break;
case 3:
vel = new PVector(0, 10);
break;
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//move the snake in direction of the vel PVector
void move() {
//increment moveCount
moveCount+=1;
//move through time
leftToLive -=1;
//if time left to live is up then kill the snake
if (leftToLive < 0) {
alive = false;
return;
}
//if the snake hit itself or the edge then kill it
if (gonnaDie(pos.x + vel.x, pos.y + vel.y)) {
alive= false;
return;
}
//if the snake is trapped then set it as trapped and end the clone
if (isTrapped()) {
trapped = true;
return;
}
//if the snake is on the same position as the food then set it as found food and end the clone
//Note the snake cannot be trapped and find food so no need to test it
if (pos.x + vel.x == food.pos.x && pos.y + vel.y == food.pos.y) {
foodFound = true;
return;
}
//not growing then move all the tail positions to follow the head
//nice
if (growCount > 0) {
growCount --;
grow();
} else {
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 snakes head
pos.add(vel);
//if the clone can see the food and hasnt already seen the food (we want the shortest point to seeing the food) then set the number of moves it took to see it
if (!seenFood && seeFood()) {
seenFood = true;
foodSeenAtCount = moveCount;
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//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;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//is the food within sight (only 4 directions) cannot see through tail
boolean seeFood() {
//look in 4 directions for the food
PVector left = new PVector(pos.x-10, pos.y);
PVector right = new PVector(pos.x+10, pos.y);
PVector up = new PVector(pos.x, pos.y-10);
PVector down = new PVector(pos.x, pos.y+10);
//look left until found the wall the snakes body or the food
//while the left vector is not on the tail or out
while (!gonnaDie(left.x, left.y)) {
//if the left vector is on the food then the snake can see the food and thus return true
if (left.x == food.pos.x && left.y == food.pos.y) {
return true;
}
//look further left
left.x-=10;
}
//look right for food
while (!gonnaDie(right.x, right.y)) {
if (right.x == food.pos.x && right.y == food.pos.y) {
return true;
}
right.x+=10;
}
//look up for food
while (!gonnaDie(up.x, up.y)) {
if (up.x == food.pos.x && up.y == food.pos.y) {
return true;
}
up.y -=10;
}
//look down for food
while (!gonnaDie(down.x, down.y)) {
if (down.x == food.pos.x && down.y == food.pos.y) {
return true;
}
down.y+=10;
}
//if not seen in any of the 4 directions then return false
return false;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//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;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//this function return whether or not the snake is trapped, trapped meaning that the snake is trapped within its own tail
boolean isTrapped() {
//stores all the points within the tails 'trap'
blanks = new ArrayList<PVector>();
countNextTo(pos.x, pos.y);//call recursive function to add all the blanks which are reachable from the head of the snake
//if the amount of spaces is less than half the remaining positions(1600 - tailPositions.size()) or less than 300 whichever is less
//then it considered as trapped so return true
//otherwise return false
return (blanks.size() <= min((1600 - tailPositions.size())/2, 300));
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//adds all the blanks which are reachable from the head of the snake to the blanks ArrayList
//see isTrapped function above for more info
//recursively calls itself to find all blanks within the snakes tail
void countNextTo(float x, float y) {
//no need to add more positions to blank if already considered not trapped
if (blanks.size() <= min((1600 - tailPositions.size())/2, 300)) {
temp = new PVector(x+10, y);//the position to check if its blank
//if not out or on the tail then add it to the blank ArrayList and then look for other blanks around that position by calling this function again
if (!gonnaDie(temp.x, temp.y) && !blanks.contains(temp)) {
blanks.add(temp);
countNextTo(temp.x, temp.y);
}
//look to the left
temp = new PVector(x-10, y);
if (!gonnaDie(temp.x, temp.y) && !blanks.contains(temp)) {
blanks.add(temp);
countNextTo(temp.x, temp.y);
}
//look down
temp = new PVector(x, y+10);
if (!gonnaDie(temp.x, temp.y) && !blanks.contains(temp)) {
blanks.add(temp);
countNextTo(temp.x, temp.y);
}
//look up
temp = new PVector(x, y-10);
if (!gonnaDie(temp.x, temp.y) && !blanks.contains(temp)) {
blanks.add(temp);
countNextTo(temp.x, temp.y);
}
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//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;
}
}