Game of life Oscillators and Spaceships not working

Lucifer Uchiha :

Hi so I am currently working on a game of life with javafx canvas. However there seems to be a bug in my algorithm. The still lifes are working however the rest is not, the patterns like the glider aren't moving the way they should. Im using a 2d int array, ALIVE is 1 and DEAD is 0. Here is my algorithm:

    private void checkRules() {
        int[][] newBoard = board;
        int amountOfAliveNeighbours;
        for (int y = 0; y < board.length; y++) {
            for (int x = 0; x < board[y].length; x++) {
                amountOfAliveNeighbours = getAmountOfAliveNeighbours(x, y);
                if (board[y][x] == ALIVE) {
                    if (amountOfAliveNeighbours == 2 || amountOfAliveNeighbours == 3) {
                        newBoard[y][x] = ALIVE;
                    }else{
                        newBoard[y][x] = DEAD;
                    }
                } else if (board[y][x] == DEAD){
                    if (amountOfAliveNeighbours == 3) {
                        newBoard[y][x] = ALIVE;
                    }else{
                        newBoard[y][x] = DEAD;
                    }
                }
            }
        }
        board = newBoard;
    }

    private int getAmountOfAliveNeighbours(int x, int y) {
        int neighbours = 0;
        // top left
        if (x - 1 >= 0 && y - 1 >= 0) {
            if (board[y - 1][x - 1] == ALIVE)
                neighbours++;
        }
        // top center
        if (y - 1 >= 0) {
            if (board[y - 1][x] == ALIVE)
                neighbours++;
        }
        // top right
        if (x + 1 < board[0].length && y - 1 >= 0) {
            if (board[y - 1][x + 1] == ALIVE)
                neighbours++;
        }
        // middle left
        if (x - 1 >= 0) {
            if (board[y][x - 1] == ALIVE)
                neighbours++;
        }
        // middle right
        if (x + 1 < board[0].length) {
            if (board[y][x + 1] == ALIVE)
                neighbours++;
        }
        // bottom left
        if (x - 1 >= 0 && y + 1 < board.length) {
            if (board[y + 1][x - 1] == ALIVE)
                neighbours++;
        }
        // bottom center
        if (y + 1 < board.length) {
            if (board[y + 1][x] == ALIVE)
                neighbours++;
        }
        // bottom right
        if (x + 1 < board[0].length && y + 1 < board.length) {
            if (board[y + 1][x + 1] == ALIVE)
                neighbours++;
        }
        return neighbours;
    }
Alex :

Allocate the memory for the temporary board like this:

int[][] newBoard = new int[board.length][board[0].length];

I would suggest to refactor calculation of neighbours:

  private int getAmountOfAliveNeighbours(int x, int y) {
    int neighbours = 0;
    for (int dx = -1; dx <= 1; dx++) {
      for (int dy = -1; dy <= 1; dy++) {
        if ((dx !=0 || dy != 0) && isAlive(x + dx, y + dy)) {
          neighbours++;
        }
      }
    }
    return neighbours;
  }

  private boolean isAlive(int x, int y) {
    return (x >= 0) && (x < board.length) &&
        (y >= 0) && (y < board[0].length) &&
        (board[x][y] == ALIVE);
  }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=216067&siteId=1