ersten Test hab ich nach Stunden immer noch nicht zum laufen gekriegt und deshalb aufgegeben. Wahrscheinlich ist es irgendwas Dummes...
This commit is contained in:
parent
09a995eccf
commit
000e651384
@ -1,11 +1,20 @@
|
|||||||
public class Life implements ILife {
|
public class Life implements ILife {
|
||||||
|
|
||||||
|
private char[][] world = {
|
||||||
|
{'.', '.', '.', '.', '.'},
|
||||||
|
{'.', '.', '.', '.', '.'},
|
||||||
|
{'.', '.', '.', '.', '.'},
|
||||||
|
{'.', '.', '.', '.', '.'},
|
||||||
|
{'.', '.', '.', '.', '.'}
|
||||||
|
};
|
||||||
|
private char[][] newWorld = world;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Life l = new Life(new String[] { " ",
|
Life l = new Life(new String[] { ".....",
|
||||||
" ",
|
".....",
|
||||||
" *** ",
|
".***.",
|
||||||
" ",
|
".....",
|
||||||
" " });
|
"....." });
|
||||||
l = (Life) l.nextGeneration();
|
l = (Life) l.nextGeneration();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -16,40 +25,87 @@ public class Life implements ILife {
|
|||||||
|
|
||||||
public Life(String[] setup) {
|
public Life(String[] setup) {
|
||||||
this();
|
this();
|
||||||
for (int y = 0; y < setup.length; y++)
|
|
||||||
for (int x = 0; x < setup[y].length(); x++)
|
int x = 0;
|
||||||
if (setup[y].charAt(x) != ' ')
|
int y;
|
||||||
setAlive(x, y);
|
|
||||||
|
|
||||||
|
for (y = 0; y < setup.length; y++) {
|
||||||
|
for (x = 0; x < setup[y].length(); x++) {
|
||||||
|
if (setup[y].charAt(x) != '.') {
|
||||||
|
world[x][y] = '*';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void nukeAll() {
|
public void nukeAll() {
|
||||||
// TODO Auto-generated method stub
|
for (int y = 0; y < world.length; y++)
|
||||||
|
for (int x = 0; x < world[y].length; x++) {
|
||||||
|
setDead(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setAlive(int x, int y) {
|
public void setAlive(int x, int y) {
|
||||||
// TODO Auto-generated method stub
|
newWorld [x][y] = '*';
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setDead(int x, int y) {
|
public void setDead(int x, int y) {
|
||||||
// TODO Auto-generated method stub
|
newWorld [x][y] = '.';
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isAlive(int x, int y) {
|
public boolean isAlive(int x, int y) {
|
||||||
// TODO Auto-generated method stub
|
if(world[y][x] == '*'){
|
||||||
return false;
|
return true;
|
||||||
|
}
|
||||||
|
else return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ILife nextGeneration() {
|
public ILife nextGeneration() {
|
||||||
// TODO Auto-generated method stub
|
String[] s = new String[newWorld.length];
|
||||||
return null;
|
checkNeighbors();
|
||||||
|
for(int y = 0; y < newWorld.length; y++){
|
||||||
|
s[y] = new String (newWorld[y]);
|
||||||
|
System.out.println(s[y]);
|
||||||
|
}
|
||||||
|
return new Life(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkNeighbors(){
|
||||||
|
for (int y = 0; y < world.length; y++)
|
||||||
|
for (int x = 0; x < world[y].length; x++) {
|
||||||
|
if (!isAlive(x, y) && getNeighborCount(x, y) == 3) {
|
||||||
|
setAlive(x, y);
|
||||||
|
}
|
||||||
|
else if (isAlive(x, y) && getNeighborCount(x, y) <= 1){
|
||||||
|
setDead(x, y);
|
||||||
|
}
|
||||||
|
else if (isAlive(x, y) && getNeighborCount(x, y) >= 4){
|
||||||
|
setDead(x, y);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
newWorld[x][y] = world[x][y];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getNeighborCount(int x, int y){
|
||||||
|
int aliveNeighborCount = 0;
|
||||||
|
for(int i = -1; i<=1; i++) {
|
||||||
|
for (int j = -1; j<=1; j++) {
|
||||||
|
if(isAlive(x, y))
|
||||||
|
aliveNeighborCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return aliveNeighborCount;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -22,16 +22,49 @@ public class LifeTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void destroyLonelyCell() {
|
public void destroyLonelyCell() {
|
||||||
|
//Arrange: eine lebende Zelle
|
||||||
|
Life l = new Life();
|
||||||
|
l.setAlive(1, 1);
|
||||||
|
|
||||||
|
//Act
|
||||||
|
ILife nextGen = l.nextGeneration();
|
||||||
|
|
||||||
|
//Assert: Zelle sollte jetzt tot sein
|
||||||
|
assertFalse(nextGen.isAlive(1,1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@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(1, 0);
|
||||||
|
|
||||||
|
// Act: Berechnung der Folgegeneration
|
||||||
|
ILife nextGen = l.nextGeneration();
|
||||||
|
|
||||||
|
// Assert: Rasterpunkt mit zwei Nachbarn lebt noch
|
||||||
|
assertTrue(nextGen.isAlive(0, 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void destroyCrowdedCell() {
|
public void destroyCrowdedCell() {
|
||||||
|
// Arrange: Zellblock
|
||||||
|
Life l = new Life();
|
||||||
|
l.setAlive(0, 0);
|
||||||
|
l.setAlive(0, 1);
|
||||||
|
l.setAlive(0, 2);
|
||||||
|
l.setAlive(1, 0);
|
||||||
|
l.setAlive(1, 1);
|
||||||
|
|
||||||
|
// Act: Berechnung der Folgegeneration
|
||||||
|
ILife nextGen = l.nextGeneration();
|
||||||
|
|
||||||
|
// Assert: Rasterpunkt mit vier Nachbarn ist tot
|
||||||
|
assertFalse(nextGen.isAlive(1, 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user