Agents can live and die according to their liveturn method

This commit is contained in:
l.dupuis-burtin 2024-05-30 19:55:50 +02:00
parent 70bbd4fdc3
commit 3467b887ea
2 changed files with 23 additions and 16 deletions

View File

@ -32,11 +32,12 @@ public class Sheep extends Agent {
public boolean liveTurn(ArrayList<Agent> neighbors, Simulator world) {
if(world.getCell(x, y)==1) {
world.setCell(x, y, 0);
hunger-=2;
} else {
hunger++;
}
this.moveRandom();
return hunger>10;
return hunger<10;
}
private void moveRandom() {

View File

@ -196,18 +196,25 @@ public class Simulator extends Thread {
// only modify if sure of what you do
// to modify agent behavior, see liveTurn method
// in agent classes
ArrayList<Integer> arrayDeadAgent=new ArrayList<Integer>();
int counter=0;
for(Agent agent : agents) {
ArrayList<Agent> neighbors =
this.getNeighboringAnimals(
agent.getX(),
agent.getY(),
ANIMAL_AREA_RADIUS);
if(!agent.liveTurn(
neighbors,
this)) {
agents.remove(agent);
ArrayList<Agent> neighbors = this.getNeighboringAnimals(agent.getX(), agent.getY(), ANIMAL_AREA_RADIUS);
if(!agent.liveTurn(neighbors,this)) {
//agents.remove(agent);
arrayDeadAgent.add(counter);
}
counter++;
}
int counterTwo=0;
for(Integer i : arrayDeadAgent) {
if (i==arrayDeadAgent.get(0)) {
agents.remove(i.intValue());
}else{agents.remove(i.intValue()-counterTwo);}
counterTwo++;
}
//then evolution of the field
// TODO : apply game rule to all cells of the field
@ -288,14 +295,13 @@ public class Simulator extends Thread {
}else {setCell(x, y, 0);}
break;
case 2 :
if(getCell(x, y)==0) {
setCell(x, y, 1);
}else {setCell(x, y, 0);}
Sheep Shaun=new Sheep(x,y);
agents.add(Shaun);
System.out.println(agents);
break;
case 3 :
if(getCell(x, y)==0) {
setCell(x, y, 1);
}else {setCell(x, y, 0);}
Wolf agrou=new Wolf(x,y);
agents.add(agrou);
break;
default:
break;