This commit is contained in:
Laure BEL 2024-05-27 12:16:07 +02:00
commit 410a3be1f6
1 changed files with 21 additions and 37 deletions

View File

@ -25,7 +25,6 @@ public class Simulator extends Thread {
private boolean loopingBorder;
private boolean clickActionFlag;
private int loopDelay = 150;
private int[][] grid;
private int[][] world;
//TODO : add missing attribute(s)
@ -118,22 +117,7 @@ public class Simulator extends Thread {
// in agent classes
int[][] newWorld = new int[getWidth()][getHeight()];
// Apply Game of Life rules
for (int x = 0; x < getWidth(); x++) {
for (int y = 0; y < getHeight(); y++) {
int aliveNeighbors = countAliveNeighbors(x, y);
if (world[x][y] == 1) {
newWorld[x][y] = (aliveNeighbors < 2 || aliveNeighbors > 3) ? 0 : 1;
} else {
newWorld[x][y] = (aliveNeighbors == 3) ? 1 : 0;
}
}
}
world = newWorld;
ArrayList<Agent> newAgents = new ArrayList<>();
@ -142,12 +126,13 @@ public class Simulator extends Thread {
this.getNeighboringAnimals(
agent.getX(),
agent.getY(),
ANIMAL_AREA_RADIUS);
if(!agent.liveTurn(
neighbors,
this)) {
agents.remove(agent);
}
ANIMAL_AREA_RADIUS);}
//if(!agent.liveTurn(
// neighbors,
// this)) {
// agents.remove(agent);
//{
}
//then evolution of the field
// TODO : apply game rule to all cells of the field
@ -168,19 +153,6 @@ public class Simulator extends Thread {
for (int x = 0; x < getWidth(); x++) {
for (int y = 0; y < getHeight(); y++) {
int aliveNeighbors = countAliveNeighbors(x, y);
if (world[x][y] == 1) {
newWorld[x][y] = (aliveNeighbors < 2 || aliveNeighbors > 3) ? 0 : 1;
} else {
newWorld[x][y] = (aliveNeighbors == 3) ? 1 : 0;
}
}
}
}
/*
* leave this as is
@ -257,8 +229,20 @@ public class Simulator extends Thread {
* the simulated world in its present state
*/
public ArrayList<String> getSaveState() {
ArrayList<String> saveState = new ArrayList<>();
for (int i = 0; i < getHeight(); i++) {
StringBuilder lineState = new StringBuilder();
for (int j = 0 ; j < getHeight() ; j++) {
lineState.append(getCell(i, j));
if (j < getWidth() - 1) {
lineState.append(";");
}
}
saveState.add(lineState.toString());
}
return saveState;
//TODO : complete method with proper return
return null;
}
/**
*