From e93caa207d7f6b6739bc4ca82ff6ac9c7ff06074 Mon Sep 17 00:00:00 2001 From: "l.dupuis-burtin" Date: Thu, 30 May 2024 21:50:20 +0200 Subject: [PATCH] saveAgent and load agent working(save agent needs modification to take into account the type of agent) --- src/backend/Simulator.java | 70 +++++++++++++++++++--------- src/windowInterface/MyInterface.java | 6 ++- 2 files changed, 52 insertions(+), 24 deletions(-) diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index f39a0aa..a02a65c 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -141,46 +141,36 @@ public class Simulator extends Thread { } public int getAliveNeighbors(int x, int y, int radius) { - //to compensate for the cell itself being alive or not int aliveNeighbors = -1; if(getCell(x,y) == 0) { aliveNeighbors++; } - - //for each neighbor for(int i = x-radius; i <= x+radius; i++) { 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(getCell(i,j) == 1) { //if alive, add 1 to counter aliveNeighbors++; } - } else if(loopingBorder==true && (i==-1 || i==getWidth() || j==-1 || j==getHeight() )) { //if looping borders enabled and neighbor outside map int I=i; int J=j; - if(I==-1) { I=getWidth()-1; } else if(I==getWidth()) { I=0; } - if(J==-1) { J=getHeight()-1; } else if(J==getHeight()) { 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++; } - } } } @@ -197,22 +187,16 @@ public class Simulator extends Thread { // to modify agent behavior, see liveTurn method // in agent classes ArrayList arrayDeadAgent=new ArrayList(); - int counter=0; for(Agent agent : agents) { ArrayList neighbors = this.getNeighboringAnimals(agent.getX(), agent.getY(), ANIMAL_AREA_RADIUS); if(!agent.liveTurn(neighbors,this)) { //agents.remove(agent); - arrayDeadAgent.add(counter); + arrayDeadAgent.add(agents.indexOf(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++; + agents.remove(i.intValue()-arrayDeadAgent.indexOf(i)); } //then evolution of the field @@ -558,13 +542,55 @@ public class Simulator extends Thread { } public ArrayList getAgentsSave() { - //TODO : Same idea as the other save method, but for agents - return null; + ArrayList arrayLines=new ArrayList(); + String sumAgentToLine=""; + for(Agent agent :agents) { + sumAgentToLine+=agent.getX()+","; + sumAgentToLine+=agent.getY(); + if (agents.indexOf(agent) stringArray) { + public void loadAgents(ArrayList lines) { //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; j0) { ArrayList 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(); if (fileName.length()>0) { ArrayList content = mySimu.getAgentsSave(); - writeFile(fileName, (String[]) content.toArray()); + String[] strArr = Arrays.copyOf(content.toArray(), content.toArray().length, String[].class); + writeFile(fileName, strArr); } }