Umänderung der nextGeneration Methode

This commit is contained in:
Oli040404 2025-02-12 10:16:24 +01:00
parent a3e3a3bc21
commit a18048da87

View File

@ -51,14 +51,12 @@ public class Life implements ILife {
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) {
if (neighbors < 2 || neighbors > 3) {
newLife.setDead(x, y);
} else {
newLife.setAlive(x, y);
@ -69,7 +67,6 @@ public class Life implements ILife {
return newLife;
}
private int countAliveNeighbors(int x, int y) {
int count = 0;
for (int dy = -1; dy <= 1; dy++) {
@ -78,6 +75,6 @@ public class Life implements ILife {
if (isAlive(x + dx, y + dy)) count++;
}
}
return count;
}
return null;
}