diff --git a/src/backend/Sheep.java b/src/backend/Sheep.java index 9420f05..c3a8250 100644 --- a/src/backend/Sheep.java +++ b/src/backend/Sheep.java @@ -12,6 +12,7 @@ public class Sheep extends Agent { int hunger; Random rand; + Simulator simulator; Sheep(int x,int y){ //first we call the constructor of the superClass(Animal) @@ -24,16 +25,24 @@ public class Sheep extends Agent { rand = new Random(); } - + boolean loopingBorder = simulator.isLoopingBorder(); + int width = simulator.getWidth(); + int height = simulator.getHeight(); /** * action of the animal * it can interact with the cells or with other animals * as you wish */ - public boolean liveTurn(ArrayList neighbors, Simulator world) { - if(world.getCell(x, y)==1) { - world.setCell(x, y, 0); + public boolean liveTurn(ArrayList neighbors, Simulator word) { + + //we check if the sheep is on the border of the world + //If loopingBorder == true, the world is a torus + //If loopingBorder == false, the world is a square and the sheep can't go out of the world + + if(simulator.getCell(x, y)==1) { + simulator.setCell(x, y, 0); + hunger = hunger--; } else { hunger++; } @@ -42,6 +51,11 @@ public class Sheep extends Agent { } private void moveRandom() { + //check if the sheep is on the border of the world + //If loopingBorder == true, the world is a torus + + + int direction = rand.nextInt(4); if(direction == 0) { x+=1; diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index 256a03a..e3ed745 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 = 100; - private final int LINE_NUM = 100; + private final int COL_NUM = 10; + private final int LINE_NUM = 10; private final int LIFE_TYPE_NUM = 4; //Conway Radius : 1 private final int LIFE_AREA_RADIUS = 1; @@ -70,7 +70,8 @@ public class Simulator extends Thread { //Default rule : Survive always, birth never - loadRule("ressources/Rule/conwayRule.json"); + //loadRule("ressources/Rule/conwayRule.json"); + loadRule("OOP_F1_Project/ressources/Rule/conwayRule.json"); } @@ -352,9 +353,28 @@ public class Simulator extends Thread { * the simulated world in its present state */ public ArrayList getSaveState() { - //TODO : complete method with proper return - return null; + ArrayList saveState = new ArrayList<>(); + + // Ensure height and width are properly initialized + int height = getHeight(); // Replace getHeight() with your method to get the height + int width = getWidth(); // Replace getWidth() with your method to get the width + + for (int y = 0; y < height; y++) { + StringBuilder lineBuilder = new StringBuilder(); + for (int x = 0; x < width; x++) { + lineBuilder.append(getCell(x, y)); + if (x < width - 1) { + lineBuilder.append(";"); + } + } + saveState.add(lineBuilder.toString()); + } + return saveState; } + + + + /** * * @param lines of file representing saved world state diff --git a/src/windowInterface/MyInterface.java b/src/windowInterface/MyInterface.java index d33f149..59a8c24 100644 --- a/src/windowInterface/MyInterface.java +++ b/src/windowInterface/MyInterface.java @@ -351,12 +351,13 @@ public class MyInterface extends JFrame { public void clicSaveToFileButton() { - String fileName=SelectFile(); - if (fileName.length()>0) { + String fileName = SelectFile(); + if (fileName.length() > 0) { ArrayList content = mySimu.getSaveState(); - writeFile(fileName, (String[]) content.toArray()); + writeFile(fileName, content.toArray(new String[0])); } } + public void clicSaveAgentsToFileButton() { String fileName=SelectFile();