diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index 32eef87..ef512de 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -87,15 +87,29 @@ public class Simulator extends Thread { private int countAliveNeighbors(int x, int y) { int count = 0; - for (int i = x - 1; i <= x + 1; i++) { - for (int j = y - 1; j <= y + 1; j++) { - if (i >= 0 && i < COL_NUM && j >= 0 && j < LINE_NUM && !(i == x && j == y)) { - count += grid[i][j]; + for (int i = -1; i <= 1; i++) { + for (int j = -1; j <= 1; j++) { + if (i == 0 && j == 0) continue; // Skip the cell itself + + 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; } + /** * method called at each step of the simulation @@ -322,7 +336,7 @@ public class Simulator extends Thread { } public void toggleLoopingBorder() { - loopingBorder =! loopingBorder; + loopingBorder = !loopingBorder; } public void setLoopDelay(int delay) {