0

我正在用 C# 编写一个生命游戏程序。我正在使用二维结构数组。当我显示 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);
        }

    }
}
4

1 回答 1

5

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

于 2012-04-11T00:21:21.147 回答