WORKING but getRule ??

This commit is contained in:
athly 2024-05-22 14:21:46 +02:00
parent cd00a26119
commit 2e594ef409
1 changed files with 60 additions and 51 deletions

View File

@ -87,54 +87,65 @@ public class Simulator extends Thread {
* makes all the actions to go from one step to the other * makes all the actions to go from one step to the other
*/ */
public void makeStep() { public void makeStep() {
// agent behaviors first // Create a new matrix for the next state of the cells
// only modify if sure of what you do int[][] newCells = Cells.initializeMatrix(LINE_NUM, COL_NUM, 0);
// to modify agent behavior, see liveTurn method int nbNeighborAlive;
// in agent classes
for(Agent agent : agents) {
ArrayList<Agent> neighbors =
this.getNeighboringAnimals(
agent.getX(),
agent.getY(),
ANIMAL_AREA_RADIUS);
if(!agent.liveTurn(neighbors,this)) {
agents.remove(agent);
}
}
int[][] newCells = Cells.initializeMatrix(100, 100, 0); //matrix of the future states of the cells to not disturb the counter
int nbNeighborAlive = 0; //counter for alive cells
for (int j=0; j< LINE_NUM;j++) { // test for each cells
for (int i=0; i< COL_NUM;i++) {
for (int a=-1; a<2;a++) { // test the surrounding cells
for(int b = -1 ; b<2; b++) {
if (i+a >0 && j+b >0 && i+a <100 && j+b <100) {
if(cells[i+a][j+b] == 1) {
nbNeighborAlive += 1;
}
}
}
}
int aliveNextIteration = 0;
if (cells[i][j] == 1) {
nbNeighborAlive +=-1; //removes the actual cell that got counted
for(int k =0; k<fieldSurviveValues.size();k++ ) {
if (nbNeighborAlive == fieldSurviveValues.get(k)) {
aliveNextIteration = 1;
}
}
}
if (cells[i][j] == 0) {
for(int k =0; k<fieldBirthValues.size();k++ ) {
if (nbNeighborAlive == fieldBirthValues.get(k)) {
aliveNextIteration = 1;
}
}
}
newCells[i][j] = aliveNextIteration; // Iterate through each cell in the grid
} for (int j = 0; j < LINE_NUM; j++) {
} for (int i = 0; i < COL_NUM; i++) {
cells = newCells; nbNeighborAlive = 0; // Reset the counter for each cell
// Check the surrounding cells
for (int a = -1; a <= 1; a++) {
for (int b = -1; b <= 1; b++) {
if (a == 0 && b == 0) {
continue; // Skip the current cell itself
}
int neighborX = i + a;
int neighborY = j + b;
if (loopingBorder) {
// Wrap around the indices using modulo operation
neighborX = (neighborX + COL_NUM) % COL_NUM;
neighborY = (neighborY + LINE_NUM) % LINE_NUM;
} else {
// Ensure the indices are within bounds
if (neighborX < 0 || neighborY < 0 || neighborX >= COL_NUM || neighborY >= LINE_NUM) {
continue;
}
}
if (cells[neighborY][neighborX] == 1) {
nbNeighborAlive++;
}
}
}
// Initialize aliveNextIteration to 0
int aliveNextIteration = 0;
// Apply survival rules
if (cells[j][i] == 1) {
// Check if the number of live neighbors is in the survive list
if (fieldSurviveValues.contains(nbNeighborAlive)) {
aliveNextIteration = 1;
}
} else {
// Apply birth rules
if (fieldBirthValues.contains(nbNeighborAlive)) {
aliveNextIteration = 1;
}
}
// Set the new state of the cell
newCells[j][i] = aliveNextIteration;
}
}
// Update the current state of the cells with the new state
cells = newCells;
} }
//then evolution of the field //then evolution of the field
// TODO : apply game rule to all cells of the field // TODO : apply game rule to all cells of the field
@ -340,9 +351,7 @@ public class Simulator extends Thread {
*/ */
public ArrayList<String> getRule() { public ArrayList<String> getRule() {
///FaLSE ArrayList<String> lines = new ArrayList<String>(); ///FaLSE ArrayList<String> lines = new ArrayList<String>();
fieldSurviveValues.add(csv.read)
fieldSurviveValues = csv.read
return lines
} }
public void loadRule(ArrayList<String> lines) { public void loadRule(ArrayList<String> lines) {
if (lines.size() <= 0) { if (lines.size() <= 0) {