diff --git a/out/production/GameOfLifeAssignment/Life.class b/out/production/GameOfLifeAssignment/Life.class index 0f85de0..7ed1173 100644 Binary files a/out/production/GameOfLifeAssignment/Life.class and b/out/production/GameOfLifeAssignment/Life.class differ diff --git a/out/test/GameOfLifeAssignment/LifeTest.class b/out/test/GameOfLifeAssignment/LifeTest.class index 6684dee..cafc26e 100644 Binary files a/out/test/GameOfLifeAssignment/LifeTest.class and b/out/test/GameOfLifeAssignment/LifeTest.class differ diff --git a/src/Life.java b/src/Life.java index 19510bc..a2c2759 100644 --- a/src/Life.java +++ b/src/Life.java @@ -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){ diff --git a/test/LifeTest.java b/test/LifeTest.java index fe8be53..ba3d9e7 100644 --- a/test/LifeTest.java +++ b/test/LifeTest.java @@ -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)); }