From 350665e641e1c6005f20d1859692dc7bdbc951e1 Mon Sep 17 00:00:00 2001 From: Raphaelsav <94864277+Raphaelsav@users.noreply.github.com> Date: Tue, 30 Apr 2024 21:44:46 +0200 Subject: [PATCH] methods cellDies and cellBorns implemented --- src/backend/Simulator.java | 51 ++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 30 deletions(-) diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index d233764..d3f47af 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -22,14 +22,15 @@ public class Simulator extends Thread { private ArrayList agents; private ArrayList> cells; - private ArrayList> newCells; + private ArrayList> newCells; private boolean stopFlag; private boolean pauseFlag; private boolean loopingBorder; private boolean clickActionFlag; private int loopDelay = 150; - + + //Rules rule = new Rules(); //TODO : add missing attribute(s) public Simulator(MyInterface mjfParam) { @@ -39,7 +40,7 @@ public class Simulator extends Thread { loopingBorder=false; clickActionFlag=false; cells = new ArrayList>(); - newCells = new ArrayList>(); + newCells = new ArrayList>(); agents = new ArrayList(); fieldBirthValues = new ArrayList(); @@ -58,10 +59,11 @@ public class Simulator extends Thread { } for(int x=0; x <= getWidth();x++) { - ArrayList arrayNewCell = new ArrayList(); //initialize first dimension with ArrayLists + ArrayList arrayNewCell = new ArrayList(); //initialize first dimension with ArrayLists newCells.add(arrayNewCell); for(int y=0; y <= getHeight(); y++) { - newCells.get(x).add(0); //assign dead cell to its position in second dimension array + Cell newCell = new Cell(0); //create a dead cell + newCells.get(x).add(newCell); //assign dead cell to its position in second dimension array } } @@ -113,18 +115,18 @@ public class Simulator extends Thread { int aliveNeighbors = getAliveNeighbors(x, y, LIFE_AREA_RADIUS); if(aliveNeighbors < 2 || aliveNeighbors > 3) { - //setNewCell(x, y, 0); - newCells.get(x).set(y, 0); - } + setNewCell(x, y, 0); + //newCells.get(x).set(y, 0); + }else{setNewCell(x, y, 1);} } public void cellBorns(int x, int y) { int aliveNeighbors = getAliveNeighbors(x, y, LIFE_AREA_RADIUS); if(aliveNeighbors == 3) { - //setNewCell(x, y, 1); - newCells.get(x).set(y, 1); - } + setNewCell(x, y, 1); + //newCells.get(x).set(y, 1); + }else{setNewCell(x, y, 0);} } public int getAliveNeighbors(int x, int y, int radius) { @@ -187,24 +189,13 @@ public class Simulator extends Thread { //i started from 1 and ended < to ignore the borders for(int x=1; x < getWidth();x++) { for(int y=1; y < getHeight(); y++) { + int status = getCell(x,y); if(status == 1) { - //cellDies(x, y); - int aliveNeighbors = getAliveNeighbors(x, y, LIFE_AREA_RADIUS); - - if(aliveNeighbors < 2 || aliveNeighbors > 3) { - //setNewCell(x, y, 0); - newCells.get(x).set(y, 0); - }else{newCells.get(x).set(y, status);} - }else if(status == 0) { - //cellBorns(x, y); - int aliveNeighbors = getAliveNeighbors(x, y, LIFE_AREA_RADIUS); - - if(aliveNeighbors == 3) { - //setNewCell(x, y, 1); - newCells.get(x).set(y, 1); - }else{newCells.get(x).set(y, status);} + cellDies(x, y); + }else if(status == 0) { + cellBorns(x, y); } } @@ -272,8 +263,8 @@ public class Simulator extends Thread { public int getNewCell(int x, int y) { //TODO : complete method with proper return - //int status = newCells.get(x).get(y).getAlive(); - int status = newCells.get(x).get(y); + int status = newCells.get(x).get(y).getAlive(); + //int status = newCells.get(x).get(y); return status; } @@ -312,9 +303,9 @@ public class Simulator extends Thread { public void setCell(int x, int y, int status) { cells.get(x).get(y).setAlive(status); } - /*public void setNewCell(int x, int y, int status) { + public void setNewCell(int x, int y, int status) { newCells.get(x).get(y).setAlive(status); - }*/ + } /** *