生命游戏 Java - 使用 2D 数组和 for 循环计算邻居
Game Of Life Java - Counting neighbors with 2D arrays and for loops
我目前正在 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.length
和 0 <= j < society[i].length
。
我目前正在 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.length
和 0 <= j < society[i].length
。