From 778ae63f4e61fda920e169f39f2e3c30f2121309 Mon Sep 17 00:00:00 2001 From: Raphaelsav <94864277+Raphaelsav@users.noreply.github.com> Date: Tue, 30 Apr 2024 20:03:12 +0200 Subject: [PATCH] finally working correctly, however not optimized at all yet --- src/backend/Cell.java | 5 +- src/backend/Simulator.java | 107 +++++++++++++++++++++++++++++++++++-- 2 files changed, 107 insertions(+), 5 deletions(-) diff --git a/src/backend/Cell.java b/src/backend/Cell.java index 1e3d9b5..83410d7 100644 --- a/src/backend/Cell.java +++ b/src/backend/Cell.java @@ -2,8 +2,11 @@ package backend; public class Cell { int alive; - private Simulator mySimu; + //private Simulator mySimu; + public Cell() { + + } public Cell(int alive) { this.alive = alive; } diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index f909703..d233764 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -2,16 +2,19 @@ package backend; import java.util.ArrayList; import windowInterface.MyInterface; +//import backend.Rules; public class Simulator extends Thread { private MyInterface mjf; + //private Rules rules; + private final int COL_NUM = 100; private final int LINE_NUM = 100; private final int LIFE_TYPE_NUM = 4; //Conway Radius : 1 - private final int LIFE_AREA_RADIUS = 1; + public final int LIFE_AREA_RADIUS = 1; //Animal Neighborhood Radius : 2 private final int ANIMAL_AREA_RADIUS = 2; private ArrayList fieldSurviveValues; @@ -19,6 +22,8 @@ public class Simulator extends Thread { private ArrayList agents; private ArrayList> cells; + private ArrayList> newCells; + private boolean stopFlag; private boolean pauseFlag; private boolean loopingBorder; @@ -34,6 +39,8 @@ public class Simulator extends Thread { loopingBorder=false; clickActionFlag=false; cells = new ArrayList>(); + newCells = new ArrayList>(); + agents = new ArrayList(); fieldBirthValues = new ArrayList(); fieldSurviveValues = new ArrayList(); @@ -42,7 +49,7 @@ public class Simulator extends Thread { //initialize grid with dead cells for(int x=0; x <= getWidth();x++) { - ArrayList arrayCell = new ArrayList(); //initialise first dimension with ArrayLists + ArrayList arrayCell = new ArrayList(); //initialize first dimension with ArrayLists cells.add(arrayCell); for(int y=0; y <= getHeight(); y++) { Cell cell = new Cell(0); //create a dead cell @@ -50,6 +57,14 @@ public class Simulator extends Thread { } } + for(int x=0; x <= getWidth();x++) { + 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 + } + } + //Default rule : Survive always, birth never for(int i =0; i<9; i++) { @@ -91,6 +106,45 @@ public class Simulator extends Thread { } + + + //i put everything here for now + public void cellDies(int x, int y) { + int aliveNeighbors = getAliveNeighbors(x, y, LIFE_AREA_RADIUS); + + if(aliveNeighbors < 2 || aliveNeighbors > 3) { + //setNewCell(x, y, 0); + newCells.get(x).set(y, 0); + } + } + + 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); + } + } + + public int getAliveNeighbors(int x, int y, int radius) { + int aliveNeighbors = -1; + if(getCell(x,y) == 0) { + aliveNeighbors++; + } + + + //for each neighbor + for(int i = x-radius; i <= x+radius; i++) { + for(int j = y-radius; j <= y+radius; j++) { + if(getCell(i,j) == 1) { //if alive, add 1 to counter + aliveNeighbors++; + } + } + } + + return aliveNeighbors; + } /** * method called at each step of the simulation * makes all the actions to go from one step to the other @@ -129,6 +183,40 @@ public class Simulator extends Thread { * then the cell becomes alive */ + //calculate and set newCells based on cells + //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);} + } + + } + } + + //update cells + for(int x=1; x < getWidth();x++) { + for(int y=1; y < getHeight(); y++) { + int status = getNewCell(x,y); + setCell(x, y, status); + } + } @@ -162,6 +250,7 @@ public class Simulator extends Thread { if(clickActionFlag) { }else { + //System.out.println(getAliveNeighbors(x, y, LIFE_AREA_RADIUS)); //print nb of neighbours if(getCell(x, y)==0) { setCell(x, y, 1); }else {setCell(x, y, 0);} @@ -180,6 +269,15 @@ public class Simulator extends Thread { int status = cells.get(x).get(y).getAlive(); return status; } + + 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); + return status; + } + + /** * * @return list of Animals in simulated world @@ -212,10 +310,11 @@ public class Simulator extends Thread { * @param val to set in cell */ public void setCell(int x, int y, int status) { - //TODO : complete method cells.get(x).get(y).setAlive(status); - } + /*public void setNewCell(int x, int y, int status) { + newCells.get(x).get(y).setAlive(status); + }*/ /** *