我写了一个包含2种方法的代码。
第一种方法将代表有效Universe的二维整数数组以及指示特定单元格位置的两个整数x和y作为输入。例如,如果x = 2且y = 1,则分析的单元是由第三子数组的第二个元素表示的单元。如果此类单元格在给定Universe的下一代中仍处于活动状态,则该方法返回1,否则返回0。下一代细胞由以下规则决定:
具有少于两个活邻居的任何活细胞都会死亡,好像是由于人口不足所致。
任何有两个或三个活邻居的活细胞都可以存活到下一代。
具有三个以上活邻居的任何活细胞都会死亡,好像是由于人口过剩造成的。
任何具有三个活邻居的死细胞都变成活细胞,好像是由繁殖引起的。
第二种方法将表示有效Universe的二维整数数组作为输入,并通过调用第一种方法返回表示下一代Universe的二维整数(维数相等)数组。
由于某种原因,我收到了OutOfBoundsException。有人可以解释一下我的代码中发生了什么,并告诉我如何解决吗?
这是我的代码:
// A method that gets the cell from the next generation
public static int getNextGenCell(int[][] validUniverse, int x, int y) {
// Count the number of live neighboring cells
// Declare and initialize a variable as a counter
int count = 0;
if (x == 0) {
if (y == 0) {
for (int i = x; i <= x + 1; i++) {
for (int j = y; j <= y + 1; j++) {
if (validUniverse[i][j] == 1) {
// Update the counter
count++;
}
}
}
}
else if (y == validUniverse[x].length) {
for (int i = x; i <= x + 1; i++) {
for (int j = y - 1; j <= y; j++) {
if (validUniverse[i][j] == 1) {
// Update the counter
count++;
}
}
}
}
else {
for (int i = x; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (validUniverse[i][j] == 1) {
// Update the counter
count++;
}
}
}
}
}
else if (x == validUniverse.length) {
if (y == 0) {
for (int i = x - 1; i <= x; i++) {
for (int j = y; j <= y + 1; j++) {
if (validUniverse[i][j] == 1) {
// Update the counter
count++;
}
}
}
}
else if (y == validUniverse[x].length) {
for (int i = x - 1; i <= x; i++) {
for (int j = y - 1; j <= y; j++) {
if (validUniverse[i][j] == 1) {
// Update the counter
count++;
}
}
}
}
else {
for (int i = x - 1; i <= x; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (validUniverse[i][j] == 1) {
// Update the counter
count++;
}
}
}
}
}
else {
if (y == 0) {
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y; j <= y + 1; j++) {
if (validUniverse[i][j] == 1) {
// Update the counter
count++;
}
}
}
}
else if (y == validUniverse[x].length) {
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y; j++) {
if (validUniverse[i][j] == 1) {
// Update the counter
count++;
}
}
}
}
else {
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (validUniverse[i][j] == 1) {
// Update the counter
count++;
}
}
}
}
}
/* Write the following conditions:
* 1) Any live cell with fewer than two live neighbors dies, as if caused by under population
* 2) Any live cell with two or three live neighbors lives on to the next generation
* 3) Any live cell with more than three live neighbors dies, as if caused by overpopulation
* 4) Any dead cell with exactly three live neighbors becomes a live cell, as if caused by reproduction */
// Declare and initialize a variable
int status = -1;
if (validUniverse[x][y] == 1) {
// Adjust the counter so that it doesn't count the cell itself
count--;
if (count < 2) {
status = 0;
}
else if (count >= 2 && count <= 3) {
status = 1;
}
else {
status = 0;
}
return status;
}
else if (validUniverse[x][y] == 0) {
if (count == 3) {
status = 1;
}
else {
status = 0;
}
}
return status;
}
// A method that creates the next generation universe by calling the getNextGenCell() method
public static int[][] getNextGenUniverse(int[][] validUniverse) {
// Create a copy of the original array
int[][] nextUniverse = new int[validUniverse.length][validUniverse[0].length];
// Iterate through all the elements of the array
for (int i = 0; i < nextUniverse.length; i++) {
// Iterate through all the elements of the subarrays
for (int j = 0; j < nextUniverse[i].length; j++) {
nextUniverse[i][j] = validUniverse[i][j];
}
}
// Iterate through all the elements of the array
for (int i = 0; i < nextUniverse.length; i++) {
// Iterate through all the elements of the subarrays
for (int j = 0; j < nextUniverse[i].length; j++) {
System.out.println("test");
if (getNextGenCell(nextUniverse, i, j) == 1) {
nextUniverse[i][j] = 1;
}
else if (getNextGenCell(nextUniverse, i, j) == 0) {
nextUniverse[i][j] = 0;
}
}
}
return nextUniverse;
}