java - How do I deal with out of bounds for Conway's Game of Life? -


currently working on class project trying recreate conway's game of life. here's copy of code, main issue runround() method. have go through , check see how many neighbors each cell has alive(true) or dead(false) , edit new grid accordingly. keep going out of bounds when trying check around edges of grid , tried implement code avoid must missing something. thank advice! still kind of new this!

package programs;  public class gameoflife { private boolean[][] grid; private int time;  public gameoflife() {     grid = new boolean[10][10];     time = 0; }  public gameoflife(int row, int col) {     if (row < 1) {         row = 10;     }     if (col < 1) {         col = 10;     }     grid = new boolean[row][col];     time = 0; }  public boolean[][] getgrid() {     boolean[][] newgrid = new boolean[grid.length][grid[1].length];     (int = 0; < grid.length; i++) {         (int j = 0; j < grid[i].length; j++) {             newgrid[i][j] = grid[i][j];         }     }     return newgrid; }  public int gettime() {     return time; }  public void simplesetup(int[][] array) {     if (gettime() == 0) {         (int row = 0; row < array.length; row++) {             if (array[0].length == 2) {                 if (array[row][0] >= 0 && array[row][1] >= 0 && array[row][0] <= grid[0].length                         && array[row][1] <= grid[0].length) {                      grid[array[row][0]][array[row][1]] = true;                 }             }         }     }  }  public void cleargrid() {     this.time = 0;     (int row = 0; row < grid.length; row++) {         (int col = 0; row < grid[col].length; col++) {             grid[row][col] = false;         }     }  }  public void runround() {     time++;     int live;     boolean[][] newgrid = getgrid();      (int row = 0; row < grid.length; row++) {         (int col = 0; col < grid[col].length; col++) {             live = 0;             if (row - 1 >= 0 || col - 1 >= 0) {                 if(grid[row - 1][col - 1] == true){                     live++;                 }             }             if (row + 1 < grid.length || col + 1 < grid[col].length) {                 if(grid[row + 1][col + 1] == true){                     live++;                 }             }             if (row - 1 >= 0 || col + 1 < grid[col].length) {                 if(grid[row - 1][col + 1] == true ){                     live++;                 }             }             if (row + 1 < grid.length || col - 1 >= 0) {                 if(grid[row + 1][col - 1] == true){                     live++;                 }             }             if (row + 1 < grid.length) {                 if(grid[row + 1][col] == true){                     live++;                 }             }             if (row - 1 >= 0) {                 if(grid[row - 1][col] == true){                     live++;                 }             }             if (col + 1 < grid[col].length) {                 if(grid[row][col + 1] == true){                     live++;                 }             }             if (col - 1 >= 0) {                 if(grid[row][col - 1] == true){                     live++;                 }             }              if (grid[row][col] == true && live == 2 || live == 3) {                 newgrid[row][col] = true;             } else {                 newgrid[row][col] = false;             }          }     }     (int row = 0; row < grid.length; row++) {         (int col = 0; col < grid[col].length; col++) {             live = 0;             if (row - 1 >= 0 || col - 1 >= 0) {                 if(grid[row - 1][col - 1] == false){                 live++;                 }             }              if (row + 1 < grid.length || col + 1 < grid[col].length) {                 if(grid[row + 1][col + 1] == false){                 live++;                 }             }             if (row - 1 >= 0 || col + 1 < grid[col].length) {                 if(grid[row - 1][col + 1] == false){                 live++;                 }             }             if (row + 1 < grid.length || col - 1 >= 0) {                 if(grid[row + 1][col - 1] == false ){                     live++;                 }             }             if (row + 1 < grid.length) {                 if(grid[row + 1][col] == false ){                     live++;                 }             }             if (row - 1 >= 0) {                 if(grid[row - 1][col] == false){                     live++;                 }             }             if (col + 1 < grid[col].length) {                 if(grid[row][col + 1] == false){                     live++;                 }             }             if (col - 1 >= 0) {                 if(grid[row][col - 1] == false){                     live++;                 }             }             if (live == 3) {                 newgrid[row][col] = true;             } else {                 newgrid[row][col] = false;             }          }      }  }  public void rungame(int foo) {     (int poo = 0; poo < foo; poo++) {         runround();     } } } 

the main problem have bounds checks "or" logic: go ahead computation if either row or col in bounds. need have them both in bounds; use and.

if you're tired of that, can similar effect padding entire grid false values, 1 layer on each side. actual cells in range 1 through grid.length+1; row/col 0 , grid.length+2 false.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -