From e785ba1cf814f1934b06cbe3690efe21d67ddf27 Mon Sep 17 00:00:00 2001 From: "guillaume.bonabau" Date: Fri, 31 May 2024 16:40:02 +0200 Subject: [PATCH] load Agents works (but not save agents) --- ressources/Agents/wolfAndSheep.csv | 0 src/backend/Sheep.java | 2 +- src/backend/Simulator.java | 43 +++++++++++++++++++++++++----- 3 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 ressources/Agents/wolfAndSheep.csv diff --git a/ressources/Agents/wolfAndSheep.csv b/ressources/Agents/wolfAndSheep.csv new file mode 100644 index 0000000..e69de29 diff --git a/src/backend/Sheep.java b/src/backend/Sheep.java index 59bcbe8..c6f9aaa 100644 --- a/src/backend/Sheep.java +++ b/src/backend/Sheep.java @@ -37,7 +37,7 @@ public class Sheep extends Agent { } else { hunger++; } - this.moveRandom(); + this.moveRandom(world); return hunger<10; //condition to be alive //if the sheep can reproduce it creates a new sheep in agents //if(canReproduce(neighbors, world)) { diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index a0e4877..4773c7b 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -18,8 +18,8 @@ public class Simulator extends Thread { private MyInterface mjf; - private final int COL_NUM = 10; - private final int LINE_NUM = 10; + private final int COL_NUM = 100; + private final int LINE_NUM = 100; private final int LIFE_TYPE_NUM = 4; //Conway Radius : 1 private final int LIFE_AREA_RADIUS = 1; @@ -71,7 +71,7 @@ public class Simulator extends Thread { //Default rule : Survive always, birth never //loadRule("ressources/Rule/conwayRule.json"); - loadRule("OOP_F1_Project/ressources/Rule/conwayRule.json"); + loadRule("ressources/Rule/conwayRule.json"); } @@ -526,7 +526,7 @@ public class Simulator extends Thread { @SuppressWarnings("unchecked") public void loadRule(String fileName) { System.out.println(fileName); - //TODO-INPROGRESS load json + //TODO-COMPLETE load json JSONParser jsonParser = new JSONParser(); try (FileReader reader = new FileReader(fileName)) { @@ -643,13 +643,42 @@ public class Simulator extends Thread { public ArrayList getAgentsSave() { - //TODO : Same idea as the other save method, but for agents - return null; + ArrayList agentSave = new ArrayList(); + String listSheep = ""; + String listWolf = ""; + //iterate through the agents arraylist + for(Agent agent : agents){ + if(agent instanceof Sheep) { + listSheep = listSheep + agent.getX() + "," + agent.getY() + ";"; + }else if (agent instanceof Wolf) { + listWolf = listWolf + agent.getX() + "," + agent.getY() + ";"; + } + } + agentSave.add(listSheep); + agentSave.add(listWolf); + return agentSave; } public void loadAgents(ArrayList stringArray) { //TODO : Same idea as other load methods, but for agent list - + // Load sheep agents + String sheepLine = stringArray.get(0); + String[] sheepCoordinates = sheepLine.split(";"); + for (String coordinate : sheepCoordinates) { + String[] xy = coordinate.split(","); + int x = Integer.parseInt(xy[0]); + int y = Integer.parseInt(xy[1]); + setSheep(x, y); + } + // Load wolf agents + String wolfLine = stringArray.get(1); + String[] wolfCoordinates = wolfLine.split(";"); + for (String coordinate : wolfCoordinates) { + String[] xy = coordinate.split(","); + int x = Integer.parseInt(xy[0]); + int y = Integer.parseInt(xy[1]); + setWolf(x, y); + } } /**