neues Programm
This commit is contained in:
parent
09a995eccf
commit
1eca2474d8
129
src/Life.java
129
src/Life.java
@ -1,5 +1,9 @@
|
|||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
public class Life implements ILife {
|
public class Life implements ILife {
|
||||||
|
|
||||||
|
String[] lifeArray;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Life l = new Life(new String[] { " ",
|
Life l = new Life(new String[] { " ",
|
||||||
" ",
|
" ",
|
||||||
@ -10,9 +14,7 @@ public class Life implements ILife {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Life() {
|
public Life() { nukeAll(); }
|
||||||
nukeAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Life(String[] setup) {
|
public Life(String[] setup) {
|
||||||
this();
|
this();
|
||||||
@ -26,30 +28,147 @@ public class Life implements ILife {
|
|||||||
@Override
|
@Override
|
||||||
public void nukeAll() {
|
public void nukeAll() {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
|
lifeArray = new String[] { " ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" ",
|
||||||
|
" "};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setAlive(int x, int y) {
|
public void setAlive(int x, int y) {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
|
String newString = "";
|
||||||
|
|
||||||
|
newString = lifeArray[y].substring(0, x) + "*" + lifeArray[y].substring(x);
|
||||||
|
|
||||||
|
lifeArray[y] = newString;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@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
|
||||||
|
String newString = "";
|
||||||
|
|
||||||
|
newString = lifeArray[y].substring(0, x) + " " + lifeArray[y].substring(x);
|
||||||
|
|
||||||
|
lifeArray[y] = newString;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@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(lifeArray[y].charAt(x) == '*')
|
||||||
|
return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ILife nextGeneration() {
|
public ILife nextGeneration() {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
return null;
|
Life newLife = new Life();
|
||||||
|
for(int y = 0; y < lifeArray.length; y++) {
|
||||||
|
for(int x = 0; x < lifeArray[y].length(); x++) {
|
||||||
|
int neighbors = countNeighbors(x, y);
|
||||||
|
|
||||||
|
writeNewGen(neighbors, newLife, x, y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return newLife;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeNewGen(int neighbors, Life newLife, int x, int y) {
|
||||||
|
|
||||||
|
//if the space is alive...
|
||||||
|
if(isAlive(x, y)) {
|
||||||
|
|
||||||
|
//...and has more than three or less than two neighbors, it dies
|
||||||
|
if (neighbors > 3 || neighbors < 2)
|
||||||
|
newLife.setDead(x, y);
|
||||||
|
|
||||||
|
//...and has 2 to 3 neighbors, it survives
|
||||||
|
if (neighbors == 3 || neighbors == 2)
|
||||||
|
newLife.setAlive(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
//if the space is dead...
|
||||||
|
if(!isAlive(x, y)) {
|
||||||
|
|
||||||
|
//and has 3 neighbors, it comes to life
|
||||||
|
if (neighbors == 3) {
|
||||||
|
newLife.setAlive(x, y);
|
||||||
|
} else {
|
||||||
|
//and has anything but 3 neighbors, it stays dead
|
||||||
|
newLife.setDead(x, y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private int countNeighbors(int x, int y) {
|
||||||
|
int counter = 0;
|
||||||
|
|
||||||
|
//count three above
|
||||||
|
counter += countAbove(x, y);
|
||||||
|
|
||||||
|
//count three below
|
||||||
|
counter += countBelow(x, y);
|
||||||
|
|
||||||
|
//count two sides
|
||||||
|
counter += countSides(x, y);
|
||||||
|
|
||||||
|
return counter;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int countSides(int x, int y) {
|
||||||
|
int counter = 0;
|
||||||
|
|
||||||
|
//is the space left us within the playing field and alive?
|
||||||
|
if(x-1 >= 0 && isAlive(x-1, y))
|
||||||
|
counter++;
|
||||||
|
|
||||||
|
//is the space right us within the playing field and alive?
|
||||||
|
if(x+1 < lifeArray[y].length() && isAlive(x+1, y))
|
||||||
|
counter++;
|
||||||
|
|
||||||
|
return counter;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int countBelow(int x, int y) {
|
||||||
|
int counter = 0;
|
||||||
|
|
||||||
|
//is the line below us within the playing field?
|
||||||
|
if(y+1 < lifeArray.length) {
|
||||||
|
for (int i = x-1; i <= x+1; i++) {
|
||||||
|
|
||||||
|
//are the spaces on our left, middle and right bottom within the playing field alive?
|
||||||
|
if (i >= 0 && i < lifeArray[y+1].length() && isAlive(i, y + 1))
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return counter;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int countAbove(int x, int y) {
|
||||||
|
int counter = 0;
|
||||||
|
|
||||||
|
//is the line above us within the playing field?
|
||||||
|
if(y-1 >= 0) {
|
||||||
|
for (int i = x-1; i <= x+1; i++) {
|
||||||
|
|
||||||
|
//are the spaces on our left, middle and right top within the playing field alive?
|
||||||
|
if (i >= 0 && i < lifeArray[y-1].length() && isAlive(i, y - 1))
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return counter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void showBoard() {
|
||||||
|
for(int y = 0; y < lifeArray.length-1; y++)
|
||||||
|
System.out.println(Arrays.toString(new String[] {lifeArray[y]}));
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -15,6 +15,7 @@ public class LifeTest {
|
|||||||
// 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(1, 1));
|
||||||
}
|
}
|
||||||
@ -22,16 +23,54 @@ public class LifeTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void destroyLonelyCell() {
|
public void destroyLonelyCell() {
|
||||||
|
// Arrange: drei lebende Zellen
|
||||||
|
Life l = new Life();
|
||||||
|
l.setAlive(0, 0);
|
||||||
|
l.setAlive(0, 1);
|
||||||
|
l.setAlive(0, 2);
|
||||||
|
|
||||||
|
// Act: Berechnung der Folgegeneration
|
||||||
|
ILife nextGen = l.nextGeneration();
|
||||||
|
|
||||||
|
|
||||||
|
// Assert: Rasterpunkt mit einem Nachbar soll tot sein
|
||||||
|
assertFalse(nextGen.isAlive(0, 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void keepAliveCell() {
|
public void keepAliveCell() {
|
||||||
|
// Arrange: drei lebende Zellen
|
||||||
|
Life l = new Life();
|
||||||
|
l.setAlive(0, 0);
|
||||||
|
l.setAlive(0, 1);
|
||||||
|
l.setAlive(0, 2);
|
||||||
|
|
||||||
|
// Act: Berechnung der Folgegeneration
|
||||||
|
ILife nextGen = l.nextGeneration();
|
||||||
|
|
||||||
|
|
||||||
|
// Assert: Rasterpunkt mit zwei Nachbarn sollte jetzt am Leben bleiben
|
||||||
|
assertTrue(nextGen.isAlive(0, 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void destroyCrowdedCell() {
|
public void destroyCrowdedCell() {
|
||||||
|
// Arrange: fünf lebende Zellen
|
||||||
|
Life l = new Life();
|
||||||
|
l.setAlive(0, 0);
|
||||||
|
l.setAlive(0, 1);
|
||||||
|
l.setAlive(0, 2);
|
||||||
|
l.setAlive(1, 1);
|
||||||
|
l.setAlive(1, 0);
|
||||||
|
|
||||||
|
// Act: Berechnung der Folgegeneration
|
||||||
|
ILife nextGen = l.nextGeneration();
|
||||||
|
|
||||||
|
|
||||||
|
// Assert: Rasterpunkt mit vier Nachbarn sollte jetzt tot sein
|
||||||
|
assertFalse(nextGen.isAlive(0, 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user