-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmartSnakesCombine.pde
More file actions
161 lines (138 loc) · 4.47 KB
/
SmartSnakesCombine.pde
File metadata and controls
161 lines (138 loc) · 4.47 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
//GLOBAL VARIABLES
import java.io.File;
World world;//world which stores the species / populations
World worldOfLegends;//world containing the legends, used for training the legends to be even better
int speed = 30;//the frame rate
//booleans used to control the game state
boolean showAll = true;//whether to show all the snakes in the generation or just the current best snake
boolean trainLegendSnakes = false; //true if training the legends i.e. if running worldOfLegends
boolean showingLegend = false;//true if testing just one legend
boolean fusionGo =false;//true if testing the snake fusion
float globalMutationRate = 0.01;
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//run on startup
void setup() {
frameRate(speed);
size(800, 500);
world = new World(5, 2000);
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
void draw() {
background(40);
drawData();
//training/evolving the legend snakes
if (trainLegendSnakes) {
if (!worldOfLegends.done()) {
worldOfLegends.updateAlive();
} else {
//all are dead
worldOfLegends.geneticAlgorithm();
}
//testing a single legend
} else if (showingLegend) {
if (world.legend.alive) {
world.updateLegend();
} else {
if(world.legend.len < 100){
world.playLegend(world.legendNo);
}else{
showingLegend = false;
}
}
// testing the supersnake fusion
} else if (fusionGo) {
if (world.fusedSnake.alive) {
world.updateSuperSnake();
} else { //once done set the fusionGo to false
fusionGo = false;
}
//training/evolving population normally
} else {
if (!world.done()) {//if there is still a snake alive then update them
world.updateAlive();
} else {//if all are dead :(
world.geneticAlgorithm();
}
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
void keyPressed() {
switch(key) {
case ' '://toggle show all
showAll = !showAll;
break;
case '+'://speed up frame rate
speed += 10;
frameRate(speed);
break;
case '-'://slow down frame rate
if (speed > 10) {
speed -= 10;
frameRate(speed);
}
break;
case 'f'://create a fused snake from the legends
fusionGo = true;
world.snakeFusion();
break;
case '0'://test legend 0
showingLegend = true;
world.playLegend(0);
break;
case '1': // test legend no 1
world.playLegend(1);
showingLegend = true;
break;
case '2'://test legend no 2
world.playLegend(2);
showingLegend = true;
break;
case '3'://test legend no 3
world.playLegend(3);
showingLegend = true;
break;
case '4'://test legend no 4
world.playLegend(4);
showingLegend = true;
break;
case 'h'://halve the mutation rate
globalMutationRate /=2;
break;
case 'd'://double the mutation rate
globalMutationRate *= 2;
break;
case 'l'://train the legends
trainLegendSnakes =!trainLegendSnakes;
if (trainLegendSnakes == true) {//load best snakes from file and setup worldOfLegends
world.loadBestSnakes();
worldOfLegends = new World(1000, world.SnakesOfLegend);
}
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
void drawData() {
fill(255);
stroke(255);
line(400, 0, 400, 400);
line(0, 400, 800, 400);
textSize(30);
//training/evolving the legend snakes
if (trainLegendSnakes) {
text("Generation: " + (worldOfLegends.gen), 10, 100);
text("Speed: " + speed, 10, 150);
text("Global Best: " + (worldOfLegends.worldBestScore), 10, 200);
text("mutation Rate: " + globalMutationRate, 10, 250);
//testing a single legend
} else if (showingLegend) {
text("Score: " + (world.legend.len-4), 10, 100);
// testing the supersnake fusion
} else if (fusionGo) {
text("Score: " + (world.fusedSnake.len-4), 10, 100);
//training/evolving population normally
} else {
text("Generation: " + (world.gen), 10, 100);
text("Speed: " + speed, 10, 150);
text("Global Best: " + (world.worldBestScore), 10, 200);
text("mutation Rate: " + globalMutationRate, 10, 250);
}
}