我正在尝试为我的CS课程建立一个康威的生命游戏计划,我坚持使用其中一种方法。以下是我到目前为止的情况:
public class GameOfLifeFunctions {
public static void toggleCell(int row, int col, int[][] board) {
// TODO: This next line crashes the program, so delete it when you start coding
//throw new UnsupportedOperationException("DELETE THIS LINE WHEN YOU START CODING");
board[row][col]=board[row][col]==0?1:0;
}
public static void resetCells(int[][] board) {
// TODO: This next line crashes the program, so delete it when you start coding
//throw new UnsupportedOperationException("DELETE THIS LINE WHEN YOU START CODING");
for (int j=0; j<board.length; j++) {
for (int i=0; i<board.length; i++) {
board[j][i]=0;
}
}
}
public static int[][] copyCells(int[][] board) {
int[][] copy=new int[board.length][board[0].length];
// TODO: This next line crashes the program, so delete it when you start coding
//throw new UnsupportedOperationException("DELETE THIS LINE WHEN YOU START CODING");
for (int j=0; j<board.length; j++) {
for (int i=0; i<board.length; i++) {
copy[j][i]=board[j][i];
}
}
return copy;
}
public static int getNumLiveNeighbors(int[][] cells, int row, int col) {
int h=cells.length;
int w=cells[0].length;
int count=0;
// TODO: This next line crashes the program, so delete it when you start coding
//throw new UnsupportedOperationException("DELETE THIS LINE WHEN YOU START CODING");
for (int j=row-1; j<=row+1; j++) {
for (int i=col-1; i<=col+1; i++) {
//Something broken in this if statement
//OutOfBounds problem
if (j==row && i==col) {
continue;
}
if (j<0 || j>h-1 || 1<0 || i>w-1) {
;
}
else if (cells[j][i]==1) {
count++;
}
}
}
return count;
}
public static int[][] calcualteNextGeneration(int[][] cells) {
// TODO: Calculate the next generation by calling getNumLiveNeighbors for each
// cell in the grid. Put the results of the new generation into the 2D array result.
int[][] result=new int[cells.length][cells[0].length];
int living;
// TODO: This next line crashes the program, so delete it when you start coding
for (int j; j<cells.length; j++) {
for (int i; i<cells[0].length; i++) {
}
}
throw new UnsupportedOperationException("DELETE THIS LINE WHEN YOU START CODING");
}
我坚持使用getNumLiveNeighbors
方法。如果你能帮我解释那就太好了!
答案 0 :(得分:1)
在 <item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowIsTranslucent">true</item>
我认为你的意思是if (j<0 || j>h-1 || 1<0 || i>w-1)
作为第三次测试,而不是i<0
。