third commit

This commit is contained in:
Justus Görgens 2021-12-22 15:54:40 +01:00
parent 892cd3870f
commit 12e086ed53
3 changed files with 54 additions and 17 deletions

View File

@ -11,4 +11,7 @@ public interface ILife {
// Methoden zum Fortschreiben der Generationen // Methoden zum Fortschreiben der Generationen
public ILife nextGeneration(); public ILife nextGeneration();
//Methode zum ausgeben der Welt
public void printWelt();
} }

View File

@ -2,7 +2,8 @@ import java.util.Arrays;
public class Life implements ILife { public class Life implements ILife {
static String[] welt = new String[]{" ", static String[] welt = new String[]{
" ",
" ", " ",
" *** ", " *** ",
" ", " ",
@ -24,10 +25,10 @@ public class Life implements ILife {
for (int x = 0; x < setup[y].length(); x++) for (int x = 0; x < setup[y].length(); x++)
if (setup[y].charAt(x) != ' ') if (setup[y].charAt(x) != ' ')
setAlive(x, y); setAlive(x, y);
printZelle(); printWelt();
} }
private void printZelle() { public void printWelt() {
for (int i = 0; i < welt.length; i++) for (int i = 0; i < welt.length; i++)
System.out.println(Arrays.toString(new String[]{welt[i]})); System.out.println(Arrays.toString(new String[]{welt[i]}));
} }
@ -59,7 +60,7 @@ public class Life implements ILife {
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 (welt[x].charAt(y) == '*') if (welt[y].charAt(x) == '*')
return true; return true;
else else
return false; return false;
@ -69,7 +70,8 @@ public class Life implements ILife {
public ILife nextGeneration() { public ILife nextGeneration() {
// TODO Auto-generated method stub // TODO Auto-generated method stub
String[] neueWelt = new String[]{" ", String[] neueWelt = new String[]{
" ",
" ", " ",
" ", " ",
" ", " ",
@ -84,18 +86,48 @@ public class Life implements ILife {
//neue Zelle //neue Zelle
//TODO fehler wenn x = 0 //TODO fehler wenn x = 0
if (nachbarn == 3) if (nachbarn == 3) {
welt[y] = welt[y].substring(0, x - 1) + '*' + welt[y].substring(x + 1); if (x > 0 && x < welt[0].length() - 1) {
neueWelt[y] = neueWelt[y].substring(0, x - 1) + '*' + neueWelt[y].substring(x);
} else if (x == 0) {
neueWelt[y] = '*' + neueWelt[y].substring(x);
} else {
neueWelt[y] = neueWelt[y].substring(0, x - 1) + '*';
}
}
//eine Zelle stirbt an überpopulation //eine Zelle stirbt an überpopulation oder einsamkeit
//TODO fehler wenn x = 0 //TODO fehler wenn x = 0
if (nachbarn < 2 || nachbarn > 3) if (nachbarn < 2 || nachbarn > 3) {
welt[y] = welt[y].substring(0, x - 1) + ' ' + welt[y].substring(x + 1); if (x > 0 && x < welt[0].length() - 1) {
System.out.println(welt[0].length());
System.out.print(nachbarn);
System.out.print(", " + x);
System.out.println(", " + y);
neueWelt[y] = neueWelt[y].substring(0, x - 1) + ' ' + neueWelt[y].substring(x);
} else if (x == 0) {
neueWelt[y] = ' ' + neueWelt[y].substring(x);
} else {
neueWelt[y] = neueWelt[y].substring(0, x - 1) + ' ';
} }
return null;
} }
private int lebendeNachbarn(int y, int x) { // 1, 1 //eine Zelle bleibt am Leben
//TODO fehler wenn x = 0
if ((nachbarn == 2) && (welt[y].charAt(x) == '*')) {
if (x > 0 && x < welt[0].length() - 1) {
neueWelt[y] = neueWelt[y].substring(0, x - 1) + '*' + neueWelt[y].substring(x);
} else if (x == 0) {
neueWelt[y] = '*' + neueWelt[y].substring(x);
} else {
neueWelt[y] = neueWelt[y].substring(0, x - 1) + '*';
}
}
}
return new Life(neueWelt);
}
public int lebendeNachbarn(int y, int x) { // 1, 1
int nachbarn = 0; int nachbarn = 0;
@ -109,7 +141,7 @@ public class Life implements ILife {
if (welt[y].charAt(x - 1) == '*') { if (welt[y].charAt(x - 1) == '*') {
nachbarn++; nachbarn++;
} }
if (y <= welt.length - 1) { if (y < welt.length - 1) {
if (welt[y + 1].charAt(x - 1) == '*') { if (welt[y + 1].charAt(x - 1) == '*') {
nachbarn++; nachbarn++;
} }
@ -117,7 +149,7 @@ public class Life implements ILife {
} }
//rechte nachbars-spalte //rechte nachbars-spalte
if (x <= welt[0].length() - 1) { if (x < welt[0].length() - 1) {
if (y > 0) { if (y > 0) {
if (welt[y - 1].charAt(x + 1) == '*') { if (welt[y - 1].charAt(x + 1) == '*') {
nachbarn++; nachbarn++;
@ -126,7 +158,7 @@ public class Life implements ILife {
if (welt[y].charAt(x + 1) == '*') { if (welt[y].charAt(x + 1) == '*') {
nachbarn++; nachbarn++;
} }
if (y <= welt.length - 1) { if (y < welt.length - 1) {
if (welt[y + 1].charAt(x + 1) == '*') { if (welt[y + 1].charAt(x + 1) == '*') {
nachbarn++; nachbarn++;
} }
@ -139,7 +171,7 @@ public class Life implements ILife {
nachbarn++; nachbarn++;
} }
} }
if (y <= welt.length -1) { if (y < welt.length - 1) {
if (welt[y + 1].charAt(x) == '*') { if (welt[y + 1].charAt(x) == '*') {
nachbarn++; nachbarn++;
} }

View File

@ -8,15 +8,17 @@ public class LifeTest {
// Arrange: drei lebende Zellen // Arrange: drei lebende Zellen
Life l = new Life(); Life l = new Life();
l.printWelt();
l.setAlive(0, 0); l.setAlive(0, 0);
l.setAlive(0, 1); l.setAlive(0, 1);
l.setAlive(0, 2); l.setAlive(0, 2);
l.printWelt();
// Act: Berechnung der Folgegeneration // Act: Berechnung der Folgegeneration
ILife nextGen = l.nextGeneration(); ILife nextGen = l.nextGeneration();
// Assert: Rasterpunkt mit drei Nachbarn sollte jetzt leben // Assert: Rasterpunkt mit drei Nachbarn sollte jetzt leben
assertTrue(nextGen.isAlive(1, 1)); assertTrue(nextGen.isAlive(0, 1));
} }