0

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);
            }
        }
    }

}
Ira
  • 3
  • 3
  • 1
    I presume `x-1` or `x+1` could go off the edge (same for `y`). What actually goes wrong? – doctorlove Mar 23 '17 at 16:21
  • it doesn't work, when I press this step method, and I have mistakes during compilation, but I don't know what exactly is wrong – Ira Mar 23 '17 at 16:26
  • Share the error messages from compilation then :-) – doctorlove Mar 23 '17 at 16:28
  • Caused by: java.lang.ArrayIndexOutOfBoundsException: -1 at sample.Controller.countNeighbors(Controller.java:54) at sample.Controller.stepMethod(Controller.java:118) – Ira Mar 23 '17 at 16:29
  • 2
    By "share the error messages" it was intended that you [edit] your question and include the entire error message. Have you [read the error message and tried to understand it](http://stackoverflow.com/q/3988788/2775450)? (BTW, that is not a compilation error: if you had compilation errors you wouldn't have been able to run the application.) – James_D Mar 23 '17 at 16:35
  • sorry, it's my first year of studies and I don't have any experience in this – Ira Mar 23 '17 at 16:38
  • @Ira That's why I provided a link for you to learn how to do it. – James_D Mar 23 '17 at 16:40
  • 1
    BTW: Some of the code can be shortened: `nextGeneration[x][y] = (neighbors == 3);` in the body of the for loop in `stepMethod`. Furthermore all `else{ neighbours+=0; }` can be removed, those statements do not have any effect. Also to count the values you could simply use a loop and subtract the value for the "center" again: `int neighbors = (neighbors[x][y] ? -1 : 0); for(int i = -1; i <= 1; i++) { for (int j=-1; j <= 1; j++) { if (board[x+i][y+j]) { neighbors++;}}}` – fabian Mar 23 '17 at 16:45
  • Sorry, the part for the loop should be `nextGeneration[x][y] = (neighbors == 3 || ((neighbors == 2) && board[x][y]));` – fabian Mar 23 '17 at 19:37

2 Answers2

2

Your error

 java.lang.ArrayIndexOutOfBoundsException: -1 at
 sample.Controller.countNeighbors(Controller.java:54) at
 sample.Controller.stepMethod(Controller.java:118) 

is a runtime error - not compilation error. It says you index (i.e. stuff from [x-1] etc) has gone OutOfBounds.

You need to add even more conditions in the ifs and elses, for example

if (board [x-1][y-1]){

will be a problem is x or y is 0, so

if (x>0 && y>0 && board [x-1][y-1]){

You need to check against the upper bound too further down.

Decide what to do at the edges of the board. Wrap round? Make itthe edge of the world? Up to you.

doctorlove
  • 18,872
  • 2
  • 46
  • 62
0

At your first iteration you'll have x=0 and y=0. So evaluating board[x-1][y-1] will give you a board[-1][-1] which will raise the ArrayOutOfBoundsException.

Alex Ciocan
  • 2,272
  • 14
  • 20
Rafael Costa
  • 163
  • 9