[英]Conway's Game of Life Method
I am trying to build a Conway's Game of Life program for my CS class and I am stuck on one of the methods.我正在尝试为我的 CS 课程构建康威的生命游戏程序,但我坚持使用其中一种方法。 Here is what i have so far:
这是我到目前为止所拥有的:
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");
}
I am stuck on the getNumLiveNeighbors
method.我被困在
getNumLiveNeighbors
方法上。 If you could help me out and explain that would be great!如果你能帮我解释一下,那就太好了!
在if (j<0 || j>h-1 || 1<0 || i>w-1)
我认为你的意思是i<0
作为第三个测试而不是1<0
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.