Next Generation
This commit is contained in:
parent
ddedb7735e
commit
1ec5b98cb7
@ -49,7 +49,37 @@ public class Life implements ILife {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ILife nextGeneration() {
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -33,6 +33,7 @@ public class LifeTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void keepAliveCell() {
|
public void keepAliveCell() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user