参数
public bool[,] map;//存储地图
public int maplength = 2500;//地图的长度
public int interval = 500;//时间间隔
public int length = 750;//画布长度
public int size = 5;//单个细胞大小
public int count;//细胞数量
public int x;//x坐标
public int y;//y坐标
绘制函数
public void draw()//绘制
{
Bitmap myImage = new Bitmap(length, length);
Graphics g = Graphics.FromImage(myImage);
g.Clear(Color.White);
int cellcount = 0;//活细胞数
for (int i = x - count / 2; i < x + count / 2; i++)
{
for (int j = y - count / 2; j < y + count / 2; j++)
{
if (map[i, j])
{
g.FillRectangle(Brushes.Black, (i - x + count / 2) * size, (j - y + count / 2) * size, size, size);
cellcount++;
}
}
}
g.FillEllipse(Brushes.Red, length / 2 + size / 2 - 2, length / 2 + size / 2 - 2, 4, 4);
Graphics g2 = this.panel1.CreateGraphics();
g2.DrawImage(myImage, 0, 0);
label5.Text = $"繁衍代数:{gen}";
label6.Text = $"区域存活细胞:{cellcount}";
}
繁衍函数
public void nextgen()//另一种繁衍-效率更高
{
int[,] count = new int[maplength + 2, maplength + 2];
int sumcount = 0;
for (int i = 0; i < maplength; i++)
{
for (int j = 0; j < maplength; j++)
{
if (map[i, j])
{
count[i, j]++; count[i, j + 1]++; count[i, j + 2]++;
count[i + 1, j]++; count[i + 1, j + 2]++;
count[i + 2, j]++; count[i + 2, j + 1]++; count[i + 2, j + 2]++;
sumcount++;
}
}
}
for (int i = 0; i < maplength; i++)
{
for (int j = 0; j < maplength; j++)
{
if (count[i + 1, j + 1] < 2)
map[i, j] = false;
else if (count[i + 1, j + 1] == 3)
map[i, j] = true;
else if (count[i + 1, j + 1] > 3)
map[i, j] = false;
}
}
gen++;
label7.Text = $"整体存活细胞:{sumcount}";
}