From 12876e151de909eded81e2c2992f33abf5cf09f0 Mon Sep 17 00:00:00 2001 From: JDenu Date: Wed, 22 Dec 2021 16:55:43 +0100 Subject: [PATCH] 4 --- src/Life.java | 148 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 140 insertions(+), 8 deletions(-) diff --git a/src/Life.java b/src/Life.java index 84ab855..6c52a5d 100644 --- a/src/Life.java +++ b/src/Life.java @@ -1,11 +1,16 @@ +import java.util.Arrays; + public class Life implements ILife { - + + static String[] welt = new String[]{ + " ", + " ", + " *** ", + " ", + " "}; + public static void main(String[] args) { - Life l = new Life(new String[] { " ", - " ", - " *** ", - " ", - " " }); + Life l = new Life(welt); l = (Life) l.nextGeneration(); } @@ -20,36 +25,163 @@ public class Life implements ILife { for (int x = 0; x < setup[y].length(); x++) if (setup[y].charAt(x) != ' ') setAlive(x, y); + printWelt(); } + public void printWelt() { + for (int i = 0; i < welt.length; i++) + System.out.println(Arrays.toString(new String[]{welt[i]})); + } @Override public void nukeAll() { // TODO Auto-generated method stub + for (int y = 0; y < welt.length; y++) + for (int x = 0; x < welt[y].length(); x++) + setDead(x, y); } @Override public void setAlive(int x, int y) { // TODO Auto-generated method stub + welt[y] = welt[y].substring(0, x) + '*' + welt[y].substring(x + 1); } @Override public void setDead(int x, int y) { // TODO Auto-generated method stub + welt[y] = welt[y].substring(0, x) + ' ' + welt[y].substring(x + 1); } @Override public boolean isAlive(int x, int y) { // TODO Auto-generated method stub - return false; + + if (welt[y].charAt(x) == '*') + return true; + else + return false; } @Override public ILife nextGeneration() { // TODO Auto-generated method stub - return null; + + String[] neueWelt = new String[]{ + " ", + " ", + " ", + " ", + " "}; + + //Zellen einzeln betrachten + for (int y = 0; y < welt.length; y++) + for (int x = 0; x < welt[y].length(); x++) { + + //anzahl der lebenden nachbarn + int nachbarn = lebendeNachbarn(y, x); + + //neue Zelle + //TODO fehler wenn x = 0 + if (nachbarn == 3) { + if (x > 0 && x < welt[0].length() - 1) { + neueWelt[y] = neueWelt[y].substring(0, x - 1) + '*' + neueWelt[y].substring(x); + } else if (x == 0) { + neueWelt[y] = '*' + neueWelt[y].substring(x); + } else { + neueWelt[y] = neueWelt[y].substring(0, x - 1) + '*'; + } + } + + //eine Zelle stirbt an überpopulation oder einsamkeit + //TODO fehler wenn x = 0 + if (nachbarn < 2 || nachbarn > 3) { + if (x > 0 && x < welt[0].length() - 1) { + System.out.println(welt[0].length()); + System.out.print(nachbarn); + System.out.print(", " + x); + System.out.println(", " + y); + neueWelt[y] = neueWelt[y].substring(0, x - 1) + ' ' + neueWelt[y].substring(x); + } else if (x == 0) { + neueWelt[y] = ' ' + neueWelt[y].substring(x); + } else { + neueWelt[y] = neueWelt[y].substring(0, x - 1) + ' '; + } + } + + //eine Zelle bleibt am Leben + //TODO fehler wenn x = 0 + if ((nachbarn == 2) && (welt[y].charAt(x) == '*')) { + if (x > 0 && x < welt[0].length() - 1) { + neueWelt[y] = neueWelt[y].substring(0, x - 1) + '*' + neueWelt[y].substring(x); + } else if (x == 0) { + neueWelt[y] = '*' + neueWelt[y].substring(x); + } else { + neueWelt[y] = neueWelt[y].substring(0, x - 1) + '*'; + } + } + } + return new Life(neueWelt); + } + + public int lebendeNachbarn(int y, int x) { // 1, 1 + + int nachbarn = 0; + + //linke nachbars-spalte + if (x > 0) { + if (y > 0) { + if (welt[y - 1].charAt(x - 1) == '*') { + nachbarn++; + } + } + if (welt[y].charAt(x - 1) == '*') { + nachbarn++; + } + if (y < welt.length - 1) { + if (welt[y + 1].charAt(x - 1) == '*') { + nachbarn++; + } + } + } + + //rechte nachbars-spalte + if (x < welt[0].length() - 1) { + if (y > 0) { + if (welt[y - 1].charAt(x + 1) == '*') { + nachbarn++; + } + } + if (welt[y].charAt(x + 1) == '*') { + nachbarn++; + } + if (y < welt.length - 1) { + if (welt[y + 1].charAt(x + 1) == '*') { + nachbarn++; + } + } + } + + // mittlere Spalte + if (y > 0) { + if (welt[y - 1].charAt(x) == '*') { + nachbarn++; + } + } + if (y < welt.length - 1) { + if (welt[y + 1].charAt(x) == '*') { + nachbarn++; + } + } + + return nachbarn; + } + + @Override + public String toString() { + return super.toString(); } } \ No newline at end of file