代码拉取完成,页面将自动刷新
public class Field
{
private final int width;
private final int height;
private final Cell[][] field;
private final int[][] neighbours;
public Field(int width, int height)
{
this.width = width;
this.height = height;
field = new Cell[height][width];
neighbours = new int[height][width];
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
field[row][col] = new Cell();
if (Math.random() < 0.2)
{
field[row][col].setAlive(true);
}
}
}
}
public int getWidth()
{
return width;
}
public int getHeight()
{
return height;
}
public boolean isAlive(int row, int col)
{
if (row < 0 || row >= height || col < 0 || col >= width)
{
return false;
}
return field[row][col].isAlive();
}
public int getNeighbour(int row, int col)
{
int cnt = 0;
for (int r = row - 1; r <= row + 1; r++)
{
for (int c = col - 1; c <= col + 1; c++)
{
if (!(r == row && c == col) && isAlive(r, c))
{
cnt++;
}
}
}
return cnt;
}
public void update()
{
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
neighbours[row][col] = getNeighbour(row, col);
}
}
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
field[row][col].update(neighbours[row][col]);
}
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。