|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class Life implements ILife { |
|
|
public class Life implements ILife { |
|
|
|
|
|
|
|
|
static String[] zelle =new String[]{ " ", |
|
|
|
|
|
" ", |
|
|
|
|
|
" *** ", |
|
|
|
|
|
" ", |
|
|
|
|
|
" " }; |
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
|
|
Life l = new Life(zelle); |
|
|
|
|
|
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); |
|
|
|
|
|
printZelle(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void printZelle() { |
|
|
|
|
|
for (int i = 0; i < zelle.length; i++) |
|
|
|
|
|
System.out.println(Arrays.toString(new String[]{zelle[i]})); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public void nukeAll() { |
|
|
|
|
|
// TODO Auto-generated method stub |
|
|
|
|
|
|
|
|
|
|
|
for (int y = 0; y < zelle.length; y++) |
|
|
|
|
|
for (int x = 0; x < zelle[y].length(); x++) |
|
|
|
|
|
setDead(x, y); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public void setAlive(int x, int y) { |
|
|
|
|
|
// TODO Auto-generated method stub |
|
|
|
|
|
|
|
|
|
|
|
zelle[y] = zelle[y].substring(0, x) + '*' + zelle[y].substring(x+1); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public void setDead(int x, int y) { |
|
|
|
|
|
// TODO Auto-generated method stub |
|
|
|
|
|
|
|
|
|
|
|
zelle[y] = zelle[y].substring(0, x) + ' ' + zelle[y].substring(x+1); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public boolean isAlive(int x, int y) { |
|
|
|
|
|
// TODO Auto-generated method stub |
|
|
|
|
|
|
|
|
|
|
|
if(zelle[x].charAt(y) == '*') |
|
|
|
|
|
return true; |
|
|
|
|
|
else |
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public ILife nextGeneration() { |
|
|
|
|
|
// TODO Auto-generated method stub |
|
|
|
|
|
|
|
|
|
|
|
String[] nextZelle =new String[]{ " ", |
|
|
|
|
|
" ", |
|
|
|
|
|
" ", |
|
|
|
|
|
" ", |
|
|
|
|
|
" " }; |
|
|
|
|
|
|
|
|
|
|
|
for (int y = 0; y < zelle.length; y++) |
|
|
|
|
|
for (int x = 0; x < zelle[y].length(); x++){ |
|
|
|
|
|
|
|
|
|
|
|
//anzahl der lebenden nachbarn |
|
|
|
|
|
float nachbarn = lebendeNachbarn(y, x); |
|
|
|
|
|
|
|
|
|
|
|
//neuer * |
|
|
|
|
|
if(nachbarn == 3) |
|
|
|
|
|
zelle[y] = zelle[y].substring(0, x) + '*' + zelle[y].substring(x+1); |
|
|
|
|
|
|
|
|
|
|
|
//ein * stirbt an überpopulation |
|
|
|
|
|
if(nachbarn < 2 || nachbarn > 3) |
|
|
|
|
|
zelle[y] = zelle[y].substring(0, x) + ' ' + zelle[y].substring(x+1); |
|
|
|
|
|
} |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private float lebendeNachbarn(int y, int x) { |
|
|
|
|
|
|
|
|
|
|
|
float nachbarn = 0; |
|
|
|
|
|
|
|
|
|
|
|
if(y == 0) { |
|
|
|
|
|
if (zelle[y + 1].charAt(x - 1) == '*') { |
|
|
|
|
|
nachbarn++; |
|
|
|
|
|
} |
|
|
|
|
|
if (zelle[y].charAt(x - 1) == '*') { |
|
|
|
|
|
nachbarn++; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
static String[] welt = new String[]{" ", |
|
|
|
|
|
" ", |
|
|
|
|
|
" *** ", |
|
|
|
|
|
" ", |
|
|
|
|
|
" "}; |
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
|
|
Life l = new Life(welt); |
|
|
|
|
|
l = (Life) l.nextGeneration(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if(x == 4 && y == 4) { |
|
|
|
|
|
if (zelle[y - 1].charAt(x - 1) == '*') { |
|
|
|
|
|
nachbarn++; |
|
|
|
|
|
} |
|
|
|
|
|
if (zelle[y - 1].charAt(x) == '*') { |
|
|
|
|
|
nachbarn++; |
|
|
|
|
|
} |
|
|
|
|
|
if (zelle[y - 1].charAt(x + 1) == '*') { |
|
|
|
|
|
nachbarn++; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
|
printZelle(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void printZelle() { |
|
|
|
|
|
for (int i = 0; i < welt.length; i++) |
|
|
|
|
|
System.out.println(Arrays.toString(new String[]{welt[i]})); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if(!(x == 0 && y== 0)) { |
|
|
|
|
|
if (zelle[y].charAt(x + 1) == '*') { |
|
|
|
|
|
nachbarn++; |
|
|
|
|
|
} |
|
|
|
|
|
if (zelle[y + 1].charAt(x + 1) == '*') { |
|
|
|
|
|
nachbarn++; |
|
|
|
|
|
} |
|
|
|
|
|
if (zelle[y + 1].charAt(x) == '*') { |
|
|
|
|
|
nachbarn++; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public void nukeAll() { |
|
|
|
|
|
// TODO Auto-generated method stub |
|
|
|
|
|
|
|
|
|
|
|
for (int y = 0; y < welt.length; y++) |
|
|
|
|
|
for (int x = 0; x < welt[y].length(); x++) |
|
|
|
|
|
setDead(x, y); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return nachbarn; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public void setAlive(int x, int y) { |
|
|
|
|
|
// TODO Auto-generated method stub |
|
|
|
|
|
|
|
|
|
|
|
welt[y] = welt[y].substring(0, x) + '*' + welt[y].substring(x + 1); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public String toString() { |
|
|
|
|
|
return super.toString(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public void setDead(int x, int y) { |
|
|
|
|
|
// TODO Auto-generated method stub |
|
|
|
|
|
|
|
|
|
|
|
welt[y] = welt[y].substring(0, x) + ' ' + welt[y].substring(x + 1); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public boolean isAlive(int x, int y) { |
|
|
|
|
|
// TODO Auto-generated method stub |
|
|
|
|
|
|
|
|
|
|
|
if (welt[x].charAt(y) == '*') |
|
|
|
|
|
return true; |
|
|
|
|
|
else |
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public ILife nextGeneration() { |
|
|
|
|
|
// TODO Auto-generated method stub |
|
|
|
|
|
|
|
|
|
|
|
String[] neueWelt = new String[]{" ", |
|
|
|
|
|
" ", |
|
|
|
|
|
" ", |
|
|
|
|
|
" ", |
|
|
|
|
|
" "}; |
|
|
|
|
|
|
|
|
|
|
|
//Zellen einzeln betrachten |
|
|
|
|
|
for (int y = 0; y < welt.length; y++) |
|
|
|
|
|
for (int x = 0; x < welt[y].length(); x++) { |
|
|
|
|
|
|
|
|
|
|
|
//anzahl der lebenden nachbarn |
|
|
|
|
|
int nachbarn = lebendeNachbarn(y, x); |
|
|
|
|
|
|
|
|
|
|
|
//neue Zelle |
|
|
|
|
|
//TODO fehler wenn x = 0 |
|
|
|
|
|
if (nachbarn == 3) |
|
|
|
|
|
welt[y] = welt[y].substring(0, x - 1) + '*' + welt[y].substring(x + 1); |
|
|
|
|
|
|
|
|
|
|
|
//eine Zelle stirbt an überpopulation |
|
|
|
|
|
//TODO fehler wenn x = 0 |
|
|
|
|
|
if (nachbarn < 2 || nachbarn > 3) |
|
|
|
|
|
welt[y] = welt[y].substring(0, x - 1) + ' ' + welt[y].substring(x + 1); |
|
|
|
|
|
} |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private int lebendeNachbarn(int y, int x) { // 1, 1 |
|
|
|
|
|
|
|
|
|
|
|
int nachbarn = 0; |
|
|
|
|
|
|
|
|
|
|
|
//linke nachbars-spalte |
|
|
|
|
|
if (x > 0) { |
|
|
|
|
|
if (y > 0) { |
|
|
|
|
|
if (welt[y - 1].charAt(x - 1) == '*') { |
|
|
|
|
|
nachbarn++; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if (welt[y].charAt(x - 1) == '*') { |
|
|
|
|
|
nachbarn++; |
|
|
|
|
|
} |
|
|
|
|
|
if (y <= welt.length - 1) { |
|
|
|
|
|
if (welt[y + 1].charAt(x - 1) == '*') { |
|
|
|
|
|
nachbarn++; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//rechte nachbars-spalte |
|
|
|
|
|
if (x <= welt[0].length() - 1) { |
|
|
|
|
|
if (y > 0) { |
|
|
|
|
|
if (welt[y - 1].charAt(x + 1) == '*') { |
|
|
|
|
|
nachbarn++; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if (welt[y].charAt(x + 1) == '*') { |
|
|
|
|
|
nachbarn++; |
|
|
|
|
|
} |
|
|
|
|
|
if (y <= welt.length - 1) { |
|
|
|
|
|
if (welt[y + 1].charAt(x + 1) == '*') { |
|
|
|
|
|
nachbarn++; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// mittlere Spalte |
|
|
|
|
|
if (y > 0) { |
|
|
|
|
|
if (welt[y - 1].charAt(x) == '*') { |
|
|
|
|
|
nachbarn++; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if (y <= welt.length -1) { |
|
|
|
|
|
if (welt[y + 1].charAt(x) == '*') { |
|
|
|
|
|
nachbarn++; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return nachbarn; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public String toString() { |
|
|
|
|
|
return super.toString(); |
|
|
|
|
|
} |
|
|
} |
|
|
} |