sheep can reproduce

This commit is contained in:
Guillaume BONABAU 2024-05-29 17:07:44 +02:00
parent e157d8e991
commit b12c2611de
1 changed files with 14 additions and 1 deletions

View File

@ -39,10 +39,14 @@ public class Sheep extends Agent {
} }
this.moveRandom(world); this.moveRandom(world);
return hunger<10; //condition to be alive return hunger<10; //condition to be alive
//if the sheep can reproduce it creates a new sheep in agents
if(canReproduce(neighbors, world)) {
world.getArrayAgent().add(new Sheep(x, y));
}
} }
private void moveRandom(Simulator world) { private void moveRandom(Simulator world) {
//check is looping border is activated //check if looping border is activated
if(world.isLoopingBorder()) { if(world.isLoopingBorder()) {
//if looping border is activated we can move in any direction //if looping border is activated we can move in any direction
x = (x+rand.nextInt(3)-1+world.getWidth())%world.getWidth(); x = (x+rand.nextInt(3)-1+world.getWidth())%world.getWidth();
@ -53,4 +57,13 @@ public class Sheep extends Agent {
y = Math.max(0, Math.min(world.getHeight()-1, y+rand.nextInt(3)-1)); y = Math.max(0, Math.min(world.getHeight()-1, y+rand.nextInt(3)-1));
} }
} }
//sheep reproduce if two are on the same cell
public boolean canReproduce(ArrayList<Agent> neighbors, Simulator world) {
for(Agent a : neighbors) {
if(a instanceof Sheep && a.getX()==x && a.getY()==y) {
return true;
}
}
return false;
}
} }