This commit is contained in:
titou 2024-05-27 17:27:29 +02:00
parent 7a3716e537
commit 5766fa1d00
1 changed files with 56 additions and 17 deletions

View File

@ -95,20 +95,15 @@ public class Simulator extends Thread {
// only modify if sure of what you do
// to modify agent behavior, see liveTurn method
// in agent classes
for(Agent agent : agents) {
ArrayList<Agent> neighbors =
this.getNeighboringAnimals(
agent.getX(),
agent.getY(),
ANIMAL_AREA_RADIUS);
if(!agent.liveTurn(
neighbors,
this)) {
for (int i = 0; i < agents.size(); i++) {
Agent agent = agents.get(i);
ArrayList<Agent> neighbors = this.getNeighboringAnimals(agent.getX(), agent.getY(), ANIMAL_AREA_RADIUS);
if (!agent.liveTurn(neighbors, this)) {
agents.remove(agent);
i--;
}
}
//then evolution of the field
// TODO : apply game rule to all cells of the field
/* you should distribute this action in methods/classes
* don't write everything here !
@ -123,11 +118,56 @@ public class Simulator extends Thread {
* and the count is in the birth list,
* then the cell becomes alive
*/
int[][] newField = new int[LINE_NUM][COL_NUM];
for (int y = 0; y < LINE_NUM; y++) {
for (int x = 0; x < COL_NUM; x++) {
int aliveNeighbors = countAliveNeighbors(x, y);
if (field[y][x] == 1) {
if (fieldSurviveValues.contains(aliveNeighbors)) {
newField[y][x] = 1;
} else {
newField[y][x] = 0;
}
} else {
if (fieldBirthValues.contains(aliveNeighbors)) {
newField[y][x] = 1;
} else {
newField[y][x] = 0;
}
}
}
}
field = newField;
}
private int countAliveNeighbors(int x, int y) {
int count = 0;
for (int i = -LIFE_AREA_RADIUS; i <= LIFE_AREA_RADIUS; i++) {
for (int j = -LIFE_AREA_RADIUS; j <= LIFE_AREA_RADIUS; j++) {
if (i == 0 && j == 0) {
continue;
}
int neighborX = x + i;
int neighborY = y + j;
if (loopingBorder) {
neighborX = (neighborX + COL_NUM) % COL_NUM;
neighborY = (neighborY + LINE_NUM) % LINE_NUM;
} else {
if (neighborX < 0 || neighborY < 0 || neighborX >= COL_NUM || neighborY >= LINE_NUM) {
continue;
}
}
if (field[neighborY][neighborX] == 1) {
count++;
}
}
}
return count;
}
/*
* leave this as is
@ -148,7 +188,6 @@ public class Simulator extends Thread {
* method called when clicking on a cell in the interface
*/
public void clickCell(int x, int y) {
//TODO : complete method
if (clickActionFlag) {
for (Agent agent : agents) {
if(agent.getX() == x && agent.getY() == y) {
@ -162,6 +201,7 @@ public class Simulator extends Thread {
field[y][x] = (field[y][x] == 0) ? 1 : 0;
}
}
//DONE
/**
* get cell value in simulated world
@ -215,7 +255,6 @@ public class Simulator extends Thread {
* the simulated world in its present state
*/
public ArrayList<String> getSaveState() {
//TODO : complete method with proper return
ArrayList<String> saveState = new ArrayList<>();
for (int y = 0; y<LINE_NUM; y++) {
StringBuilder line = new StringBuilder();
@ -268,7 +307,6 @@ public class Simulator extends Thread {
* to be alive in new state
*/
public void generateRandom(float chanceOfLife) {
//TODO : complete method
Random random = new Random();
for (int y = 0; y<LINE_NUM; y++) {
for (int x = 0; x < COL_NUM; x++) {
@ -283,6 +321,7 @@ public class Simulator extends Thread {
* and use it here
*/
}
//DONE
public boolean isLoopingBorder() {
return loopingBorder;