【问题标题】:Game Of Life game: Check 2D array 8 closest surroundings; ArrayIndexOutOfBoundsException issueGame Of Life 游戏:检查 2D 数组 8 个最近的环境; ArrayIndexOutOfBoundsException 问题
【发布时间】:2021-01-31 17:40:10
【问题描述】:

我遇到了这个问题,我找不到解决方案。有一个二维数组,其中只有零和一个(数组的大小并不重要,我使用的是 10x10)。零表示死,1 表示活着。我将它加倍循环以检查元素sorroundings,当它与其他“单元格”一起使用时,代码可以正常工作。但是当它在角落或数组的另一边之一时,它会抛出 ArrayIndexOutOfBoundsException。 我的问题是如何在不处理所有可能情况的情况下为此编写代码?

public class Main {

public static void main(String[] args) {
    int[][] grid = {
            {1,1,1,0,1,0,1,0,0,0},
            {1,1,1,0,1,1,0,0,1,0},
            {1,1,0,0,1,0,0,1,0,0},
            {0,1,0,1,0,0,1,0,0,0},
            {0,0,1,0,0,1,0,0,1,0},
            {0,0,0,0,0,0,0,0,1,0},
            {0,0,0,1,0,0,0,1,0,0},
            {0,1,0,0,0,0,0,0,0,0},
            {0,0,0,1,0,0,1,0,0,0},
            {0,1,0,0,0,0,0,0,1,0},
    };

    Simulation sm = new Simulation(grid);
    sm.printOutOriginalGrid();
    sm.gameOfLife();
}

}

public class Simulation {

private int[][] grid;

public Simulation(int[][] grid){
    this.grid = grid;
}
public  void printOutOriginalGrid() {
    System.out.println("Original grid");
    for (int i = 0; i < this.grid.length; i++) {
        for (int j = 0; j < this.grid[i].length; j++) {
            System.out.print(this.grid[i][j] + "  ");
        }
        System.out.println(" ");
    }
}

public int[][] gameOfLife() {
    for (int i = 0; i < this.grid.length; i++) {
        for (int j = 0; j < this.grid.length; j++) {
            int currentCell = this.grid[i][j];
            if(currentCell == 1){
                int currentCellNeighbours = numberOfAliveNeighbours(i,j); 
            }
        }
    }

    return new int[12][12];
}

private int numberOfAliveNeighbours(int i, int j){
    int numberOfAliveNeighbours = 0;

    numberOfAliveNeighbours += this.grid[i-1][j-1];
    numberOfAliveNeighbours += this.grid[i][j-1];
    numberOfAliveNeighbours += this.grid[i+1][j-1];

    numberOfAliveNeighbours += this.grid[i-1][j];
    numberOfAliveNeighbours += this.grid[i+1][j];

    numberOfAliveNeighbours += this.grid[i-1][j+1];
    numberOfAliveNeighbours += this.grid[i][j+1];
    numberOfAliveNeighbours += this.grid[i+1][j+1];

    return numberOfAliveNeighbours;
}

}

【问题讨论】:

  • 返回新的 int[12][12];只是编译器的随机返回
  • 这不是一个有效的位置int[12][12]; 也在方法numberOfAliveNeighbours() 中传递二维数组大小来检查你没有移出数组索引。

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


【解决方案1】:

嗯...既然我已经完成了,我将发布我的答案(我没有@GilberLeBlanc 快)。

正如您已经知道的,可以从矩阵中获取 8 个可能的邻居,因此我们需要确保每个定向单元邻居确实在该矩阵的范围内……这是显而易见的。遍历提供的矩阵单元的每个方向,并且仅对包含与最初由ij 提供给 numberOfAliveNeighbours() 的单元元素相同的值的有效单元(在边界内)执行增量 方法:

这是我对它的快速看法:

public class Main {

    public static void main(String[] args) {
        int[][] grid = {
            {1,1,1,0,1,0,1,0,0,0},
            {1,1,1,0,1,1,0,0,1,0},
            {1,1,0,0,1,0,0,1,0,0},
            {0,1,0,1,0,0,1,0,0,0},
            {0,0,1,0,0,1,0,0,1,0},
            {0,0,0,0,0,0,0,0,1,0},
            {0,0,0,1,0,0,0,1,0,0},
            {0,1,0,0,0,0,0,0,0,0},
            {0,0,0,1,0,0,1,0,0,0},
            {0,1,0,0,0,0,0,0,1,0},
        };

        Simulation sm = new Simulation(grid);
        sm.printOutOriginalGrid();
        sm.gameOfLife();
    }
}

public class Simulation {

    private int[][] grid;

    public Simulation(int[][] grid) {
        this.grid = grid;
    }

    public void printOutOriginalGrid() {
        System.out.println("Original grid");
        for (int i = 0; i < this.grid.length; i++) {
            for (int j = 0; j < this.grid[i].length; j++) {
                System.out.print(this.grid[i][j] + "  ");
            }
            System.out.println(" ");
        }
    }

    public int[][] gameOfLife() {
        for (int i = 0; i < this.grid.length; i++) {
            for (int j = 0; j < this.grid[i].length; j++) {
                int currentCell = this.grid[i][j];
                if (currentCell == 1) {
                    int currentCellNeighbours = numberOfAliveNeighbours(i, j);
                    // Do what you want with value in currentCellNeighbours.
                    System.out.println("Cell " + i + ", " + j + " has " + 
                                       currentCellNeighbours + " neighbours.");
                }
            }
        }
        
        // return whatever you're preparing to do...
        return new int[12][12];
    }

    private int numberOfAliveNeighbours(int i, int j) {
        /* In a matrix there can be 8 possible neighbours. 
           We need to check if a specific neigbour is actually
           within bounds before checking its value.       */
        int[][] directions = {
            {-1, -1}, // Top/Left
            {0, -1}, // Top
            {1, -1}, // Top/Right
            {-1, 0}, // Left
            {1, 0}, // Right
            {-1, 1}, // Bottom/Left
            {0, 1}, // Bottom
            {1, 1} // Bottom/Right  
        };

        int numberOfAliveNeighbours = 0;  // Start with 0 neighbours

        // Iterate through all the different directions...
        for (int[] d : directions) {
            int dx = d[0];  // Current directional Cell Row
            int dy = d[1];  // Current directional Cell Column
            /* Is the current directional cell within bounds and 
               if it is, does the directional cell element equal 
               the supplied cell element (i, j)?           */
            if ((i + dx) >= 0 && (i + dx) < (grid.length)
                    && (j + dy) >= 0 && (j + dy) < (grid[0].length)
                    && grid[i + dx][j + dy] == grid[i][j]) {
                // Yes it does...increment the neighbour count.
                numberOfAliveNeighbours++;
            }
        }
        // Return the determined neighbour count.
        return numberOfAliveNeighbours;
    }

}

【讨论】:

  • 也非常感谢。是的 GilberLeBlanc 有点快,但我仍然很感激你的回答。 :D 我也喜欢这个解决方案,因为它更紧凑,而且看起来很简单,除了 if 语句:P 这也适用于我。
【解决方案2】:

numberOfAliveNeighbours 方法中,您必须测试小于零或大于数组大小 - 1 的数组索引。

换句话说,一个长 8 个值的 int 数组的索引值从 0 到 7。

您的代码已修复。

public class GameOfLife {

    public static void main(String[] args) {
        int[][] grid = { 
                { 1, 1, 1, 0, 1, 0, 1, 0, 0, 0 }, 
                { 1, 1, 1, 0, 1, 1, 0, 0, 1, 0 },
                { 1, 1, 0, 0, 1, 0, 0, 1, 0, 0 }, 
                { 0, 1, 0, 1, 0, 0, 1, 0, 0, 0 }, 
                { 0, 0, 1, 0, 0, 1, 0, 0, 1, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 }, 
                { 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 }, 
                { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 1, 0, 0, 1, 0, 0, 0 }, 
                { 0, 1, 0, 0, 0, 0, 0, 0, 1, 0 }, };

        Simulation sm = new GameOfLife().new Simulation(grid);
        sm.printOutOriginalGrid();
        sm.gameOfLife();
    }

    public class Simulation {

        private int[][] grid;

        public Simulation(int[][] grid) {
            this.grid = grid;
        }

        public void printOutOriginalGrid() {
            System.out.println("Original grid");
            for (int i = 0; i < this.grid.length; i++) {
                for (int j = 0; j < this.grid[i].length; j++) {
                    System.out.print(this.grid[i][j] + "  ");
                }
                System.out.println(" ");
            }
        }

        public int[][] gameOfLife() {
            for (int i = 0; i < this.grid.length; i++) {
                for (int j = 0; j < this.grid[i].length; j++) {
                    int currentCell = this.grid[i][j];
                    if (currentCell == 1) {
                        int currentCellNeighbours = 
                                numberOfAliveNeighbours(i, j, grid.length);
                    }
                }
            }

            return new int[12][12];
        }

        private int numberOfAliveNeighbours(int i, int j, int limit) {
            int numberOfAliveNeighbours = 0;
            int a = i - 1;
            int b = i + 1;
            int c = j - 1;
            int d = j + 1;

            if (c >= 0) {
                if (a >= 0) {
                    numberOfAliveNeighbours += this.grid[a][c];
                }
                numberOfAliveNeighbours += this.grid[i][c];
                if (b < limit) {                    
                    numberOfAliveNeighbours += this.grid[b][c];
                }
            }

            if (a >= 0) {
                numberOfAliveNeighbours += this.grid[a][j];
            }
            if (b < limit) {
                numberOfAliveNeighbours += this.grid[b][j];
            }

            if (d < limit) {
                if (a >= 0) {
                    numberOfAliveNeighbours += this.grid[a][d];
                }
                numberOfAliveNeighbours += this.grid[i][d];
                if (b < limit) {
                    numberOfAliveNeighbours += this.grid[b][d];
                }
            }

            return numberOfAliveNeighbours;
        }

    }

}

【讨论】:

  • 非常感谢!老实说,它看起来很整洁,因为它是。我尝试过类似的事情,但对我来说没有成功。
猜你喜欢
  • 2015-09-05
  • 1970-01-01
  • 1970-01-01
  • 2021-04-13
  • 1970-01-01
  • 1970-01-01
  • 2015-05-07
  • 2022-12-26
  • 2011-04-12
相关资源
最近更新 更多