diff --git a/src/Life.java b/src/Life.java index 84ab855..8ef92e9 100644 --- a/src/Life.java +++ b/src/Life.java @@ -49,7 +49,37 @@ public class Life implements ILife { @Override public ILife nextGeneration() { - // TODO Auto-generated method stub + public ILife nextGeneration() { + 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); + + if (isAlive(x, y)) { + // Regel: Weniger als 2 Nachbarn → Zelle stirbt + if (neighbors < 2) { + newLife.setDead(x, y); + } else { + newLife.setAlive(x, y); + } + } + } + } + return newLife; + } + + + private int countAliveNeighbors(int x, int y) { + int count = 0; + for (int dy = -1; dy <= 1; dy++) { + for (int dx = -1; dx <= 1; dx++) { + if (dx == 0 && dy == 0) continue; // Eigene Zelle ignorieren + if (isAlive(x + dx, y + dy)) count++; + } + } + } return null; } } \ No newline at end of file diff --git a/test/LifeTest.java b/test/LifeTest.java index bd005b1..2d6f42b 100644 --- a/test/LifeTest.java +++ b/test/LifeTest.java @@ -33,6 +33,7 @@ public class LifeTest { @Test public void keepAliveCell() { + }