2021-12-13 15:21:05 +01:00
|
|
|
public class Life implements ILife {
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
Life l = new Life(new String[] { " ",
|
|
|
|
" ",
|
|
|
|
" *** ",
|
|
|
|
" ",
|
|
|
|
" " });
|
|
|
|
l = (Life) l.nextGeneration();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Life() {
|
|
|
|
nukeAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Life(String[] setup) {
|
|
|
|
this();
|
|
|
|
for (int y = 0; y < setup.length; y++)
|
|
|
|
for (int x = 0; x < setup[y].length(); x++)
|
|
|
|
if (setup[y].charAt(x) != ' ')
|
|
|
|
setAlive(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void nukeAll() {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setAlive(int x, int y) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setDead(int x, int y) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isAlive(int x, int y) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ILife nextGeneration() {
|
2025-02-12 10:16:24 +01:00
|
|
|
Life newLife = new Life();
|
2025-02-12 10:08:00 +01:00
|
|
|
|
2025-02-12 10:16:24 +01:00
|
|
|
for (int y = 0; y < height; y++) {
|
|
|
|
for (int x = 0; x < width; x++) {
|
|
|
|
int neighbors = countAliveNeighbors(x, y);
|
2025-02-12 10:08:00 +01:00
|
|
|
|
2025-02-12 10:16:24 +01:00
|
|
|
if (isAlive(x, y)) {
|
|
|
|
if (neighbors < 2 || neighbors > 3) {
|
|
|
|
newLife.setDead(x, y);
|
|
|
|
} else {
|
|
|
|
newLife.setAlive(x, y);
|
2025-02-12 10:08:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-02-12 10:16:24 +01:00
|
|
|
return newLife;
|
|
|
|
}
|
2025-02-12 10:08:00 +01:00
|
|
|
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
}
|
2025-02-12 10:16:24 +01:00
|
|
|
return count;
|
2025-02-12 10:08:00 +01:00
|
|
|
}
|
2021-12-13 15:21:05 +01:00
|
|
|
}
|