functioning adding and removal of agents

This commit is contained in:
Guillaume LE CHARTIER 2024-05-29 11:10:29 +02:00
parent 184c5bd759
commit 96c202115d
2 changed files with 30 additions and 36 deletions

View File

@ -1,14 +0,0 @@
package backend;
import java.awt.Color;
import java.util.ArrayList;
import java.util.Random;
//null agent that signifies the absence of agents
public class NullAgent extends Agent {
NullAgent(int x,int y){
super(x,y,Color.black);
}
}

View File

@ -69,17 +69,7 @@ public class Simulator extends Thread {
//TODO-COMPLETE : replace with proper return
return this.height;
}
//we define the initial size of the arraylist as 1 in order to ease the code in the clickAgent part
public void StartingCap(int initialCapacity){
// Initial capacity of the agent arraylist
initialCapacity = COL_NUM*LINE_NUM;
agents = new ArrayList<>(initialCapacity);
//we fill it with null values
for (int i = 0; i <= agents.size(); i++) {
agents.add(i,NullAgent.NullAgent);
}
System.out.println(agents);
}
//Should probably stay as is
public void run() {
int stepCount=0;
@ -211,24 +201,42 @@ public class Simulator extends Thread {
}
else {
int radius = 1;
int i=0;
Agent agent = new Sheep(x,y);
System.out.println(agents);
if(agent.isInArea(x,y,radius)){
for(int i=0;i<=agents.size();i++){
if (agents.get(i) instanceof Sheep){
//if there are no agents, skip directly to adding one
boolean removal =false;
if (agents.size()>0 ){
//if an agent is in this area we iterate in the arraylist agents in order to find which one it is
for(i=0;i<=agents.size()-1;i++){
//if we proceed to find the agent on the coordinates of the click, we remove it
if (agents.get(i).getX() == x && agents.get(i).getY() == y ){
agents.remove(i);
System.out.println("Corresponding agent found, proceeding with removal");
System.out.println(agents.size());
removal = true;
}
}
if(i==agents.size() && removal ==false){
//if we find no corresponding agent we add one
System.out.println("no agents to remove, proceeding with creation");
setSheep(x, y);
}
}
else{
System.out.println("1st iteration");
setSheep(x,y);
System.out.println("Corresponding agent not found, proceeding with creation");
}
}
}
if (enableLogs) {
System.out.println("clickAgent Called, Agent created at: " + x + "," + y + "");
}