SaveState

This commit is contained in:
Balthazar SQUINABOL 2024-05-29 17:02:41 +02:00
parent f0826db6c5
commit 818e8d2e98
3 changed files with 47 additions and 12 deletions

View File

@ -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<Agent> neighbors, Simulator world) {
if(world.getCell(x, y)==1) {
world.setCell(x, y, 0);
public boolean liveTurn(ArrayList<Agent> 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;

View File

@ -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<String> getSaveState() {
//TODO : complete method with proper return
return null;
ArrayList<String> 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

View File

@ -351,13 +351,14 @@ public class MyInterface extends JFrame {
public void clicSaveToFileButton() {
String fileName=SelectFile();
if (fileName.length()>0) {
String fileName = SelectFile();
if (fileName.length() > 0) {
ArrayList<String> content = mySimu.getSaveState();
writeFile(fileName, (String[]) content.toArray());
writeFile(fileName, content.toArray(new String[0]));
}
}
public void clicSaveAgentsToFileButton() {
String fileName=SelectFile();
if (fileName.length()>0) {