【问题标题】:Game Of Life Java - Counting neighbors with 2D arrays and for loopsGame Of Life Java - 使用 2D 数组和 for 循环计算邻居
【发布时间】:2015-09-05 00:19:00
【问题描述】:

我目前正在使用@Test 案例在 Eclipse 中开发 Conway 的 Game of life 程序。我的所有方法都通过了测试,除了 neighborCount 方法。我已经看到使用 for 循环的这种方法的帖子,并且由于某种原因它不适用于我的代码。

我试图通过仅使用 for 循环定位相邻单元格来环绕二维数组。在数完邻居后,我也无法更新新社会。如果有人可以查看我的代码并在我的方法中找到错误,将不胜感激。先感谢您。我已经附上了我的所有代码,以防我在影响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。
  • 哦,好吧,这是有道理的!感谢您添加@SergeRogatch

标签: java arrays for-loop conways-game-of-life


【解决方案1】:

看起来你在两个地方有 >= 而不是

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 &lt;= i &lt; society.length0 &lt;= j &lt; society[i].length

【讨论】:

  • 感谢您的回复和帮助...但是,好吧,我刚刚在我的程序中更改了它,但由于某种原因,它说它已经计算了 8 个总计数,而预计它只计数 0。我在想我的程序中的其他地方有一个错误,它影响了 neighborCount 方法。
  • @user4590197 也许您没有正确更新society 数组。如果得到 8,则表示 society[row][col] 的所有邻居都包含 'o'。
  • 天啊!你是绝对正确的!我在我的 growCellAt 方法中无缘无故地使用了循环。太感谢了!最简单的错误让我花了这么多时间哈哈。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-22
  • 2022-12-26
  • 1970-01-01
  • 2019-09-16
  • 2023-04-01
  • 1970-01-01
相关资源
最近更新 更多