【发布时间】:2019-03-13 10:16:01
【问题描述】:
我看了康威的生命游戏,想自己制作。然而,边缘的细胞不知何故不遵守规则,只是一直活着(或死了)。有人知道我在哪里犯了错误吗? 这是我的代码:(我只上传了我应用规则的类。如果需要其他类来解决问题,我也可以上传)
import java.util.Arrays;
/**
* The class Grid does the initialization of the game of life and the application of the rules.
* It is a 2D array of the type Cell. It saves the cells and uses a copy of it to apply the rules.
*
*/
public class Grid
{
public int col;
public int row;
public int x,y;
public Cell [][] array; //2D array of the type Cell
public Cell [][] arraycopy;
/**
* This constructor is to create the initial generation of cells and set the state of random 20% to true (=alive).
* @param col the number of columns
* @param row the number of rows
*
*/
public Grid(int col, int row)
{
this.col = col;
this.row = row;
this.array = new Cell [col][row];
//Loops through every spot in the 2D array
for (int x = 0; x < col; x++)
{
for (int y=0; y < row; y++)
{
//set randomly 20% of the cells' state to "true"
if (Math.random() <= 0.2)
{
Cell cell = new Cell (true);
this.array[x][y]= cell;
}
else
{
Cell cell = new Cell (false);
this.array[x][y]= cell;
}
}
}
}
/**
* This method will count the alive cells in a 3*3 neighboorhood and apply the rules of life.
* This method uses arraycopy.
*
*/
public void lifeSteps()
{
//Works with a copy of the array and the cells
this.arraycopy = new Cell [col][row];
for (int x = 0; x < col; x++)
{
for (int y=0; y < row; y++)
{
this.arraycopy [x][y] = new Cell(this.array[x][y].getState());
}
}
//Looping through the cells, but the cells at the edge are skipped
for (int x = 1; x < col-1; x++)
{
for (int y= 1; y < row-1; y++)
{
//Looping through all the neighbors
int numNeighborsAlive = 0;
for (int i = x-1; i <= x+1; i++)
{
for (int j = y-1; j <= y+1; j++)
{
//In a 3x3 neighborhood the middle cell needs to be skipped
if ((x != i) && (y != j))
{
//Only the cells that are alive (true) are added
if (arraycopy [i][j].getState() == true)
{
numNeighborsAlive += 1;
}
}
}
}
//Apply the rules of life
if ((array [x][y].getState()) && (numNeighborsAlive < 2 || numNeighborsAlive >3)) //Loneliness and Overpopulation
{
array[x][y].setState(false);
}
else if ((array [x][y].getState() == false) && (numNeighborsAlive ==3)) //Birth
{
array[x][y].setState(true);
}
else
{ //stasis
}
}
}
}
/**
* This method will return the statement for the array.
* @return the 2D array of the type Cell
*/
public Cell[][] returnGrid ()
{
return this.array;
}
/**
* This method will test if everything is working well by printing zeros and ones.
*
*/
public void printTest()
{
System.out.println("\t"); // a new line
for (int x = 0; x < col; x++)
{
for (int y=0; y < row; y++)
{
// assigns 1 if the cell is alive and 0 if it is dead
if (array[x][y].getState() == true)
{
System.out.print("1");
}
else
{
System.out.print("0");
}
}
System.out.println(""); // will be displayed as colums and rows
}
System.out.println("\t"); // a new line
}
}
【问题讨论】:
-
我认为您的评论
//Looping through the cells, but the cells at the edge are skipped可能是相关的 ;-)
标签: java arrays conways-game-of-life