From cd00a261196debfc7518c1171e4b107a29cb6fc4 Mon Sep 17 00:00:00 2001 From: athly Date: Tue, 21 May 2024 18:10:02 +0200 Subject: [PATCH] modif makestep et loadrule --- .../src/backend/Simulator.java | 137 ++++++++++-------- 1 file changed, 80 insertions(+), 57 deletions(-) diff --git a/OOP_A06_Project_Life/src/backend/Simulator.java b/OOP_A06_Project_Life/src/backend/Simulator.java index b845774..aade830 100644 --- a/OOP_A06_Project_Life/src/backend/Simulator.java +++ b/OOP_A06_Project_Life/src/backend/Simulator.java @@ -25,18 +25,9 @@ public class Simulator extends Thread { private boolean loopingBorder; private boolean clickActionFlag; private int loopDelay = 150; - int[][] cells = initializeMatrix(100, 100, 0); + int[][] cells = Cells.initializeMatrix(100, 100, 0); //TODO : add missing attribute(s) - public static int[][] initializeMatrix(int rows, int cols, int initialValue) { - int[][] matrix = new int[rows][cols]; - for (int i = 0; i < rows; i++) { - for (int j = 0; j < cols; j++) { - matrix[i][j] = initialValue; - } - } - return matrix; - } public Simulator(MyInterface mjfParam) { mjf = mjfParam; stopFlag=false; @@ -53,9 +44,10 @@ public class Simulator extends Thread { //Default rule : Survive always, birth never - for(int i =0; i<9; i++) { - fieldSurviveValues.add(i); - } + for(int i =0; i<9; i++) { + fieldSurviveValues.add(i); + } + } @@ -105,12 +97,45 @@ public class Simulator extends Thread { agent.getX(), agent.getY(), ANIMAL_AREA_RADIUS); - if(!agent.liveTurn( - neighbors, - this)) { + 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 getSaveState() { ArrayList saveState = new ArrayList<>(); - // Iterate in each row of the world then puts the values of the row in a string + // takes from each row of the world then puts the values of the row in a string for (int i = 0; i < LINE_NUM; i++) { StringBuilder line = new StringBuilder(); - - // Append each cell value in the row, separated by semicolons for (int j = 0; j < COL_NUM; j++) { line.append(cells[i][j]); if (j < COL_NUM - 1) { line.append(";"); // Add semicolon separator for each value except the last one } } - - // Add the line to the saveState list saveState.add(line.toString()); } @@ -307,7 +327,7 @@ public class Simulator extends Thread { } public void toggleClickAction() { - clickActionFlag = !clickActionFlag; + clickActionFlag =! clickActionFlag; } /** @@ -319,35 +339,38 @@ public class Simulator extends Thread { * @see loadRule for inverse process */ public ArrayList getRule() { - //TODO : complete method with proper return - - return null; + ///FaLSE ArrayList lines = new ArrayList(); + fieldSurviveValues.add(csv.read) + fieldSurviveValues = csv.read + return lines } - public void loadRule(ArrayList lines) { - if(lines.size()<=0) { - System.out.println("empty rule file"); - return; - } - //TODO : remove previous rule (=emptying lists) - - - String surviveLine = lines.get(0); - String birthLine = lines.get(1); - String[] surviveElements = surviveLine.split(";"); - for(int x=0; x getAgentsSave() {