Looping border is now working!

This commit is contained in:
yoyom 2024-05-18 12:22:27 +02:00
parent fe2f80d27c
commit 8ddf555291
1 changed files with 19 additions and 5 deletions

View File

@ -87,15 +87,29 @@ public class Simulator extends Thread {
private int countAliveNeighbors(int x, int y) { private int countAliveNeighbors(int x, int y) {
int count = 0; int count = 0;
for (int i = x - 1; i <= x + 1; i++) { for (int i = -1; i <= 1; i++) {
for (int j = y - 1; j <= y + 1; j++) { for (int j = -1; j <= 1; j++) {
if (i >= 0 && i < COL_NUM && j >= 0 && j < LINE_NUM && !(i == x && j == y)) { if (i == 0 && j == 0) continue; // Skip the cell itself
count += grid[i][j];
int nx = x + i;
int ny = y + j;
if (loopingBorder) { // looping border
if (nx < 0) nx = COL_NUM - 1;
else if (nx >= COL_NUM) nx = 0;
if (ny < 0) ny = LINE_NUM - 1;
else if (ny >= LINE_NUM) ny = 0;
}
else { // closed border
if (nx < 0 || nx >= COL_NUM || ny < 0 || ny >= LINE_NUM) continue;
} }
count += grid[nx][ny];
} }
} }
return count; return count;
} }
/** /**
* method called at each step of the simulation * method called at each step of the simulation
@ -322,7 +336,7 @@ public class Simulator extends Thread {
} }
public void toggleLoopingBorder() { public void toggleLoopingBorder() {
loopingBorder =! loopingBorder; loopingBorder = !loopingBorder;
} }
public void setLoopDelay(int delay) { public void setLoopDelay(int delay) {