First two tests working
This commit is contained in:
parent
09a995eccf
commit
9c236f1514
@ -1,5 +1,7 @@
|
|||||||
public class Life implements ILife {
|
public class Life implements ILife {
|
||||||
|
|
||||||
|
boolean[][] cells = new boolean[15][15];
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Life l = new Life(new String[] { " ",
|
Life l = new Life(new String[] { " ",
|
||||||
" ",
|
" ",
|
||||||
@ -26,30 +28,75 @@ public class Life implements ILife {
|
|||||||
@Override
|
@Override
|
||||||
public void nukeAll() {
|
public void nukeAll() {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
|
for (int i = 0; i < 15; i++) {
|
||||||
|
for (int j = 0; j < 15; j++) {
|
||||||
|
cells[i][j] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@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
|
||||||
|
cells[x][y] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@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
|
||||||
|
cells[x][y] = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@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
|
||||||
return false;
|
return cells[x][y];
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ILife nextGeneration() {
|
public ILife nextGeneration() {
|
||||||
// TODO Auto-generated method stub
|
// TODO Auto-generated method stub
|
||||||
return null;
|
boolean[][] nextGen = new boolean[15][15];
|
||||||
|
|
||||||
|
for (int y = 0; y < 15; y++) {
|
||||||
|
|
||||||
|
for (int x = 0; x < 15; x++) {
|
||||||
|
|
||||||
|
int aliveNeighbours = countNeighbourCells(x, y);
|
||||||
|
if (isAlive(x, y)) {
|
||||||
|
|
||||||
|
if (aliveNeighbours == 2 || aliveNeighbours == 3) {
|
||||||
|
nextGen[x][y] = true;
|
||||||
|
} else {
|
||||||
|
nextGen[x][y] = false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (aliveNeighbours == 3) {
|
||||||
|
nextGen[x][y] = true;
|
||||||
|
} else {
|
||||||
|
nextGen[x][y] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cells = nextGen;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int countNeighbourCells(int x , int y){
|
||||||
|
int counter = 0;
|
||||||
|
|
||||||
|
if((x-1) >=0 && (y-1) >=0 && isAlive(x-1,y-1)) counter++;
|
||||||
|
if((y-1) >=0 && isAlive(x,y-1)) counter++;
|
||||||
|
if((x+1) < 15 && (y-1) >=0 && isAlive(x+1,y-1)) counter++;
|
||||||
|
if((x-1) >=0&& isAlive(x-1,y)) counter++;
|
||||||
|
if((x+1) < 15 && isAlive(x+1,y)) counter++;
|
||||||
|
if((x-1) >=0 && (y+1) < 15 && isAlive(x-1,y+1)) counter++;
|
||||||
|
|
||||||
|
if((y+1) <15 && isAlive(x,y+1)) counter++;
|
||||||
|
|
||||||
|
if((x+1) <15 && (y+1) <15 && isAlive(x+1,y+1)) counter++;
|
||||||
|
|
||||||
|
return counter;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -22,6 +22,18 @@ 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);
|
||||||
|
|
||||||
|
// Act: Berechnung der Folgegeneration
|
||||||
|
ILife nextGen = l.nextGeneration();
|
||||||
|
|
||||||
|
// Assert: Rasterpunkt mit drei Nachbarn sollte jetzt leben
|
||||||
|
assertTrue(!nextGen.isAlive(1, 1));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user