-
Notifications
You must be signed in to change notification settings - Fork 0
/
geneticCars3.pde
98 lines (81 loc) · 1.88 KB
/
geneticCars3.pde
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
ArrayList<Car> pop;
GenAlg ga;
int genCounter;
ArrayList<PVector> lines;
PVector lastClick;
boolean started;
boolean isOver;
void setup(){
size(400, 400);
pop = new ArrayList();
ga = new GenAlg();
pop = ga.genPop(pop);
started = false;
getLines();
}
void draw(){
background(255);
show();
if(isOver){
pop = ga.genPop(pop);
genCounter++;
//for(Car c: pop){
// c.index = 0;
// c.setIsDone(false);
//}
isOver = false;
print("Generation #"+genCounter + " --- ");
}
}
void show(){
//println("show()");
if(started){
if(isOver){
return;
}
//println("Playing");
isOver = true;
for(Car c: pop){
c.move();
if(!c.getIsDone()){
isOver = false;
}
}
}
drawMapObjects();
}
void getLines(){
println("Getlines()");
lines = new ArrayList();
lastClick = null;
}
void drawMapObjects(){
if(lastClick != null && !started){
line(lastClick.x, lastClick.y, mouseX, mouseY);
}
//Obstacles
for(int i = 0; i< lines.size(); i = i+2){
line(lines.get(i).x, lines.get(i).y, lines.get(i+1).x, lines.get(i+1).y);
}
//Target
ellipse(Constants.target.x, Constants.target.y, 12, 12);
//Start point
ellipse(Constants.start.x, Constants.start.y, 10, 10);
}
void mousePressed(){
println("mousePressed()");
if(lastClick != null){
lines.add(lastClick);
lines.add(new PVector(mouseX, mouseY));
lastClick = null;
}
else{
lastClick = new PVector(mouseX, mouseY);
}
}
void keyPressed(){
println("keyPressed()");
if(keyCode == ENTER){
started = true;
}
}