IA_PathFinder/Brain.pde

48 lines
955 B
Plaintext
Raw Normal View History

2022-05-07 12:08:13 +00:00
class Brain{
PVector[] directions;
int step=0;
//Constructor
Brain(int size){
directions=new PVector[size];
//random fill
for(int i=0; i<directions.length; i++){
float randomAngle=random(2*PI);
directions[i]=PVector.fromAngle(randomAngle);
}
}
//Clone brain
Brain clone(){
Brain cpy=new Brain(directions.length);
for(int i=0; i<directions.length; i++){
cpy.directions[i]=directions[i].copy();
}
return cpy;
}
//Mutate brain
void mutate(){
float mutateRate=0.01;
for(int i=0; i<directions.length; i++){
if(random(1)<mutateRate){
float randomAngle=random(2*PI);
directions[i]=PVector.fromAngle(randomAngle);
}
}
}
//get next step
PVector next(){
if(step<directions.length){
step++;
return directions[step-1];
}
else{
return null;
}
}
}