From d0a9f6c7d12eac74ad8693ac1f02ea8aae94bebb Mon Sep 17 00:00:00 2001 From: Oli040404 Date: Wed, 12 Feb 2025 10:40:22 +0100 Subject: [PATCH] Jetzt gehen keine Tests mehr irgendwie --- src/Life.java | 84 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 32 deletions(-) diff --git a/src/Life.java b/src/Life.java index 9522bde..1295fcd 100644 --- a/src/Life.java +++ b/src/Life.java @@ -1,64 +1,80 @@ public class Life implements ILife { - + + private int width; + private int height; + private boolean[][] grid; + public static void main(String[] args) { - Life l = new Life(new String[] { " ", - " ", - " *** ", - " ", - " " }); + Life l = new Life(new String[]{ + " ", + " ", + " *** ", + " ", + " " + }); 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.grid = new boolean[width][height]; nukeAll(); } + // Konstruktor mit Setup-String public Life(String[] setup) { - this(); - for (int y = 0; y < setup.length; y++) - for (int x = 0; x < setup[y].length(); x++) - if (setup[y].charAt(x) != ' ') + this.height = setup.length; + this.width = setup[0].length(); + this.grid = new boolean[width][height]; + + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + if (setup[y].charAt(x) == '*') { setAlive(x, y); + } + } + } } - + // Setzt alle Zellen auf "tot" @Override public void nukeAll() { - // TODO Auto-generated method stub - } + // Setzt eine Zelle auf "lebendig" @Override public void setAlive(int x, int y) { - // TODO Auto-generated method stub - } + // Setzt eine Zelle auf "tot" @Override public void setDead(int x, int y) { - // TODO Auto-generated method stub - } @Override public boolean isAlive(int x, int y) { - // TODO Auto-generated method stub - return false; + return isWithinBounds(x, y) && grid[x][y]; } + // Berechnet die nächste Generation des Spielfelds @Override public ILife nextGeneration() { - Life newLife = new Life(); - + Life newLife = new Life(new String[height]); // Neues Life-Objekt mit gleicher Größe 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.setDead(x, y); + if (neighbors == 2 || neighbors == 3) { + newLife.setAlive(x, y); } else { + newLife.setDead(x, y); + } + } else { + if (neighbors == 3) { newLife.setAlive(x, y); } } @@ -67,14 +83,18 @@ public class Life implements ILife { return newLife; } - private int countAliveNeighbors(int x, int y) { - int count = 0; - for (int dy = -1; dy <= 1; dy++) { - for (int dx = -1; dx <= 1; dx++) { - if (dx == 0 && dy == 0) continue; // Eigene Zelle ignorieren - if (isAlive(x + dx, y + dy)) count++; - } + private int countAliveNeighbors(int x, int y) { + int count = 0; + for (int dy = -1; dy <= 1; dy++) { + for (int dx = -1; dx <= 1; dx++) { + if (dx == 0 && dy == 0) continue; + if (isAlive(x + dx, y + dy)) count++; } - return count; } + return count; } + private boolean isWithinBounds(int x, int y) { + return x >= 0 && x < width && y >= 0 && y < height; + } +} +