在一些帮助下,我创建了一个名为nextStep的临时2D数组来处理在细胞群中生存的新细胞 - 但由于某种原因,我的代码仍在分娩并杀死同一代细胞而不是在下一代应用必要的变化。任何帮助都将非常感激。
public void updateColony() {
nextStep = new int[colony.length][colony[0].length];
for (int i = 0; i < colony.length; i++) {
for (int j = 0; j < colony[i].length; j++) {
evolution(i, j, colony);
}
}
colony = nextStep;
}
public void setCellAlive (int row, int col){
if (row <= numberRows){
nextStep [row][col] = 1;
}else{
System.out.println ("Index out of range.");
}
}
public void setCellDead (int row, int col){
if (row <= numberRows){
nextStep [row][col]=0;
}else{
System.out.println ("Index out of range.");
}
}
private void evolution(int i, int j, int [][] colony) {
int left = 0, right = 0, up = 0, down = 0;
int UpperLeft = 0, UpperRight = 0, LowerLeft = 0, LowerRight = 0;
if (j < colony.length - 1) {
right = colony[i][j + 1];
if(i>0)
UpperRight = colony[i - 1][j + 1];
if (i < colony.length - 1)
LowerRight = colony[i + 1][j + 1];
}
if (j > 0) {
left = colony[i][j - 1];
if (i > 0)
UpperLeft = colony[i - 1][j - 1];
if (i< colony.length-1)
LowerLeft = colony[i + 1][j - 1];
}
if (i > 0)
up = colony[i - 1][j];
if (i < colony.length - 1)
down = colony[i + 1][j];
int sum = left + right + up + down + UpperLeft + UpperRight
+ LowerLeft
+ LowerRight;
if (colony[i][j] == 1) {
if (sum < 2)
setCellDead (i,j);
if (sum > 3)
setCellDead(i,j);
}
else {
if (sum == 3)
setCellAlive (i,j);
}
}