diff --git a/src/Life.java b/src/Life.java index 1295fcd..ab76c40 100644 --- a/src/Life.java +++ b/src/Life.java @@ -1,5 +1,4 @@ public class Life implements ILife { - private int width; private int height; private boolean[][] grid; @@ -15,15 +14,13 @@ public class Life implements ILife { l = (Life) l.nextGeneration(); } - // Standard-Konstruktor -> Erstellt ein leeres Spielfeld mit Standardgröße public Life() { - this.width = 5; // Standardbreite - this.height = 5; // Standardhöhe + this.width = 5; + this.height = 5; this.grid = new boolean[width][height]; nukeAll(); } - // Konstruktor mit Setup-String public Life(String[] setup) { this.height = setup.length; this.width = setup[0].length(); @@ -38,40 +35,44 @@ public class Life implements ILife { } } - // Setzt alle Zellen auf "tot" @Override public void nukeAll() { + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + grid[x][y] = false; + } + } } - // Setzt eine Zelle auf "lebendig" @Override public void setAlive(int x, int y) { } - // Setzt eine Zelle auf "tot" @Override public void setDead(int x, int y) { } @Override public boolean isAlive(int x, int y) { - return isWithinBounds(x, y) && grid[x][y]; + return isInBounds(x, y) && grid[x][y]; } - // Berechnet die nächste Generation des Spielfelds @Override public ILife nextGeneration() { - Life newLife = new Life(new String[height]); // Neues Life-Objekt mit gleicher Größe + Life newLife = new Life(); + newLife.width = this.width; + newLife.height = this.height; + newLife.grid = new boolean[width][height]; + for (int y = 0; y < height; y++) { - newLife.grid[y] = new boolean[width]; // Spalten anlegen for (int x = 0; x < width; x++) { int neighbors = countAliveNeighbors(x, y); if (isAlive(x, y)) { - if (neighbors == 2 || neighbors == 3) { - newLife.setAlive(x, y); - } else { + if (neighbors < 2 || neighbors > 3) { newLife.setDead(x, y); + } else { + newLife.setAlive(x, y); } } else { if (neighbors == 3) { @@ -93,8 +94,8 @@ public class Life implements ILife { } return count; } - private boolean isWithinBounds(int x, int y) { + + private boolean isInBounds(int x, int y) { return x >= 0 && x < width && y >= 0 && y < height; } -} - +} \ No newline at end of file diff --git a/test/LifeTest.java b/test/LifeTest.java index 76bd6e9..c34d24e 100644 --- a/test/LifeTest.java +++ b/test/LifeTest.java @@ -2,24 +2,19 @@ import org.junit.Test; import static org.junit.Assert.*; 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 ILife nextGen = l.nextGeneration(); - // Assert: Rasterpunkt mit drei Nachbarn sollte jetzt leben assertTrue(nextGen.isAlive(1, 1)); } - @Test public void destroyLonelyCell() { Life l = new Life(); @@ -30,23 +25,18 @@ public class LifeTest { assertFalse(nextGen.isAlive(2, 2)); } - @Test public void keepAliveCell() { - // Arrange: Eine Zelle mit zwei Nachbarn Life l = new Life(); l.setAlive(1, 1); l.setAlive(1, 2); l.setAlive(2, 1); - // Act: Nächste Generation berechnen ILife nextGen = l.nextGeneration(); - // Assert: Die Zelle bleibt am Leben assertTrue(nextGen.isAlive(1, 1)); } - @Test public void destroyCrowdedCell() { Life l = new Life(); @@ -60,5 +50,4 @@ public class LifeTest { assertFalse(nextGen.isAlive(1, 1)); } -} - +} \ No newline at end of file