diff --git a/out/production/GameOfLifeAssignment/Life.class b/out/production/GameOfLifeAssignment/Life.class index f9fdfd8..f6a07d8 100644 Binary files a/out/production/GameOfLifeAssignment/Life.class and b/out/production/GameOfLifeAssignment/Life.class differ diff --git a/src/Life.java b/src/Life.java index dada11d..ce74dee 100644 --- a/src/Life.java +++ b/src/Life.java @@ -1,16 +1,33 @@ public class Life implements ILife { - private boolean[][] grid = new boolean[5][5]; // vorläufig 5x5 Raster + private boolean[][] grid = new boolean[10][10]; + + // + // ----- ZUSATZ ----- + // + // Visuelle Darstellung des Spiels in der Konsole, einfach weil ich Lust darauf hatte. + // Mir ist bewusst, dass das Spielfeld nicht dynamisch ist und auch keine Überprüfung stattfindet, + // ob eine Zelle über das Feld hinaus läuft. + // public static void main(String[] args) { - Life l = new Life(new String[] { " ", - " ", - " *** ", - " ", - " " }); - l = (Life) l.nextGeneration(); - } + Life l = new Life(new String[] { + " ", + " * ", + " *** ", + " * ", + " " }); + while (true) { + l.printGrid(); + l = (Life) l.nextGeneration(); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } public Life() { nukeAll(); @@ -24,7 +41,6 @@ public class Life implements ILife { setAlive(x, y); } - @Override public void nukeAll() { for (int y = 0; y < grid.length; y++) { @@ -55,7 +71,7 @@ public class Life implements ILife { for (int y = 0; y < grid.length; y++) { for (int x = 0; x < grid[y].length; x++) { - int aliveNeighbours = countAliveNeighbours(x, y); // Nur einmal berechnen + int aliveNeighbours = countAliveNeighbours(x, y); if (isAlive(x, y) && (aliveNeighbours == 2 || aliveNeighbours == 3)) { next.setAlive(x, y); @@ -91,4 +107,14 @@ public class Life implements ILife { private boolean isValid(int x, int y) { return x >= 0 && x < grid.length && y >= 0 && y < grid[x].length; } -} \ No newline at end of file + + public void printGrid() { + for (int y = 0; y < grid.length; y++) { + for (int x = 0; x < grid[y].length; x++) { + System.out.print(isAlive(x, y) ? "*" : " "); + } + System.out.println(); + } + System.out.println(); // Leerzeile für bessere Lesbarkeit + } +}