LifeTest second step passed

This commit is contained in:
Moritz Neumeier 2021-12-17 11:22:08 +01:00
parent 2e9764cb45
commit 2b8a04fe31
4 changed files with 44 additions and 20 deletions

View File

@ -15,7 +15,7 @@ public class Life implements ILife {
public Life() {
//nukeAll();
nukeAll();
}
public Life(String[] setup) {
@ -31,7 +31,7 @@ public class Life implements ILife {
printTable(board);
}
private void printTable(String[] board) { //works
private void printTable(String[] board) {
for(int i = 0; i < board.length; i++) System.out.println(Arrays.toString(new String[]{board[i]}));
System.out.println("________");
}
@ -49,21 +49,20 @@ public class Life implements ILife {
}
@Override
public void setAlive(int x, int y) { //works
public void setAlive(int x, int y) {
// TODO Auto-generated method stub
board[y] = board[y].substring(0, x) + '*' + board[y].substring(x+1);
}
@Override
public void setDead(int x, int y) { //works
public void setDead(int x, int y) {
// TODO Auto-generated method stub
board[y] = board[y].substring(0, x) + ' ' + board[y].substring(x+1);
}
@Override
public boolean isAlive(int x, int y) { //works
//char c = ;
if(board[y].charAt(x) == '*'){ //-->Befehl falsch immer false!!!
public boolean isAlive(int x, int y) {
if(board[y].charAt(x) == '*'){
return true;
}
else{
@ -73,23 +72,38 @@ public class Life implements ILife {
@Override
public ILife nextGeneration() {
System.out.println("next Generation");
//Problem mehrere steps auf einmal da gesetzer * bei nachfolgendem durchlauf mitberücksichtigt wird
int alive;
String[] nextBoard = new String[]{ " ",
" ",
" ",
" ",
" " };
for (int y = 0; y < board.length; y++){
for (int x = 0; x < board[y].length(); x++){
alive=aliveNeighbours(x,y); //Problem immer 0
alive=aliveNeighbours(x,y);
//A new Cell is born
if(!isAlive(x,y) && alive == 3) {
setAlive(x, y);
nextBoard[y] = nextBoard[y].substring(0, x) + '*' + nextBoard[y].substring(x+1);
}
//Cell is lonely and dies
else if(isAlive(x,y) && alive < 2){
nextBoard[y] = nextBoard[y].substring(0, x) + ' ' + nextBoard[y].substring(x+1);
}
else{
nextBoard[y] = nextBoard[y].substring(0, x) + board[y].charAt(x) + nextBoard[y].substring(x+1);
}
}
}
board = nextBoard;
System.out.println("next Generation");
printTable(board);
return null;
}
private int aliveNeighbours(int x, int y) { //-->Problem wahrscheinlich Befehl substring siehe isAlive
private int aliveNeighbours(int x, int y) {
int neighbours = 0;
if(x>0 && y>0){

View File

@ -5,8 +5,25 @@ public class LifeTest {
@Test
public void createNewCell() {
// Arrange: drei lebende Zellen
Life l = new Life();
l.setAlive(0, 0);
l.setAlive(0, 1);
l.setAlive(0, 2);
// Act: Berechnung der Folgegeneration
l.nextGeneration();
// Assert: Rasterpunkt mit drei Nachbarn sollte jetzt leben
assertTrue(l.isAlive(1, 1));
}
@Test
public void destroyLonelyCell() {
//Lebende Zellen mit weniger als zwei lebenden Nachbarn sterben in der Folgegeneration an Einsamkeit.
// Arrange: drei lebende Zellen
Life l = new Life();
l.setAlive(0, 0);
@ -16,15 +33,8 @@ public class LifeTest {
// Act: Berechnung der Folgegeneration
l.nextGeneration();
// Assert: Rasterpunkt mit drei Nachbarn sollte jetzt leben
assertTrue(l.isAlive(1, 1));
}
@Test
public void destroyLonelyCell() {
// Assert: Rasterpunkt mit weniger als zwei Nachbarn sollte sterben
assertFalse(l.isAlive(0, 0));
}