Merge remote-tracking branch 'origin/dev_squ'

This commit is contained in:
Balthazar SQUINABOL 2024-05-29 17:05:52 +02:00
commit e97f4e3861
3 changed files with 60 additions and 22 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++;
}
@ -41,16 +50,24 @@ public class Sheep extends Agent {
return hunger<10; //condition to be alive
}
private void moveRandom(Simulator world) {
//check is looping border is activated
if(world.isLoopingBorder()) {
//if looping border is activated we can move in any direction
x = (x+rand.nextInt(3)-1+world.getWidth())%world.getWidth();
y = (y+rand.nextInt(3)-1+world.getHeight())%world.getHeight();
} else {
//if looping border is not activated we can only move in the world
x = Math.max(0, Math.min(world.getWidth()-1, x+rand.nextInt(3)-1));
y = Math.max(0, Math.min(world.getHeight()-1, y+rand.nextInt(3)-1));
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;
}
if(direction == 1) {
y+=1;
}
if(direction == 2) {
x-=1;
}
if(direction == 3) {
y-=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,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<String> content = mySimu.getSaveState();
writeFile(fileName, (String[]) content.toArray());
writeFile(fileName, content.toArray(new String[0]));
}
}
public void clicSaveAgentsToFileButton() {
String fileName=SelectFile();