Next Generation

This commit is contained in:
Oli040404 2025-02-12 10:08:00 +01:00
parent ddedb7735e
commit 1ec5b98cb7
2 changed files with 32 additions and 1 deletions

View File

@ -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;
}
}

View File

@ -33,6 +33,7 @@ public class LifeTest {
@Test
public void keepAliveCell() {
}