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