如何解决Game Of Life游戏:查看2D阵列8个最近的环境; ArrayIndexOutOfBoundsException 问题
我遇到了这个问题,但找不到解决方案。有一个只有 0 和 1 的二维数组(数组的大小并不重要,我使用的是 10x10)。零表示死,1 表示活着。我把它加倍循环以检查元素周围的情况,当它与其他“单元格”一起出现时,代码会正常工作。但是当它位于数组的角落或另一边之一时,它会抛出一个 Arrayindexoutofboundsexception。 我的问题是如何在不处理所有可能情况的情况下为此编写代码?
public class Main {
public static void main(String[] args) {
int[][] grid = {
{1,1,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;
}
}
解决方法
好吧...既然我已经完成了,我将发布我的答案(我没有@GilberLeBlanc 快)。
正如您已经知道的那样,可以从矩阵中获取 8 个可能的邻居,因此我们需要确保每个有方向的单元邻居确实在该矩阵的范围内......这是显而易见的。迭代提供的矩阵单元格的每个方向,并仅对包含与最初由 i
和 j
提供给 numberOfAliveNeighbours() 方法:
这是我的快速理解:
public class Main {
public static void main(String[] args) {
int[][] grid = {
{1,1,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,// Top
{1,// Top/Right
{-1,// Left
{1,// Right
{-1,1},// Bottom/Left
{0,// 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;
}
}
,
在 numberOfAliveNeighbours
方法中,您必须测试小于零或大于数组大小 - 1 的数组索引。
换句话说,一个长度为 8 个值的 int 数组具有从 0 到 7 的索引值。
这是您修复的代码。
public class GameOfLife {
public static void main(String[] args) {
int[][] grid = {
{ 1,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;
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。