Umänderung der nextGeneration Methode
This commit is contained in:
parent
a3e3a3bc21
commit
a18048da87
@ -49,26 +49,23 @@ public class Life implements ILife {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ILife nextGeneration() {
|
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 y = 0; y < height; y++) {
|
for (int x = 0; x < width; x++) {
|
||||||
for (int x = 0; x < width; x++) {
|
int neighbors = countAliveNeighbors(x, y);
|
||||||
int neighbors = countAliveNeighbors(x, y);
|
|
||||||
|
|
||||||
if (isAlive(x, y)) {
|
if (isAlive(x, y)) {
|
||||||
// Regel: Weniger als 2 Nachbarn → Zelle stirbt
|
if (neighbors < 2 || neighbors > 3) {
|
||||||
if (neighbors < 2) {
|
newLife.setDead(x, y);
|
||||||
newLife.setDead(x, y);
|
} else {
|
||||||
} else {
|
newLife.setAlive(x, y);
|
||||||
newLife.setAlive(x, y);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return newLife;
|
|
||||||
}
|
}
|
||||||
|
return newLife;
|
||||||
|
}
|
||||||
|
|
||||||
private int countAliveNeighbors(int x, int y) {
|
private int countAliveNeighbors(int x, int y) {
|
||||||
int count = 0;
|
int count = 0;
|
||||||
@ -78,6 +75,6 @@ public class Life implements ILife {
|
|||||||
if (isAlive(x + dx, y + dy)) count++;
|
if (isAlive(x + dx, y + dy)) count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user