From a18048da87d47ade118a1eeb8e9f59d42cb78967 Mon Sep 17 00:00:00 2001 From: Oli040404 Date: Wed, 12 Feb 2025 10:16:24 +0100 Subject: [PATCH] =?UTF-8?q?Um=C3=A4nderung=20der=20nextGeneration=20Method?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Life.java | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/Life.java b/src/Life.java index 43a5058..9522bde 100644 --- a/src/Life.java +++ b/src/Life.java @@ -49,26 +49,23 @@ public class Life implements ILife { @Override public ILife nextGeneration() { - Life newLife = new Life(); + Life newLife = new Life(); - // Prüfe alle Zellen im aktuellen Spielfeld - for (int y = 0; y < height; y++) { - for (int x = 0; x < width; x++) { - int neighbors = countAliveNeighbors(x, y); + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + int neighbors = countAliveNeighbors(x, y); - if (isAlive(x, y)) { - // Regel: Weniger als 2 Nachbarn → Zelle stirbt - if (neighbors < 2) { - newLife.setDead(x, y); - } else { - newLife.setAlive(x, y); - } + if (isAlive(x, y)) { + if (neighbors < 2 || neighbors > 3) { + newLife.setDead(x, y); + } else { + newLife.setAlive(x, y); } } } - return newLife; } - + return newLife; + } private int countAliveNeighbors(int x, int y) { int count = 0; @@ -78,6 +75,6 @@ public class Life implements ILife { if (isAlive(x + dx, y + dy)) count++; } } + return count; } - return null; }