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