Game Of Life Java - 使用2D数组和for循环计算邻居

时间:2015-06-19 06:04:02

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

我目前正在使用@Test案例在Eclipse中研究Conway的生命游戏计划。除了neighborCount方法之外,我的所有方法都通过了他们的测试。我已经看到这个方法的帖子使用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];
        }
    }
}

1 个答案:

答案 0 :(得分:2)

看起来你有&gt; =而不是&lt;在两个地方:

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