生命游戏算法问题

时间:2012-04-11 00:16:57

标签: c#

我正在用C#编写一个生命游戏计划。我正在使用2D数组结构。似乎当我显示Random方法时,邻居或某事的算法是错误的。当它经历了几代人时,随机细胞就会变得“活着”。有什么帮助吗?

public struct cellDetail
{
    public int curGenStatus;
    public int nextGenStatus;
    public int age;

}
public class Class1
{

    static cellDetail[,] Generations(cellDetail[,] cells)
    {

        int neighbours = 0;

        for (int i = 0; i < 40; i++)
            for (int j = 0; j < 60; j++)
                cells[i, j].nextGenStatus = 0;

        for (int row = 0; row < 39; row++)
            for (int col = 0; col < 59; col++)
            {
                neighbours = 0;

                if (row > 0 && col > 0)
                {
                    if (cells[row - 1, col - 1].curGenStatus > 0)
                        neighbours++;
                    if (cells[row - 1, col].curGenStatus > 0)
                        neighbours++;
                    if (cells[row - 1, col + 1].curGenStatus > 0)
                        neighbours++;

                    if (cells[row, col - 1].curGenStatus > 0)
                        neighbours++;
                    if (cells[row, col + 1].curGenStatus > 0)
                        neighbours++;

                    if (cells[row + 1, col - 1].curGenStatus > 0)
                        neighbours++;
                }

                if (cells[row + 1, col].curGenStatus > 0)
                    neighbours++;
                if (cells[row + 1, col + 1].curGenStatus > 0)
                    neighbours++;

                if (neighbours < 2)
                    cells[row, col].nextGenStatus = 0;
                if (neighbours > 3)
                    cells[row, col].nextGenStatus = 0;
                if ((neighbours == 2 || neighbours == 3) && cells[row, col].curGenStatus > 0)
                    cells[row, col].nextGenStatus = 1;
                if (neighbours == 3 && cells[row, col].curGenStatus == 0)
                    cells[row, col].nextGenStatus = 1;
            }

        for (int i = 0; i < 40; i++)
            for (int j = 0; j < 60; j++)
                cells[i, j].curGenStatus = cells[i, j].nextGenStatus;

        return cells;
    }

    static void PrintCells(cellDetail[,] cells)
    {
        for (int row = 0; row < 40; row++)
        {
            for (int col = 0; col < 60; col++)
            {
                if (cells[row, col].curGenStatus != 0)
                {
                    cells[row, col].curGenStatus = (char)30;
                    Console.Write((char)cells[row, col].curGenStatus);
                }
                else
                    Console.Write(" ");
            }
        }
    }

    public static void Random(int numOfGenerations)
    {
        cellDetail[,] cells = new cellDetail[40,60];
        Random rand = new Random();

        for (int row = 0; row < 40; row++)
            for (int col = 0; col < 60; col++)
                cells[row, col].curGenStatus = rand.Next(0, 2);

        for (int i = 0; i < numOfGenerations; i++)
        {
            Console.ForegroundColor = (ConsoleColor.Green);
            Generations(cells);
            Console.SetCursorPosition(0, 3);
            PrintCells(cells);
        }

    }
}

1 个答案:

答案 0 :(得分:5)

随机对象应该只创建一次并由类的所有对象使用。通过将它声明为类的静态成员,可以实现这一点。更好的选择是为随机对象创建单例辅助类。