java—如何简化计算一个单元格的邻居数的生命游戏方法?

huwehgph  于 2021-06-30  发布在  Java
关注(0)|答案(3)|浏览(340)

此方法检查单元格是否是边缘单元格(在边界上),然后计算它有多少个活动的邻居。然后返回该值并用于确定该单元是生存还是死亡。这需要很大的空间,看起来很难看。有没有一种方法可以让我用更少的台词来完成这一切?

// takes in board and checks a cell's neighbors, returning the number of living neighbors
// checks if a cell is on a border, therefore causing it to have less neigbors
public static int checkNeighbors(int[][] board, int i, int j)
{
    int count = 0;   // live neighbors of cell will be added here

    if(i - 1 < 0 && j - 1 < 0)   // in top left corner, only three neighbors
    {
        if(board[i][j + 1] == 1)
            count++;
        if(board[i + 1][j] == 1)
            count++;
        if(board[i + 1][j + 1] == 1)
            count++;
    }
    else if(i - 1 < 0 && j + 1 > width - 1)   // in top right corner, only three neighbors
    {
        if(board[i][j - 1] == 1)
            count++;
        if(board[i + 1][j] == 1)
            count++;
        if(board[i + 1][j - 1] == 1)
            count++;
    }
    else if(i + 1 > height - 1 && j - 1 < 0)   // in bottom left corner, only three neighbors
    {
        if(board[i][j + 1] == 1)
            count++;
        if(board[i - 1][j] == 1)
            count++;
        if(board[i - 1][j + 1] == 1)
            count++;
    }
    else if(i + 1 > height - 1 && j + 1 > width - 1)   // in bottom right corner, only three neighbors
    {
        if(board[i][j - 1] == 1)
            count++;
        if(board[i - 1][j] == 1)
            count++;
        if(board[i - 1][j - 1] == 1)
            count++;
    }
    else if(j - 1 < 0)   // on left border, only five neighbors
    {
        if(board[i - 1][j] == 1)
            count++;
        if(board[i - 1][j + 1] == 1)
            count++;
        if(board[i][j + 1] == 1)
            count++;
        if(board[i + 1][j + 1] == 1)
            count++;
        if(board[i + 1][j] == 1)
            count++;
    }
    else if(j + 1 > width - 1)   // on right border, only five neighbors
    {
        if(board[i - 1][j] == 1)
            count++;
        if(board[i - 1][j - 1] == 1)
            count++;
        if(board[i][j - 1] == 1)
            count++;
        if(board[i + 1][j - 1] == 1)
            count++;
        if(board[i + 1][j] == 1)
            count++;
    }
    else if(i - 1 < 0)   // on top border, only five neighbors
    {
        if(board[i][j - 1] == 1)
            count++;
        if(board[i - 1][j - 1] == 1)
            count++;
        if(board[i - 1][j] == 1)
            count++;
        if(board[i - 1][j + 1] == 1)
            count++;
        if(board[i][j + 1] == 1)
            count++;
    }
    else if(i + 1 > height - 1)   // on bottom border, only five neighbors
    {
        if(board[i][j - 1] == 1)
            count++;
        if(board[i + 1][j - 1] == 1)
            count++;
        if(board[i + 1][j] == 1)
            count++;
        if(board[i + 1][j + 1] == 1)
            count++;
        if(board[i][j + 1] == 1)
            count++;
    }
    else   // cell is not on any border, has full eight neighbors
    {
        if(board[i - 1][j - 1] == 1)
            count++;
        if(board[i - 1][j] == 1)
            count++;
        if(board[i - 1][j + 1] == 1)
            count++;
        if(board[i][j - 1] == 1)
            count++;
        if(board[i][j + 1] == 1)
            count++;
        if(board[i + 1][j - 1] == 1)
            count++;
        if(board[i + 1][j] == 1)
            count++;
        if(board[i + 1][j + 1] == 1)
            count++;
    }
    return count;
}

stackoverflow告诉我添加更多细节,因为我的帖子大部分是代码。我不认为需要更多的细节,所以我在这里键入这个只是为了摆脱这个错误。

0aydgbwb

0aydgbwb1#

您可以将支票移到一个地方:

public static int countNeighbours(int[][] board, int i, int j) {
    int count = 0;
    for (int x = -1; x <= 1; x++) {
        for (int y = -1; y <= 1; y++) {
            if ((x != 0 || y != 0) && checkCell(board, i + x, j + y)) {
                ++count;
            }
        }
    }
    return count;
}

public static boolean checkCell(int[][] board,int x, int y) {
    return (x >= 0 && y >= 0 && x < width && y < height && board[x][y] == 1);
}

你可以用流做一些更短的事情,但是如果你不熟悉它们,这就更清楚了。

j2cgzkjk

j2cgzkjk2#

检查索引是否超出范围或等于您的值 i,j 参数,如果是,则跳过它们。否则,请将它们添加到 counter .

public static int checkNeighbors(int[][] board, int i, int j) {
    int counter = 0;
    for (int row = i - 1; row <= i + 1; row++) {
        if (row < 0 || row >= board.length) {
            continue;
        }
        for (int column = j - 1; column <= j + 1; column++) {
            if (column < 0 || column >= board[row].length || (row == i && column == j)) {
                continue;
            }
            counter += board[row][column];
        }
    }
    return counter;
}
z3yyvxxp

z3yyvxxp3#

这个怎么样?

for (int y = i-1; y <= i+1; i1++){//loop around point on outer array (y-axis on classic 2D coordinate system)
    for (int x = j-1; x <= j+1; x++) {//loop around point on inner array (x-axis on classic 2D coordinate system)
        if (i==y&&j==x) {
            continue;//same case-->skip
        }
        //option 1
        if(y<0||y>board.length||x<0||x>board[x].length){
            continue;//outside-->skip
        }
        //option 2
//      int otherCellX=y%board.length;//if > length -->start again on other side
//      int otherCellY=x%board[0].length;
//      if (y<0) {//if < 0-->go to end
//          otherCellX+=board.length;
//      }
//      if (x<0) {
//          otherCellY+=board[otherCellX].length;
//      }
        if (board[otherCellX][otherCellY]==1) {//if alive, increment count
            count++;
        }
    }
}

这将在当前单元格周围的点上循环。
它测试是同一个单元还是板外的单元。如果不是,并且细胞是活的,它将在计数中加1。
在选项2中,如果它在电路板之外,则取另一侧的数字。

相关问题