From 2e594ef409b734fbc4251b2507c3b613f91d789d Mon Sep 17 00:00:00 2001 From: athly Date: Wed, 22 May 2024 14:21:46 +0200 Subject: [PATCH] WORKING but getRule ?? --- .../src/backend/Simulator.java | 111 ++++++++++-------- 1 file changed, 60 insertions(+), 51 deletions(-) diff --git a/OOP_A06_Project_Life/src/backend/Simulator.java b/OOP_A06_Project_Life/src/backend/Simulator.java index aade830..d26017f 100644 --- a/OOP_A06_Project_Life/src/backend/Simulator.java +++ b/OOP_A06_Project_Life/src/backend/Simulator.java @@ -87,54 +87,65 @@ public class Simulator extends Thread { * makes all the actions to go from one step to the other */ public void makeStep() { - // agent behaviors first - // only modify if sure of what you do - // to modify agent behavior, see liveTurn method - // in agent classes - for(Agent agent : agents) { - ArrayList 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= 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 // TODO : apply game rule to all cells of the field @@ -340,9 +351,7 @@ public class Simulator extends Thread { */ public ArrayList getRule() { ///FaLSE ArrayList lines = new ArrayList(); - fieldSurviveValues.add(csv.read) - fieldSurviveValues = csv.read - return lines + } public void loadRule(ArrayList lines) { if (lines.size() <= 0) {