saveAgent and load agent working(save agent needs modification to take
into account the type of agent)
This commit is contained in:
parent
3467b887ea
commit
e93caa207d
|
|
@ -141,46 +141,36 @@ public class Simulator extends Thread {
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getAliveNeighbors(int x, int y, int radius) {
|
public int getAliveNeighbors(int x, int y, int radius) {
|
||||||
|
|
||||||
//to compensate for the cell itself being alive or not
|
//to compensate for the cell itself being alive or not
|
||||||
int aliveNeighbors = -1;
|
int aliveNeighbors = -1;
|
||||||
if(getCell(x,y) == 0) {
|
if(getCell(x,y) == 0) {
|
||||||
aliveNeighbors++;
|
aliveNeighbors++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//for each neighbor
|
//for each neighbor
|
||||||
for(int i = x-radius; i <= x+radius; i++) {
|
for(int i = x-radius; i <= x+radius; i++) {
|
||||||
for(int j = y-radius; j <= y+radius; j++) {
|
for(int j = y-radius; j <= y+radius; j++) {
|
||||||
if(i!=-1 && i!=getWidth() && j!=-1 && j!=getHeight()) { //if neighbor is not outside the map
|
if(i!=-1 && i!=getWidth() && j!=-1 && j!=getHeight()) { //if neighbor is not outside the map
|
||||||
|
|
||||||
if(getCell(i,j) == 1) { //if alive, add 1 to counter
|
if(getCell(i,j) == 1) { //if alive, add 1 to counter
|
||||||
aliveNeighbors++;
|
aliveNeighbors++;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else if(loopingBorder==true && (i==-1 || i==getWidth() || j==-1 || j==getHeight() )) { //if looping borders enabled and neighbor outside map
|
else if(loopingBorder==true && (i==-1 || i==getWidth() || j==-1 || j==getHeight() )) { //if looping borders enabled and neighbor outside map
|
||||||
int I=i;
|
int I=i;
|
||||||
int J=j;
|
int J=j;
|
||||||
|
|
||||||
if(I==-1) {
|
if(I==-1) {
|
||||||
I=getWidth()-1;
|
I=getWidth()-1;
|
||||||
}
|
}
|
||||||
else if(I==getWidth()) {
|
else if(I==getWidth()) {
|
||||||
I=0;
|
I=0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(J==-1) {
|
if(J==-1) {
|
||||||
J=getHeight()-1;
|
J=getHeight()-1;
|
||||||
}
|
}
|
||||||
else if(J==getHeight()) {
|
else if(J==getHeight()) {
|
||||||
J=0;
|
J=0;
|
||||||
}
|
}if(getCell(I,J) == 1) { //if alive, add 1 to counter
|
||||||
|
|
||||||
if(getCell(I,J) == 1) { //if alive, add 1 to counter
|
|
||||||
aliveNeighbors++;
|
aliveNeighbors++;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -197,22 +187,16 @@ public class Simulator extends Thread {
|
||||||
// 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>();
|
ArrayList<Integer> arrayDeadAgent=new ArrayList<Integer>();
|
||||||
int counter=0;
|
|
||||||
for(Agent agent : agents) {
|
for(Agent agent : agents) {
|
||||||
|
|
||||||
ArrayList<Agent> neighbors = this.getNeighboringAnimals(agent.getX(), agent.getY(), ANIMAL_AREA_RADIUS);
|
ArrayList<Agent> neighbors = this.getNeighboringAnimals(agent.getX(), agent.getY(), ANIMAL_AREA_RADIUS);
|
||||||
if(!agent.liveTurn(neighbors,this)) {
|
if(!agent.liveTurn(neighbors,this)) {
|
||||||
//agents.remove(agent);
|
//agents.remove(agent);
|
||||||
arrayDeadAgent.add(counter);
|
arrayDeadAgent.add(agents.indexOf(agent));
|
||||||
}
|
}
|
||||||
counter++;
|
|
||||||
}
|
}
|
||||||
int counterTwo=0;
|
|
||||||
for(Integer i : arrayDeadAgent) {
|
for(Integer i : arrayDeadAgent) {
|
||||||
if (i==arrayDeadAgent.get(0)) {
|
agents.remove(i.intValue()-arrayDeadAgent.indexOf(i));
|
||||||
agents.remove(i.intValue());
|
|
||||||
}else{agents.remove(i.intValue()-counterTwo);}
|
|
||||||
counterTwo++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//then evolution of the field
|
//then evolution of the field
|
||||||
|
|
@ -558,13 +542,55 @@ 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> arrayLines=new ArrayList<String>();
|
||||||
return null;
|
String sumAgentToLine="";
|
||||||
|
for(Agent agent :agents) {
|
||||||
|
sumAgentToLine+=agent.getX()+",";
|
||||||
|
sumAgentToLine+=agent.getY();
|
||||||
|
if (agents.indexOf(agent)<agents.size()-1);{
|
||||||
|
sumAgentToLine+=";";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
arrayLines.add(sumAgentToLine);
|
||||||
|
//System.out.println(sumElementToLine.length());
|
||||||
|
//System.out.println(arrayLines);
|
||||||
|
return arrayLines;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadAgents(ArrayList<String> stringArray) {
|
public void loadAgents(ArrayList<String> lines) {
|
||||||
//TODO : Same idea as other load methods, but for agent list
|
//TODO : Same idea as other load methods, but for agent list
|
||||||
|
if(lines.size()<=0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String firstLine = lines.get(0);
|
||||||
|
String[] firstLineElements = firstLine.split(";");
|
||||||
|
if(firstLineElements.length<=0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int j =0; j<lines.size();j++) {
|
||||||
|
String line = lines.get(j);
|
||||||
|
String[] lineElements = line.split(";");
|
||||||
|
for(int i=0; i<lineElements.length;i++) {
|
||||||
|
String[] coordinates = lineElements[i].split(",");
|
||||||
|
int x = Integer.parseInt(coordinates[0]);
|
||||||
|
int y = Integer.parseInt(coordinates[1]);
|
||||||
|
switch (clickActionFlag) {
|
||||||
|
case 2 :
|
||||||
|
Sheep Shaun=new Sheep(x,y);
|
||||||
|
agents.add(Shaun);
|
||||||
|
//System.out.println(agents);
|
||||||
|
break;
|
||||||
|
case 3 :
|
||||||
|
Wolf agrou=new Wolf(x,y);
|
||||||
|
agents.add(agrou);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -327,7 +327,8 @@ public class MyInterface extends JFrame {
|
||||||
String fileName=SelectFile();
|
String fileName=SelectFile();
|
||||||
if (fileName.length()>0) {
|
if (fileName.length()>0) {
|
||||||
ArrayList<String> content = mySimu.getSaveState();
|
ArrayList<String> content = mySimu.getSaveState();
|
||||||
|
String[] strArr = Arrays.copyOf(content.toArray(), content.toArray().length, String[].class);
|
||||||
|
writeFile(fileName, strArr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -344,7 +345,8 @@ public class MyInterface extends JFrame {
|
||||||
String fileName=SelectFile();
|
String fileName=SelectFile();
|
||||||
if (fileName.length()>0) {
|
if (fileName.length()>0) {
|
||||||
ArrayList<String> content = mySimu.getAgentsSave();
|
ArrayList<String> content = mySimu.getAgentsSave();
|
||||||
writeFile(fileName, (String[]) content.toArray());
|
String[] strArr = Arrays.copyOf(content.toArray(), content.toArray().length, String[].class);
|
||||||
|
writeFile(fileName, strArr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue