0

生命游戏——我不太明白如何正确检查邻居..

谁能告诉我为什么我的右边框和上边框不能正常工作,即放置在其中的任何内容都被删除了?例如,如果我放置了一个撞到右边框的滑翔机,它会在最后一列之前创建一个正方形,而不是创建一个包含最后一列的正方形,如果我放置一个撞到顶部的滑翔机,它会创建一个包含顶行的正方形但它也继续到最后一行。撞到左边框或下边框的滑翔机会创建一个正方形,包括左列或底行(分别)。这是产生影响的算法......我使用的是 75x75 的网格,这就是为什么有常量

    public class Grid{

    private int columns;
    private int rows;
    private Cell[][] grid;

    public Grid(int column, int row){
      this.columns = column;
      this.rows = row;
        grid = new Cell[row][column];
      for (int r = 0; r < row; r++){
        for (int c = 0; c < column; c++){
         grid[r][c] = new Cell(r,c);
       }
     }
    }

  public boolean getCell(int r, int c){
    return this.grid[r][c].isCellAlive();
  }

  public void giveCellLife(int r, int c){
   this.grid[r][c].setCell(true);
  }

  public void killCell(int r, int c){
    this.grid[r][c].setCell(false);
  }

  public void nextGridState(){
    cellNeighbors();
    for (int r = 0; r < rows; r++){
      for (int c = 0; c < columns; c++){
        if (grid[r][c].isCellAlive()  && grid[r][c].getNeighbors() < 2){
          grid[r][c].setCell(false);
        }
        if (grid[r][c].isCellAlive()  && grid[r][c].getNeighbors() > 3){
          grid[r][c].setCell(false);
        }
        if (!grid[r][c].isCellAlive()  && grid[r][c].getNeighbors() == 3){
          grid[r][c].setCell(true);
        }
      }
    }
    reset();
  }

  public void reset(){            
    for (int r = 0; r < rows; r++){
      for (int c = 0; c < columns; c++){ 
        grid[r][c].removeNeighbors();
      } 
    }
  }





  public void cellNeighbors(){
    for (int r = 0; r < rows; r++){
     for (int c = 0; c < columns; c++){ 
       try {
         if (grid[r-1][c+1].isCellAlive() == true){
           grid[r][c].addNeighbor();
         }
         if (grid[r][c+1].isCellAlive() == true){
           grid[r][c].addNeighbor();
         }
         if (grid[r+1][c+1].isCellAlive() == true){
           grid[r][c].addNeighbor();
         }
         if (grid[r-1][c].isCellAlive() == true){
           grid[r][c].addNeighbor();
         }
         if (grid[r+1][c].isCellAlive() == true){
           grid[r][c].addNeighbor();
         }
         if (grid[r-1][c-1].isCellAlive() == true){
           grid[r][c].addNeighbor();
         }
         if (grid[r][c-1].isCellAlive() == true){
          grid[r][c].addNeighbor();
         }
         if (grid[r+1][c-1].isCellAlive() == true){
           grid[r][c].addNeighbor();
         }
       } catch (ArrayIndexOutOfBoundsException e){
       }

       if (r == 0){
         try {
         if (grid[r][c+1].isCellAlive() == true){
           grid[r][c].addNeighbor();
         }
         if (grid[r][c-1].isCellAlive() == true){
           grid[r][c].addNeighbor();
         }
         if (grid[r+74][c+1].isCellAlive() == true){
           grid[r][c].addNeighbor();
         }
         if (grid[r+74][c-1].isCellAlive() == true){
           grid[r][c].addNeighbor();
         }
         if (grid[r+74][c].isCellAlive() == true){
           grid[r][c].addNeighbor();
         }
         if (grid[r+1][c+1].isCellAlive() == true){
           grid[r][c].addNeighbor();
         }
         if (grid[r+1][c].isCellAlive() == true){
          grid[r][c].addNeighbor();
         }
         if (grid[r+1][c-1].isCellAlive() == true){
           grid[r][c].addNeighbor();
         }
       } catch (ArrayIndexOutOfBoundsException e){
       }
       }

       if (c == 74){
         try {
         if (grid[r-1][c-1].isCellAlive() == true){
           grid[r][c].addNeighbor();
         }
         if (grid[r][c-1].isCellAlive() == true){
           grid[r][c].addNeighbor();
         }
         if (grid[r+1][c-1].isCellAlive() == true){
           grid[r][c].addNeighbor();
         }
         if (grid[r-1][c].isCellAlive() == true){
           grid[r][c].addNeighbor();
         }
         if (grid[r+1][c].isCellAlive() == true){
           grid[r][c].addNeighbor();
         }
         if (grid[r-1][0].isCellAlive() == true){
           grid[r][c].addNeighbor();
         }
         if (grid[r][0].isCellAlive() == true){
          grid[r][c].addNeighbor();
         }
         if (grid[r+1][0].isCellAlive() == true){
           grid[r][c].addNeighbor();
         }
       } catch (ArrayIndexOutOfBoundsException e){
       }
       }


     }
    }
  }
}   

public class Cell{

  private int x;
  private int y;
  public int neighbors;
  private boolean living;

  public Cell(int x, int y){
    this.x = x;
    this.y = y;
    neighbors = 0;
    living = false;
  }

  public int getNeighbors(){
    return this.neighbors;
  }

  public void addNeighbor(){
    neighbors++;
  }

  public void removeNeighbors(){
    neighbors = 0;
  }

  public boolean isCellAlive(){
    return this.living;
  }

  public void setCell(boolean isCellAlive){
    this.living = isCellAlive;
  }



}

我已经看了好几个小时了,另一双眼睛真的很好=)谢谢

编辑**这里是问题的屏幕截图......

在此处输入图像描述

4

0 回答 0