我正在研究康威的生命游戏程序,我正处于死/活细胞检查它周围的邻居并计算它周围的活邻居的数量的地步。现在,我正在检查 [0][0]。我遇到的问题是正在检查 [0][0] 以及周围的索引。我想如果我输入“if (k!=o && l!=p)”,它会排除 [0][0],但事实并非如此。
public class Life {
public static void main(String[] args)
{
int N = 5;
boolean[][] b = new boolean[N][N];
double cellmaker = Math.random();
int i = 0;
int j = 0;
int o=0;
int p=0;
int livecnt = 0; //keeps track of the alive cells surrounding cell
System.out.println("First Generation:");
// Makes the first batch of cells
for ( i = 0; i < N ; i++)
{
for ( j = 0; j< N; j++)
{
cellmaker = Math.random();
if (cellmaker > 0.5) // * = alive; - = dead
{
b[i][j]=true;
System.out.print( "* ");
}
if (cellmaker < 0.5)
{ b[i][j] = false;
System.out.print("- ");
}
}
System.out.println();
}
// Checks for the amount of "*" surrounding (o,p)
for (int k=(o-1); k <= o+1; k++ )
{
for (int l =(p-1); l <=p+1; l++)
{
if ( k >= 0 && k < N && l >= 0 && l < N) //for the border indexes.
{
//if (k!=o && l!=p)
{
if (b[k][l] == true)
{
livecnt++;
}
}
}
}
}
System.out.println(livecnt++);
}
}