From f45ef43a7812ee8cb4af8f4baa7b3bef9d022be2 Mon Sep 17 00:00:00 2001 From: Saskia Date: Sat, 15 Feb 2025 16:24:12 +0100 Subject: [PATCH] Zwischen-Commit 02 (nextGeneration+ isAlive fertig) --- src/Life.java | 36 +++++++++++++++++++++++++++++++++--- test/LifeTest.java | 1 + 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/Life.java b/src/Life.java index b561cf9..bea4087 100644 --- a/src/Life.java +++ b/src/Life.java @@ -46,8 +46,7 @@ public class Life implements ILife { @Override public boolean isAlive(int x, int y) { - // TODO Auto-generated method stub - return false; + return grid[x][y]; } @Override @@ -57,9 +56,40 @@ public class Life implements ILife { for(int y = 0; y < GRIDSIZE; y++){ for(int x = 0; x < GRIDSIZE; x++){ - + int neighborCount = countNeigbours(x, y); + if(isAlive(x, y)){ + if(neighborCount == 2 || neighborCount == 3){ + nextLife.setAlive(x, y); + } + }else{ + if(neighborCount == 3){ + nextLife.setAlive(x, y); + } + } } } return null; } + + 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++; + } + return count; + } } \ No newline at end of file diff --git a/test/LifeTest.java b/test/LifeTest.java index 3940e66..346fbd3 100644 --- a/test/LifeTest.java +++ b/test/LifeTest.java @@ -22,6 +22,7 @@ public class LifeTest { @Test public void destroyLonelyCell() { + }