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) { public boolean liveTurn(ArrayList<Agent> neighbors, Simulator world) {
if(world.getCell(x, y)==1) { if(world.getCell(x, y)==1) {
world.setCell(x, y, 0); world.setCell(x, y, 0);
hunger-=2;
} else { } else {
hunger++; hunger++;
} }
this.moveRandom(); this.moveRandom();
return hunger>10; return hunger<10;
} }
private void moveRandom() { private void moveRandom() {

View File

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