以下是使用C#编写的生命游戏的示例代码:

using System;

namespace GameOfLife
{
    class Program
    {
        static void Main(string[] args)
        {
            // 定义细胞图的大小
            int width = 10;
            int height = 10;

            // 创建细胞图
            Cell[,] cells = new Cell[width, height];

            // 初始化细胞图
            InitializeCells(cells);

            // 打印初始细胞图
            PrintCells(cells);

            // 进行10代迭代
            for (int i = 0; i < 10; i++)
            {
                // 计算下一代细胞图
                Cell[,] nextGeneration = CalculateNextGeneration(cells);

                // 打印下一代细胞图
                PrintCells(nextGeneration);

                // 将下一代细胞图作为当前细胞图
                cells = nextGeneration;
            }

            Console.ReadLine();
        }

        // 初始化细胞图
        static void InitializeCells(Cell[,] cells)
        {
            Random random = new Random();

            for (int i = 0; i < cells.GetLength(0); i++)
            {
                for (int j = 0; j < cells.GetLength(1); j++)
                {
                    // 随机设置细胞的状态为生或死
                    bool isAlive = random.Next(2) == 0 ? false : true;
                    cells[i, j] = new Cell(isAlive);
                }
            }
        }

        // 打印细胞图
        static void PrintCells(Cell[,] cells)
        {
            for (int i = 0; i < cells.GetLength(0); i++)
            {
                for (int j = 0; j < cells.GetLength(1); j++)
                {
                    // 根据细胞的状态打印黑色方格或空格
                    Console.Write(cells[i, j].IsAlive ? "■" : " ");
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }

        // 计算下一代细胞图
        static Cell[,] CalculateNextGeneration(Cell[,] cells)
        {
            Cell[,] nextGeneration = new Cell[cells.GetLength(0), cells.GetLength(1)];

            for (int i = 0; i < cells.GetLength(0); i++)
            {
                for (int j = 0; j < cells.GetLength(1); j++)
                {
                    int aliveNeighbors = CountAliveNeighbors(cells, i, j);

                    // 根据生存定律计算下一代细胞的状态
                    if (cells[i, j].IsAlive)
                    {
                        if (aliveNeighbors < 2 || aliveNeighbors > 3)
                        {
                            nextGeneration[i, j] = new Cell(false);
                        }
                        else
                        {
                            nextGeneration[i, j] = new Cell(true);
                        }
                    }
                    else
                    {
                        if (aliveNeighbors == 3)
                        {
                            nextGeneration[i, j] = new Cell(true);
                        }
                        else
                        {
                            nextGeneration[i, j] = new Cell(false);
                        }
                    }
                }
            }

            return nextGeneration;
        }

        // 计算细胞周围存活细胞的数量
        static int CountAliveNeighbors(Cell[,] cells, int x, int y)
        {
            int count = 0;

            for (int i = x - 1; i <= x + 1; i++)
            {
                for (int j = y - 1; j <= y + 1; j++)
                {
                    if (i >= 0 && i < cells.GetLength(0) && j >= 0 && j < cells.GetLength(1) && !(i == x && j == y))
                    {
                        if (cells[i, j].IsAlive)
                        {
                            count++;
                        }
                    }
                }
            }

            return count;
        }
    }

    // 细胞类
    class Cell
    {
        public bool IsAlive { get; set; }

        public Cell(bool isAlive)
        {
            IsAlive = isAlive;
        }
    }
}

在上述代码中,我们定义了一个Cell类来表示细胞,其中包含一个布尔类型的属性IsAlive表示细胞的状态(生或死)。Program类包含了主要的逻辑,其中Main方法用于控制游戏的流程。

Main方法中,我们首先定义了细胞图的大小,并创建了一个二维数组cells来表示细胞图。然后,我们使用InitializeCells方法来初始化细胞图,随机设置细胞的状态为生或死。接下来,我们使用PrintCells方法打印初始细胞图。

然后,我们使用一个循环来进行10代的迭代。在每一代迭代中,我们使用CalculateNextGeneration方法来计算下一代细胞图。该方法根据生存定律,根据当前细胞的状态和周围存活细胞的数量来计算下一代细胞的状态。然后,我们使用PrintCells方法打印下一代细胞图,并将下一代细胞图作为当前细胞图。

最后,我们使用Console.ReadLine方法暂停程序的执行,以便查看游戏的结果。


原文地址: https://www.cveoy.top/t/topic/h32c 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录