生命游戏Java版 - 使用二维数组和for循环计算邻居数量

3

我正在使用Eclipse和@Test用例编写康威生命游戏程序。除了邻居计数方法之外,我的所有方法都通过了测试。我看到了一些使用for循环的邻居计数方法,但不知道为什么它在我的代码中无法运行。

我正在尝试使用for循环来定位2D数组中的邻居单元格,并且在计算完邻居数量后更新新社会时遇到了麻烦。如果有人可以查看我的代码并找出方法中的错误,我将不胜感激。提前致谢。如果其他方法中存在错误影响neighborCount(),我已经附上了所有代码。

    public class GameOfLife {

    private int theRows;
    private int theCols;
    private char[][] society;


    public GameOfLife(int rows, int cols) {
        // Complete this method.
        society = new char[rows][cols];
        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < cols; c++) {
                society[r][c] = ' ';
            }
        }
        theRows = rows;
        theCols = cols;
    }

    public int numberOfRows() {

        return theRows;
    }


    public int numberOfColumns() {

        return theCols;
    }

    public void growCellAt(int row, int col) {
        // Complete this method
        for (int r = 0; r < society.length; r++) {
            for (int c = 0; c < society[r].length; c++) {
                society[r][c] = 'o';
            }
        }

    }

    public boolean cellAt(int row, int col) {
                if (society[row][col] == 'o') {
                    return true;
                } else { 
                    return false;
                }

        }

    @Override
    public String toString() {
        String res = "";
        for (int r = 0; r < society.length; r++) {
            for (int c = 0; c < society[r].length; c++) 
                res = res + society[r][c];

        }
        return res;
    }

    public int neighborCount(int row, int col) {

        int count = 0;
        for(int i = row - 1; i <= row + 1; i++) {
            if (i >= 0 && i >= society.length)
                for(int j = col - 1; j <= col + 1; j++) 
                    if (j >= 0 && j >= society[i].length) 
                        if (i != row || j != col) 
                            if (society[i][j] == 'o') 
                                count++;
        }

        return count;
    }

    public void update() {
        // Complete this method
        char[][] newSociety = new char[society.length][society[0].length];

        for (int r = 0; r < society.length; r++) {
            for (int c = 0; c < society[r].length; c++) 
                newSociety[r][c] = society[r][c];
        }
    }
}

关于代码的说明:toString()方法太慢了,因为它逐个字符地增加字符串,每个字符都重新分配字符串。考虑使用StringBuilder。 - Serge Rogatch
哦,好的,那很有道理!谢谢你加上了这个 @SergeRogatch - user4590197
1个回答

4

看起来你在两个地方使用了 >=,而不是 <=。

public int neighborCount(int row, int col) {

    int count = 0;
    for(int i = row - 1; i <= row + 1; i++) {
        if (i >= 0 && i < society.length) // fixed here
            for(int j = col - 1; j <= col + 1; j++) 
                if (j >= 0 && j < society[i].length) // fixed here
                    if (i != row || j != col) 
                        if (society[i][j] == 'o') 
                            count++;
    }

    return count;
}

如果您要访问society[i][j],请确保0 <= i < society.length0 <= j < society[i].length

(说明:该段文字提醒读者在访问数组元素时需要注意索引值的范围限制)

谢谢您的回复和帮助...但是,我已经在我的程序中进行了更改,但由于某种原因,它显示已经计算了8个总数,而预期只计算0个。我认为我在程序的其他地方有错误,这会影响到neighborCount方法。 - user4590197
@user4590197 或许你没有正确更新society数组。如果你得到了8,那就意味着society[row][col]的所有邻居都包含'o'。 - Eran
哇!你绝对是正确的!我在我的growCellAt方法中没有必要使用循环。非常感谢你!最简单的错误让我浪费了这么多时间哈哈。谢谢! - user4590197

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接