Browse Source

LifeTest second step passed

master
Moritz Neumeier 2 years ago
parent
commit
2b8a04fe31

BIN
out/production/GameOfLifeAssignment/Life.class View File


BIN
out/test/GameOfLifeAssignment/LifeTest.class View File


+ 25
- 11
src/Life.java 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){

+ 14
- 4
test/LifeTest.java View File

@@ -5,26 +5,36 @@ 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);
l.setAlive(0, 1);
l.setAlive(0, 2);

// Act: Berechnung der Folgegeneration
l.nextGeneration();

// Assert: Rasterpunkt mit weniger als zwei Nachbarn sollte sterben
assertFalse(l.isAlive(0, 0));
}



Loading…
Cancel
Save