load and save agent

This commit is contained in:
Laure BEL 2024-05-29 19:43:55 +02:00
parent 127cc64841
commit d39dd5d90e
2 changed files with 40 additions and 17 deletions

View File

@ -1,2 +1,2 @@
1;3;5;8 1;3;5;8
3;5;7; 3;5;7

1 1 1;3;5;8 3 5 8
2 3 3;5;7 5 7

View File

@ -129,11 +129,11 @@ public class Simulator extends Thread {
agent.getX(), agent.getX(),
agent.getY(), agent.getY(),
ANIMAL_AREA_RADIUS);} ANIMAL_AREA_RADIUS);}
//if(!agent.liveTurn( /*if(!agent.liveTurn(
// neighbors, neighbors,
//this)) { this)) {
//agents.remove(agent); agents.remove(agent);
//{ {*/
// Apply Game of Life rules // Apply Game of Life rules
@ -415,23 +415,46 @@ public class Simulator extends Thread {
public ArrayList<String> getAgentsSave() { public ArrayList<String> getAgentsSave() {
ArrayList<String> agentsSave = new ArrayList<>(); ArrayList<String> agentsList = new ArrayList<String>();
for (int j = 0; j < getHeight(); j++) { String sheepLine = "1,";
StringBuilder lineState = new StringBuilder(); String wolfLine = "2,";
for (int i = 0 ; i < getWidth() ; i++) { for (Agent agent : agents) {
lineState.append(getCell(i, j)); int x = agent.getX();
if (j < getHeight() -1) { int y = agent.getY();
lineState.append(","); if (agent instanceof Sheep)
} {
sheepLine = sheepLine + x + ";" + y + ",";
}
else if (agent instanceof Wolf)
{
wolfLine = wolfLine + x + ";" + y + ",";
} }
agentsSave.add(lineState.toString());
} }
return agentsSave; agentsList.add(sheepLine);
agentsList.add(wolfLine);
return agentsList;
} }
public void loadAgents(ArrayList<String> stringArray) { public void loadAgents(ArrayList<String> stringArray) {
//TODO : Same idea as other load methods, but for agent list //TODO : Same idea as other load methods, but for agent list
for(int y =0; y<stringArray.size();y++) {
String line = stringArray.get(y);
String[] lineElements = line.split(",");
int agentType = Integer.parseInt(lineElements[0]);
for(int x=1; x<lineElements.length;x++) {
String[] coordinates = lineElements[x].split(";");
int i = Integer.parseInt(coordinates[0]);
int j = Integer.parseInt(coordinates[1]);
if (agentType == 1) {
Sheep newSheep = new Sheep(i, j);
agents.add(newSheep);
}
if (agentType == 2) {
Wolf newWolf = new Wolf(i, j);
agents.add(newWolf);
}
}
}
} }
/** /**