I know that it was many questions about game of life, but still I can't understand how to write this method correctly in javafx. here is my code which doesn't work, because I don't understand how to implement algorithm for counting neighbors.
public void stepMethod(ActionEvent event){
for (int x = 0; x < cellSize; x++){
for (int y = 0; y < cellSize; y++){
int neighbours = countNeighbors(x, y);
nextGeneration[x][y] = board [x][y];
nextGeneration[x][y] = (neighbours == 3) ? true: nextGeneration[x][y];
nextGeneration[x][y] = ((neighbours < 2) || (neighbours > 3)) ? false : nextGeneration[x][y];
}
}
draw();
}
public int countNeighbors(int x, int y){
int neighbours = 0;
if (board [x-1][y-1]){
neighbours+=1;
}else{
neighbours+=0;
}
if (board[x][y-1]){
neighbours+=1;
}else{
neighbours+=0;
}
if (board[x+1][y-1]){
neighbours+=1;
}else{
neighbours+=0;
}
if (board[x-1][y]){
neighbours+=1;
}else{
neighbours+=0;
}
if (board[x+1][y]){
neighbours+=1;
}else{
neighbours+=0;
}
if (board[x-1][y+1]){
neighbours+=1;
}else{
neighbours+=0;
}
if (board[x][y+1]){
neighbours+=1;
}else{
neighbours+=0;
}
if (board[x+1][y+1]){
neighbours+=1;
}else{
neighbours+=0;
}
if(board[x][y]){
neighbours--;
}
return neighbours;
}
and here is my draw method
public void draw(){
initGraphics();
for(int x = 0; x < cellSize; x++){
for(int y = 0; y < cellSize; y++){
if(board[x][y] ){
gc.setFill(Color.CHOCOLATE);
gc.fillOval(x*cellSize,y*cellSize,cellSize,cellSize);
}
}
}
}