diff --git a/src/Life.java b/src/Life.java index 43a5058..9522bde 100644 --- a/src/Life.java +++ b/src/Life.java @@ -49,26 +49,23 @@ public class Life implements ILife { @Override public ILife nextGeneration() { - Life newLife = new Life(); + Life newLife = new Life(); - // Prüfe alle Zellen im aktuellen Spielfeld - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { - int neighbors = countAliveNeighbors(x, y); + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + int neighbors = countAliveNeighbors(x, y); - if (isAlive(x, y)) { - // Regel: Weniger als 2 Nachbarn → Zelle stirbt - if (neighbors < 2) { - newLife.setDead(x, y); - } else { - newLife.setAlive(x, y); - } + if (isAlive(x, y)) { + if (neighbors < 2 || neighbors > 3) { + newLife.setDead(x, y); + } else { + newLife.setAlive(x, y); } } } - return newLife; } - + return newLife; + } private int countAliveNeighbors(int x, int y) { int count = 0; @@ -78,6 +75,6 @@ public class Life implements ILife { if (isAlive(x + dx, y + dy)) count++; } } + return count; } - return null; }