From 3bbf66ea1f56958fdf3a02e8e986509fe4c11be3 Mon Sep 17 00:00:00 2001 From: Saskia Date: Sat, 15 Feb 2025 16:45:25 +0100 Subject: [PATCH] =?UTF-8?q?Zwischen-Commit=2003=20(verbesserung=20countNei?= =?UTF-8?q?ghbours+=20Test=201=20createNewCell=20gr=C3=BCn)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Life.java | 36 ++++++++++++++++++------------------ test/LifeTestNukeAll.java | 16 ++++++++++++++++ 2 files changed, 34 insertions(+), 18 deletions(-) create mode 100644 test/LifeTestNukeAll.java diff --git a/src/Life.java b/src/Life.java index bea4087..18a53b3 100644 --- a/src/Life.java +++ b/src/Life.java @@ -68,28 +68,28 @@ public class Life implements ILife { } } } - return null; + return nextLife; } - private int countNeigbours(int x, int y){ + + private int countNeigbours(int x, int y) { int count = 0; - if(isAlive(x-1, y-1)){ - count ++; - }else if(isAlive(x-1, y)){ - count ++; - }else if(isAlive(x-1, y+1)){ - count++; - }else if(isAlive(x, y-1)){ - count++; - }else if(isAlive(x, y+1)){ - count++; - }else if(isAlive(x+1, y-1)){ - count++; - }else if(isAlive(x+1, y)){ - count++; - }else if(isAlive(x+1, y+1)){ - count++; + + for (int dx = -1; dx <= 1; dx++) { + for (int dy = -1; dy <= 1; dy++) { + if (dx == 0 && dy == 0) continue; + + int nx = x + dx; + int ny = y + dy; + + if (nx >= 0 && nx < GRIDSIZE && ny >= 0 && ny < GRIDSIZE) { + if (isAlive(nx, ny)) { + count++; + } + } + } } return count; } + } \ No newline at end of file diff --git a/test/LifeTestNukeAll.java b/test/LifeTestNukeAll.java new file mode 100644 index 0000000..eb25c5e --- /dev/null +++ b/test/LifeTestNukeAll.java @@ -0,0 +1,16 @@ +import junit.framework.TestCase; + +public class LifeTestNukeAll extends TestCase { + + public static void main(String[] args) { + Life l = new Life(new String[] { " ", + " ", + " *** ", + " ", + " " }); + l = (Life) l.nextGeneration(); + } + + public void testNukeAll() { + } +} \ No newline at end of file