From 1fa9941eedfd9bd21a451300fbec0c3b9798e67f Mon Sep 17 00:00:00 2001 From: Raphaelsav <94864277+Raphaelsav@users.noreply.github.com> Date: Mon, 20 May 2024 17:06:13 +0200 Subject: [PATCH] updated getAliveNeighbours so that it works on the borders --- src/backend/Simulator.java | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/backend/Simulator.java b/src/backend/Simulator.java index d3f47af..1d589dd 100644 --- a/src/backend/Simulator.java +++ b/src/backend/Simulator.java @@ -130,18 +130,23 @@ public class Simulator extends Thread { } public int getAliveNeighbors(int x, int y, int radius) { + + //to compensate for the cell itself being alive or not int aliveNeighbors = -1; if(getCell(x,y) == 0) { - aliveNeighbors++; + aliveNeighbors++; } //for each neighbor for(int i = x-radius; i <= x+radius; i++) { for(int j = y-radius; j <= y+radius; j++) { - if(getCell(i,j) == 1) { //if alive, add 1 to counter + if(i!=-1 && i!=getWidth()+1 && j!=-1 && j!=getHeight()+1) { //if neighbor is not outside the map + if(getCell(i,j) == 1) { //if alive, add 1 to counter aliveNeighbors++; } + } + } } @@ -186,24 +191,23 @@ public class Simulator extends Thread { */ //calculate and set newCells based on cells - //i started from 1 and ended < to ignore the borders - for(int x=1; x < getWidth();x++) { - for(int y=1; y < getHeight(); y++) { + for(int x=0; x <= getWidth();x++) { + for(int y=0; y <= getHeight(); y++) { int status = getCell(x,y); if(status == 1) { - cellDies(x, y); + cellDies(x, y); //check if cell should die }else if(status == 0) { - cellBorns(x, y); + cellBorns(x, y); //check if cell should born } } } //update cells - for(int x=1; x < getWidth();x++) { - for(int y=1; y < getHeight(); y++) { + for(int x=0; x <= getWidth();x++) { + for(int y=0; y <= getHeight(); y++) { int status = getNewCell(x,y); setCell(x, y, status); }