third commit
This commit is contained in:
parent
892cd3870f
commit
12e086ed53
@ -11,4 +11,7 @@ public interface ILife {
|
||||
// Methoden zum Fortschreiben der Generationen
|
||||
public ILife nextGeneration();
|
||||
|
||||
//Methode zum ausgeben der Welt
|
||||
public void printWelt();
|
||||
|
||||
}
|
||||
|
@ -2,7 +2,8 @@ import java.util.Arrays;
|
||||
|
||||
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++)
|
||||
if (setup[y].charAt(x) != ' ')
|
||||
setAlive(x, y);
|
||||
printZelle();
|
||||
printWelt();
|
||||
}
|
||||
|
||||
private void printZelle() {
|
||||
public void printWelt() {
|
||||
for (int i = 0; i < welt.length; 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) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
if (welt[x].charAt(y) == '*')
|
||||
if (welt[y].charAt(x) == '*')
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
@ -69,7 +70,8 @@ public class Life implements ILife {
|
||||
public ILife nextGeneration() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
String[] neueWelt = new String[]{" ",
|
||||
String[] neueWelt = new String[]{
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
@ -84,18 +86,48 @@ public class Life implements ILife {
|
||||
|
||||
//neue Zelle
|
||||
//TODO fehler wenn x = 0
|
||||
if (nachbarn == 3)
|
||||
welt[y] = welt[y].substring(0, x - 1) + '*' + welt[y].substring(x + 1);
|
||||
if (nachbarn == 3) {
|
||||
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
|
||||
if (nachbarn < 2 || nachbarn > 3)
|
||||
welt[y] = welt[y].substring(0, x - 1) + ' ' + welt[y].substring(x + 1);
|
||||
if (nachbarn < 2 || nachbarn > 3) {
|
||||
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) + ' ';
|
||||
}
|
||||
}
|
||||
|
||||
//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 null;
|
||||
return new Life(neueWelt);
|
||||
}
|
||||
|
||||
private int lebendeNachbarn(int y, int x) { // 1, 1
|
||||
public int lebendeNachbarn(int y, int x) { // 1, 1
|
||||
|
||||
int nachbarn = 0;
|
||||
|
||||
@ -109,7 +141,7 @@ public class Life implements ILife {
|
||||
if (welt[y].charAt(x - 1) == '*') {
|
||||
nachbarn++;
|
||||
}
|
||||
if (y <= welt.length - 1) {
|
||||
if (y < welt.length - 1) {
|
||||
if (welt[y + 1].charAt(x - 1) == '*') {
|
||||
nachbarn++;
|
||||
}
|
||||
@ -117,7 +149,7 @@ public class Life implements ILife {
|
||||
}
|
||||
|
||||
//rechte nachbars-spalte
|
||||
if (x <= welt[0].length() - 1) {
|
||||
if (x < welt[0].length() - 1) {
|
||||
if (y > 0) {
|
||||
if (welt[y - 1].charAt(x + 1) == '*') {
|
||||
nachbarn++;
|
||||
@ -126,7 +158,7 @@ public class Life implements ILife {
|
||||
if (welt[y].charAt(x + 1) == '*') {
|
||||
nachbarn++;
|
||||
}
|
||||
if (y <= welt.length - 1) {
|
||||
if (y < welt.length - 1) {
|
||||
if (welt[y + 1].charAt(x + 1) == '*') {
|
||||
nachbarn++;
|
||||
}
|
||||
@ -139,7 +171,7 @@ public class Life implements ILife {
|
||||
nachbarn++;
|
||||
}
|
||||
}
|
||||
if (y <= welt.length -1) {
|
||||
if (y < welt.length - 1) {
|
||||
if (welt[y + 1].charAt(x) == '*') {
|
||||
nachbarn++;
|
||||
}
|
||||
|
@ -8,15 +8,17 @@ public class LifeTest {
|
||||
|
||||
// Arrange: drei lebende Zellen
|
||||
Life l = new Life();
|
||||
l.printWelt();
|
||||
l.setAlive(0, 0);
|
||||
l.setAlive(0, 1);
|
||||
l.setAlive(0, 2);
|
||||
l.printWelt();
|
||||
|
||||
// Act: Berechnung der Folgegeneration
|
||||
ILife nextGen = l.nextGeneration();
|
||||
|
||||
// Assert: Rasterpunkt mit drei Nachbarn sollte jetzt leben
|
||||
assertTrue(nextGen.isAlive(1, 1));
|
||||
assertTrue(nextGen.isAlive(0, 1));
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user