created looped borders function

This commit is contained in:
Raphaelsav 2024-05-24 17:09:17 +02:00
parent 1fa9941eed
commit 868da2b221
1 changed files with 38 additions and 13 deletions

View File

@ -49,19 +49,19 @@ public class Simulator extends Thread {
//TODO : add missing attribute initialization
//initialize grid with dead cells
for(int x=0; x <= getWidth();x++) {
for(int x=0; x < getWidth();x++) {
ArrayList<Cell> arrayCell = new ArrayList<Cell>(); //initialize first dimension with ArrayLists
cells.add(arrayCell);
for(int y=0; y <= getHeight(); y++) {
for(int y=0; y < getHeight(); y++) {
Cell cell = new Cell(0); //create a dead cell
cells.get(x).add(cell); //assign dead cell to its position in second dimension array
}
}
for(int x=0; x <= getWidth();x++) {
for(int x=0; x < getWidth();x++) {
ArrayList<Cell> arrayNewCell = new ArrayList<Cell>(); //initialize first dimension with ArrayLists
newCells.add(arrayNewCell);
for(int y=0; y <= getHeight(); y++) {
for(int y=0; y < getHeight(); y++) {
Cell newCell = new Cell(0); //create a dead cell
newCells.get(x).add(newCell); //assign dead cell to its position in second dimension array
}
@ -141,12 +141,36 @@ public class Simulator extends Thread {
//for each neighbor
for(int i = x-radius; i <= x+radius; i++) {
for(int j = y-radius; j <= y+radius; j++) {
if(i!=-1 && i!=getWidth()+1 && j!=-1 && j!=getHeight()+1) { //if neighbor is not outside the map
if(i!=-1 && i!=getWidth() && j!=-1 && j!=getHeight()) { //if neighbor is not outside the map
if(getCell(i,j) == 1) { //if alive, add 1 to counter
aliveNeighbors++;
}
}
}
else if(loopingBorder==true && (i==-1 || i==getWidth() || j==-1 || j==getHeight() )) { //if looping borders enabled and neighbor outside map
int I=i;
int J=j;
if(I==-1) {
I=getWidth()-1;
}
else if(I==getWidth()) {
I=0;
}
if(J==-1) {
J=getHeight()-1;
}
else if(J==getHeight()) {
J=0;
}
if(getCell(I,J) == 1) { //if alive, add 1 to counter
aliveNeighbors++;
}
}
}
}
@ -191,8 +215,8 @@ public class Simulator extends Thread {
*/
//calculate and set newCells based on cells
for(int x=0; x <= getWidth();x++) {
for(int y=0; y <= getHeight(); y++) {
for(int x=0; x < getWidth();x++) {
for(int y=0; y < getHeight(); y++) {
int status = getCell(x,y);
@ -206,8 +230,8 @@ public class Simulator extends Thread {
}
//update cells
for(int x=0; x <= getWidth();x++) {
for(int y=0; y <= getHeight(); y++) {
for(int x=0; x < getWidth();x++) {
for(int y=0; y < getHeight(); y++) {
int status = getNewCell(x,y);
setCell(x, y, status);
}
@ -243,9 +267,10 @@ public class Simulator extends Thread {
//if clickActionFlag = true then create an agent
//else if flag = false then change value of cell
if(clickActionFlag) {
System.out.print(x );
System.out.println(y);
}else {
//System.out.println(getAliveNeighbors(x, y, LIFE_AREA_RADIUS)); //print nb of neighbours
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);}