second commit

This commit is contained in:
Justus Görgens 2021-12-22 13:23:17 +01:00
parent cdabef59bb
commit 892cd3870f

View File

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