-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake.pde
More file actions
353 lines (299 loc) · 11.9 KB
/
Snake.pde
File metadata and controls
353 lines (299 loc) · 11.9 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
class Snake {
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
int lifetime = 0;//how long the snake lived for
long fitness = 0;//the quality of this snake
int leftToLive = 200; //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
boolean test = false;//true if the snake is being tested not trained
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//constructor
Snake() {
//set initial position of head and add 3 tail positions since the starting length is 4
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;
//intiate the food
food = new Food();
brain = new NeuralNet(24, 18, 4);//create a neural net with 24 input neurons 18 hidden neurons and 4 output neurons
leftToLive = 200;
}
//mutates neural net
void mutate(float mr) {
brain.mutate(mr);
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//set the velocity from the output of the neural network
void setVelocity() {
//get the output of the neural network
decision = brain.output(vision);
//get the maximum position in the output array and use this as the decision on which direction to go
float max = 0;
int maxIndex =0;
for (int i = 0; i < decision.length; i++) {
if (max < decision[i]) {
max = decision[i];
maxIndex = i;
}
}
//set the velocity based on this decision
if (maxIndex == 0) {
//if (vel.x!=10 && vel.y !=0) { //this is to stop the snake from going back into its own body but i removed it to teach the snakes to avoid their bodies
vel.x =-10;
vel.y =0;
//}
} else if (maxIndex == 1) {
//if (vel.x!=0 && vel.y !=10) {
vel.x =0;
vel.y =-10;
//}
} else if (maxIndex == 2) {
//if (vel.x!=-10 && vel.y !=0) {
vel.x =10;
vel.y =0;
//}
} else {
//if (vel.x!=0 && vel.y !=-10) {
vel.x =0;
vel.y =10;
//}
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//move the snake in direction of the vel PVector
void move() {
//move through time
lifetime+=1;
leftToLive -=1;
//if time left to live is up then kill the snake
if (leftToLive < 0) {
alive = false;
}
//if the snake hit itself or the edge then kill it
if (gonnaDie(pos.x + vel.x, pos.y + vel.y)) {
alive= false;
}
//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);
}
temp = new PVector(pos.x, pos.y);
tailPositions.set(len-2, temp);
}
//actually move the snakes head
pos.add(vel);
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//the snake just ate some food
void eat() {
//reset food to a new position
food = new Food();
while (tailPositions.contains(food.pos)) { //make sure the food isnt on the snake
food = new Food();
}
//increase time left to live
leftToLive += 100;
//if testing then grow by 4 but if not and the snake is still small only grow by 1
//this is for helping snakes evolving so they dont get too big too soon
if (test||len>=10) {
growCount += 4;
} else {
growCount += 1;
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//display the snake
void show() {
fill(255);
stroke(0);
//show the tail
for (int i = 0; i< tailPositions.size(); i++) {
rect(tailPositions.get(i).x, tailPositions.get(i).y, 10, 10);
}
//show the head
rect(pos.x, pos.y, 10, 10);
//show the food
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;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//calculate the fitness of the snake after it has died
void calcFitness() {
//fitness is based on length and lifetime
if (len < 10) {
fitness = floor(lifetime *lifetime * pow(2, (floor(len))));
} else {
//grows slower after 10 to stop fitness from getting stupidly big
//ensure greater than len = 9
fitness = lifetime * lifetime;
fitness *= pow(2, 10);
fitness *=(len-9);
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//crossover function for genetic algorithm
Snake crossover(Snake partner) {
Snake child = new Snake();
child.brain = brain.crossover(partner.brain);
return child;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//returns a clone of the snake
Snake clone() {
Snake clone = new Snake();
clone.brain = brain.clone();
clone.alive = true;
return clone;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//saves the snake to a file by converting it to a table
void saveSnake(int snakeNo, int score, int popID) {
//save the snakes top score and its population id
Table snakeStats = new Table();
snakeStats.addColumn("Top Score");
snakeStats.addColumn("PopulationID");
TableRow tr = snakeStats.addRow();
tr.setFloat(0, score);
tr.setInt(1, popID);
saveTable(snakeStats, "data/SnakeStats" + snakeNo+ ".csv");
//save snakes brain
saveTable(brain.NetToTable(), "data/Snake" + snakeNo+ ".csv");
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//return the snake saved in the parameter position
Snake loadSnake(int snakeNo) {
Snake load = new Snake();
Table t = loadTable("data/Snake" + snakeNo + ".csv");
load.brain.TableToNet(t);
return load;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//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;
}
}