Projet_V5

This commit is contained in:
Paul MATHY 2024-05-31 23:53:17 +02:00
parent 62a553b71f
commit fc9ea87712
2 changed files with 39 additions and 26 deletions

25
src/backend/Agents.java Normal file
View File

@ -0,0 +1,25 @@
package backend;
import java.util.ArrayList;
public class Agents {
private ArrayList<Agent> agentList;
public Agents() {
agentList = new ArrayList<Agent>();
}
public ArrayList<Agent> getAgentList() {
return agentList;
}
public void addAgent(Agent agent) {
agentList.add(agent);
}
public void removeAll() {
agentList.clear();
}
}

View File

@ -17,7 +17,7 @@ public class Simulator extends Thread {
//private ArrayList<Integer> fieldSurviveValues;
//private ArrayList<Integer> fieldBirthValues;
private ArrayList<Agent> agents;
//private ArrayList<Agent> agents;
private boolean stopFlag;
private boolean pauseFlag;
@ -35,6 +35,7 @@ public class Simulator extends Thread {
private int stepCount=0;
private Rules rules;
private Agents agents;
public Simulator(MyInterface mjfParam) {
mjf = mjfParam;
@ -43,21 +44,12 @@ public class Simulator extends Thread {
loopingBorder=false;
clickActionFlag=true;
agents = new ArrayList<Agent>();
//fieldSurviveValues = new ArrayList<Integer>();
//fieldBirthValues = new ArrayList<Integer>();
this.width=COL_NUM;
this.height=LINE_NUM;
gride = new Gride(height, width, this);
//Reset rules to Conway rules
//this.resetRules();
rules = new Rules();
agents = new Agents();
}
public int getWidth() {
@ -98,13 +90,11 @@ public class Simulator extends Thread {
public void makeStep() {
ListIterator<Agent> iter = agents.listIterator();
ListIterator<Agent> iter = agents.getAgentList().listIterator();
while(iter.hasNext()){
Agent agent = iter.next();
//System.out.println(agent.getX() + "," + agent.getY());
ArrayList<Agent> neighbors = this.getNeighboringAnimals(
agent.getX(),
agent.getY(),
@ -117,7 +107,7 @@ public class Simulator extends Thread {
iter.remove();
}
}
/*
/* Not working when atempt to remove one agent -> move to iterator
for(Agent agent : agents) {
ArrayList<Agent> neighbors =
this.getNeighboringAnimals(
@ -162,7 +152,7 @@ public class Simulator extends Thread {
this.setCell(x, y, newCellValue);
} else {
//Apply sheep /agent here
this.agents.add(new Sheep(x, y));
this.agents.addAgent(new Sheep(x, y));
}
return;
@ -173,13 +163,13 @@ public class Simulator extends Thread {
}
public ArrayList<Agent> getAnimals(){
return agents;
return agents.getAgentList();
}
public ArrayList<Agent> getNeighboringAnimals(int x, int y, int radius){
ArrayList<Agent> inArea = new ArrayList<Agent>();
for(int i=0;i<agents.size();i++) {
Agent agent = agents.get(i);
for(int i=0;i<agents.getAgentList().size();i++) {
Agent agent = agents.getAgentList().get(i);
if(agent.isInArea(x,y,radius)) {
inArea.add(agent);
}
@ -234,7 +224,7 @@ public class Simulator extends Thread {
rules.setConwayRules();
//Reset Agents
this.agents.clear();
this.agents.removeAll();
}
private void resetRules() {
@ -348,7 +338,6 @@ public class Simulator extends Thread {
}
}
newGrideUpdated.setCell(i, j, new Cell(newCellValue));
//System.out.println("applyStep called : " + newGrideUpdated.getCell(i, j).getValue() + " at " + i + " " + j);
}
}
gride = newGrideUpdated;
@ -356,13 +345,12 @@ public class Simulator extends Thread {
public ArrayList<String> getAgentsSave() {
//TODO : Same idea as the other save method, but for agents
ArrayList<String> strArrayList = new ArrayList<>();
String[] strArrayLine = new String[this.agents.size()];
String[] strArrayLine = new String[this.agents.getAgentList().size()];
for(int ii=0;ii<this.agents.size();ii++) {
strArrayLine[ii] = this.agents.get(ii).getX() + "," + this.agents.get(ii).getY();
for(int ii=0;ii<this.agents.getAgentList().size();ii++) {
strArrayLine[ii] = this.agents.getAgentList().get(ii).getX() + "," + this.agents.getAgentList().get(ii).getY();
}
strArrayList.add(String.join(";", strArrayLine));
@ -390,7 +378,7 @@ public class Simulator extends Thread {
int y = Integer.parseInt(coordinate.substring(commaPosition+1));
//Add an agent to the lista at x,y position
agents.add(new Sheep(x, y));
agents.addAgent(new Sheep(x, y));
}
}