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

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