1 Star 0 Fork 0

青羽/code-base

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Field.java 1.79 KB
一键复制 编辑 原始数据 按行查看 历史
青羽 提交于 2025-01-14 23:36 +08:00 . [merge] ready merge for java
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]);
}
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/chobity/code-base.git
git@gitee.com:chobity/code-base.git
chobity
code-base
code-base
main

搜索帮助