SaveState
This commit is contained in:
parent
f0826db6c5
commit
818e8d2e98
|
|
@ -12,6 +12,7 @@ public class Sheep extends Agent {
|
||||||
|
|
||||||
int hunger;
|
int hunger;
|
||||||
Random rand;
|
Random rand;
|
||||||
|
Simulator simulator;
|
||||||
|
|
||||||
Sheep(int x,int y){
|
Sheep(int x,int y){
|
||||||
//first we call the constructor of the superClass(Animal)
|
//first we call the constructor of the superClass(Animal)
|
||||||
|
|
@ -24,16 +25,24 @@ public class Sheep extends Agent {
|
||||||
rand = new Random();
|
rand = new Random();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean loopingBorder = simulator.isLoopingBorder();
|
||||||
|
int width = simulator.getWidth();
|
||||||
|
int height = simulator.getHeight();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* action of the animal
|
* action of the animal
|
||||||
* it can interact with the cells or with other animals
|
* it can interact with the cells or with other animals
|
||||||
* as you wish
|
* as you wish
|
||||||
*/
|
*/
|
||||||
public boolean liveTurn(ArrayList<Agent> neighbors, Simulator world) {
|
public boolean liveTurn(ArrayList<Agent> neighbors, Simulator word) {
|
||||||
if(world.getCell(x, y)==1) {
|
|
||||||
world.setCell(x, y, 0);
|
//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 {
|
} else {
|
||||||
hunger++;
|
hunger++;
|
||||||
}
|
}
|
||||||
|
|
@ -42,6 +51,11 @@ public class Sheep extends Agent {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void moveRandom() {
|
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);
|
int direction = rand.nextInt(4);
|
||||||
if(direction == 0) {
|
if(direction == 0) {
|
||||||
x+=1;
|
x+=1;
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,8 @@ public class Simulator extends Thread {
|
||||||
|
|
||||||
private MyInterface mjf;
|
private MyInterface mjf;
|
||||||
|
|
||||||
private final int COL_NUM = 100;
|
private final int COL_NUM = 10;
|
||||||
private final int LINE_NUM = 100;
|
private final int LINE_NUM = 10;
|
||||||
private final int LIFE_TYPE_NUM = 4;
|
private final int LIFE_TYPE_NUM = 4;
|
||||||
//Conway Radius : 1
|
//Conway Radius : 1
|
||||||
private final int LIFE_AREA_RADIUS = 1;
|
private final int LIFE_AREA_RADIUS = 1;
|
||||||
|
|
@ -70,7 +70,8 @@ public class Simulator extends Thread {
|
||||||
|
|
||||||
|
|
||||||
//Default rule : Survive always, birth never
|
//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
|
* the simulated world in its present state
|
||||||
*/
|
*/
|
||||||
public ArrayList<String> getSaveState() {
|
public ArrayList<String> getSaveState() {
|
||||||
//TODO : complete method with proper return
|
ArrayList<String> saveState = new ArrayList<>();
|
||||||
return null;
|
|
||||||
|
// 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
|
* @param lines of file representing saved world state
|
||||||
|
|
|
||||||
|
|
@ -354,10 +354,11 @@ public class MyInterface extends JFrame {
|
||||||
String fileName = SelectFile();
|
String fileName = SelectFile();
|
||||||
if (fileName.length() > 0) {
|
if (fileName.length() > 0) {
|
||||||
ArrayList<String> content = mySimu.getSaveState();
|
ArrayList<String> content = mySimu.getSaveState();
|
||||||
writeFile(fileName, (String[]) content.toArray());
|
writeFile(fileName, content.toArray(new String[0]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void clicSaveAgentsToFileButton() {
|
public void clicSaveAgentsToFileButton() {
|
||||||
String fileName=SelectFile();
|
String fileName=SelectFile();
|
||||||
if (fileName.length()>0) {
|
if (fileName.length()>0) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue