java - Game Of Life Java - 使用二维数组和 for 循环计算邻居

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

我目前正在 Eclipse 中使用@Test 案例开发 Conway 的生命游戏程序。除了 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];
        }
    }
}

最佳答案

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

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 .

关于java - Game Of Life Java - 使用二维数组和 for 循环计算邻居,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30930967/

相关文章:

java - SSL 套接字连接错误

javascript - 如何使用数组条件过滤数组

python - 如何从文件夹内的 n 个 csv 文件导入数据?

java - hazelcast dataSerialized - 序列化对象数组

java - 使用 "for"循环在 java 中打印出菱形图案

for-loop - pl/sql循环记录select oracle plsql

java - 如何获得波斯语地址

java - 当我将按钮放在 Activity 上时,Android 应用程序崩溃,空 Activity 正在工作

java - 为什么当我在根文件夹下按掩码搜索文件时会看到 NullPointerException?(windows)

c - 将变量引用到现有数组