我的生活游戏是为 3D 环境编写的,使用 SharpGL 用 C# 编写。就逻辑而言,代码运行良好,但运行速度非常慢。
游戏中的单元格是用颜色编码的,这意味着根据它们在下一个“回合”中的状态,它们会改变颜色。我的老师要在一些非常旧的计算机上测试这个项目,所以我需要弄清楚如何让代码更快更高效。
计算代码贴在下面。请指出任何可能减慢游戏速度的问题。
//cell.willdie means that the cell will die in the next turn
//cell.dies means that the cell dies immediately
//cell.abouttobeborn means that the cell will be born in the next turn
//cell.coord is where the cell is
public void checkcells() //determines whether or not for every cell if they die or not. Also changes color-coding of cells.
{
List<int[]> openspaceatwhere = new List<int[]>();
List<cell_class> acell = new List<cell_class>();
for (int i = 0; i < cell.Count(); i++)
{
bool dies = false;
int neighbors = 0;
cell[i].willdie = false;
for (int x = -1; x < 2; x++)
{
for (int y = -1; y < 2; y++)
{
for (int z = -1; z < 2; z++)
{
bool isopen = true;
for (int i2 = 0; i2 < cell.Count(); i2++)
{
if (!(x == 0 && y == 0 && z == 0) && cell[i].coord[0] + x == cell[i2].coord[0] && cell[i].coord[1] + y == cell[i2].coord[1] && cell[i].coord[2] + z == cell[i2].coord[2])
{
isopen = false;
neighbors += 1;
}
}
if (isopen)
{
openspaceatwhere.Add(new int[4] { cell[i].coord[0]+x, cell[i].coord[1]+y, cell[i].coord[2]+z, 0 });
}
}
}
}
if (neighbors > 6 || neighbors < 3)
dies = true;
if (dies)
cell[i].dies = true;
else
cell[i].dies = false;
for (int a = 0; a < openspaceatwhere.Count(); a++)
{
for (int b = 0; b < openspaceatwhere.Count(); b++)
{
if (openspaceatwhere[a][0] == openspaceatwhere[b][0] && openspaceatwhere[a][1] == openspaceatwhere[b][1] && openspaceatwhere[a][2] == openspaceatwhere[b][2])
{
if (a != b)
{ openspaceatwhere.RemoveAt(b); openspaceatwhere[a][3] += 1; }
}
}
}
}
List<int[]> quequecell = new List<int[]>();
for (int i = 0; i < openspaceatwhere.Count(); i++ )
{
if(openspaceatwhere[i][3] == 6)
quequecell.Add(new int[3] { openspaceatwhere[i][0], openspaceatwhere[i][1], openspaceatwhere[i][2]});
}
for (int i = 0; i < cell.Count(); i++)
{
if (cell[i].dies)
{
if (animated && delay == 50)
{
cell.RemoveAt(i);
cellsdied += 1;
i--;
}
else
{
cell[i].willdie = true;
}
}
}
for (int i = 0; i < quequecell.Count(); i++)
{
if (animated && delay == 50)
{
cell.Add(new cell_class());
cell[cell.Count()-1].coord[0] = quequecell[i][0];
cell[cell.Count()-1].coord[1] = quequecell[i][1];
cell[cell.Count()-1].coord[2] = quequecell[i][2];
createdcells += 1;
}
else
{
acell.Add(new cell_class());
acell[acell.Count()-1].coord[0] = quequecell[i][0];
acell[acell.Count()-1].coord[1] = quequecell[i][1];
acell[acell.Count()-1].coord[2] = quequecell[i][2];
acell[acell.Count()-1].abouttobeborn = true;
rendercell(acell[acell.Count()-1]);
}
}
}