Compare commits

...

2 Commits

Author SHA1 Message Date
Raphaelsav 1fa9941eed updated getAliveNeighbours so that it works on the borders 2024-05-20 17:06:13 +02:00
Raphaelsav 350665e641 methods cellDies and cellBorns implemented 2024-04-30 21:44:46 +02:00
1 changed files with 32 additions and 37 deletions

View File

@ -22,7 +22,7 @@ public class Simulator extends Thread {
private ArrayList<Agent> agents; private ArrayList<Agent> agents;
private ArrayList<ArrayList<Cell>> cells; private ArrayList<ArrayList<Cell>> cells;
private ArrayList<ArrayList<Integer>> newCells; private ArrayList<ArrayList<Cell>> newCells;
private boolean stopFlag; private boolean stopFlag;
private boolean pauseFlag; private boolean pauseFlag;
@ -30,6 +30,7 @@ public class Simulator extends Thread {
private boolean clickActionFlag; private boolean clickActionFlag;
private int loopDelay = 150; private int loopDelay = 150;
//Rules rule = new Rules();
//TODO : add missing attribute(s) //TODO : add missing attribute(s)
public Simulator(MyInterface mjfParam) { public Simulator(MyInterface mjfParam) {
@ -39,7 +40,7 @@ public class Simulator extends Thread {
loopingBorder=false; loopingBorder=false;
clickActionFlag=false; clickActionFlag=false;
cells = new ArrayList<ArrayList<Cell>>(); cells = new ArrayList<ArrayList<Cell>>();
newCells = new ArrayList<ArrayList<Integer>>(); newCells = new ArrayList<ArrayList<Cell>>();
agents = new ArrayList<Agent>(); agents = new ArrayList<Agent>();
fieldBirthValues = new ArrayList<Integer>(); fieldBirthValues = new ArrayList<Integer>();
@ -58,10 +59,11 @@ public class Simulator extends Thread {
} }
for(int x=0; x <= getWidth();x++) { for(int x=0; x <= getWidth();x++) {
ArrayList<Integer> arrayNewCell = new ArrayList<Integer>(); //initialize first dimension with ArrayLists ArrayList<Cell> arrayNewCell = new ArrayList<Cell>(); //initialize first dimension with ArrayLists
newCells.add(arrayNewCell); newCells.add(arrayNewCell);
for(int y=0; y <= getHeight(); y++) { 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,21 +115,23 @@ public class Simulator extends Thread {
int aliveNeighbors = getAliveNeighbors(x, y, LIFE_AREA_RADIUS); int aliveNeighbors = getAliveNeighbors(x, y, LIFE_AREA_RADIUS);
if(aliveNeighbors < 2 || aliveNeighbors > 3) { if(aliveNeighbors < 2 || aliveNeighbors > 3) {
//setNewCell(x, y, 0); setNewCell(x, y, 0);
newCells.get(x).set(y, 0); //newCells.get(x).set(y, 0);
} }else{setNewCell(x, y, 1);}
} }
public void cellBorns(int x, int y) { public void cellBorns(int x, int y) {
int aliveNeighbors = getAliveNeighbors(x, y, LIFE_AREA_RADIUS); int aliveNeighbors = getAliveNeighbors(x, y, LIFE_AREA_RADIUS);
if(aliveNeighbors == 3) { if(aliveNeighbors == 3) {
//setNewCell(x, y, 1); setNewCell(x, y, 1);
newCells.get(x).set(y, 1); //newCells.get(x).set(y, 1);
} }else{setNewCell(x, y, 0);}
} }
public int getAliveNeighbors(int x, int y, int radius) { public int getAliveNeighbors(int x, int y, int radius) {
//to compensate for the cell itself being alive or not
int aliveNeighbors = -1; int aliveNeighbors = -1;
if(getCell(x,y) == 0) { if(getCell(x,y) == 0) {
aliveNeighbors++; aliveNeighbors++;
@ -137,9 +141,12 @@ public class Simulator extends Thread {
//for each neighbor //for each neighbor
for(int i = x-radius; i <= x+radius; i++) { for(int i = x-radius; i <= x+radius; i++) {
for(int j = y-radius; j <= y+radius; j++) { for(int j = y-radius; j <= y+radius; j++) {
if(getCell(i,j) == 1) { //if alive, add 1 to counter if(i!=-1 && i!=getWidth()+1 && j!=-1 && j!=getHeight()+1) { //if neighbor is not outside the map
if(getCell(i,j) == 1) { //if alive, add 1 to counter
aliveNeighbors++; aliveNeighbors++;
} }
}
} }
} }
@ -184,35 +191,23 @@ public class Simulator extends Thread {
*/ */
//calculate and set newCells based on cells //calculate and set newCells based on cells
//i started from 1 and ended < to ignore the borders for(int x=0; x <= getWidth();x++) {
for(int x=1; x < getWidth();x++) { for(int y=0; y <= getHeight(); y++) {
for(int y=1; y < getHeight(); y++) {
int status = getCell(x,y); int status = getCell(x,y);
if(status == 1) { if(status == 1) {
//cellDies(x, y); cellDies(x, y); //check if cell should die
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) { }else if(status == 0) {
//cellBorns(x, y); cellBorns(x, y); //check if cell should born
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 //update cells
for(int x=1; x < getWidth();x++) { for(int x=0; x <= getWidth();x++) {
for(int y=1; y < getHeight(); y++) { for(int y=0; y <= getHeight(); y++) {
int status = getNewCell(x,y); int status = getNewCell(x,y);
setCell(x, y, status); setCell(x, y, status);
} }
@ -272,8 +267,8 @@ public class Simulator extends Thread {
public int getNewCell(int x, int y) { public int getNewCell(int x, int y) {
//TODO : complete method with proper return //TODO : complete method with proper return
//int status = newCells.get(x).get(y).getAlive(); int status = newCells.get(x).get(y).getAlive();
int status = newCells.get(x).get(y); //int status = newCells.get(x).get(y);
return status; return status;
} }
@ -312,9 +307,9 @@ public class Simulator extends Thread {
public void setCell(int x, int y, int status) { public void setCell(int x, int y, int status) {
cells.get(x).get(y).setAlive(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); newCells.get(x).get(y).setAlive(status);
}*/ }
/** /**
* *