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

using System;

class GameOfLife
{
    private int rows;
    private int cols;
    private int[,] grid;

    public GameOfLife(int rows, int cols)
    {
        this.rows = rows;
        this.cols = cols;
        this.grid = new int[rows, cols];
        InitializeGrid();
    }

    public void InitializeGrid()
    {
        Random random = new Random();
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < cols; j++)
            {
                grid[i, j] = random.Next(2);
            }
        }
    }

    public void PrintGrid()
    {
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < cols; j++)
            {
                Console.Write(grid[i, j] == 1 ? "■" : " ");
            }
            Console.WriteLine();
        }
    }

    public void NextGeneration()
    {
        int[,] newGrid = new int[rows, cols];

        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < cols; j++)
            {
                int neighbors = CountNeighbors(i, j);

                if (grid[i, j] == 1)
                {
                    if (neighbors < 2 || neighbors > 3)
                    {
                        newGrid[i, j] = 0;
                    }
                    else
                    {
                        newGrid[i, j] = 1;
                    }
                }
                else
                {
                    if (neighbors == 3)
                    {
                        newGrid[i, j] = 1;
                    }
                    else
                    {
                        newGrid[i, j] = 0;
                    }
                }
            }
        }

        grid = newGrid;
    }

    private int CountNeighbors(int row, int col)
    {
        int count = 0;

        for (int i = row - 1; i <= row + 1; i++)
        {
            for (int j = col - 1; j <= col + 1; j++)
            {
                if (i >= 0 && i < rows && j >= 0 && j < cols && !(i == row && j == col))
                {
                    count += grid[i, j];
                }
            }
        }

        return count;
    }
}

class Program
{
    static void Main(string[] args)
    {
        int rows = 10;
        int cols = 10;

        GameOfLife game = new GameOfLife(rows, cols);

        Console.WriteLine("初始状态:");
        game.PrintGrid();
        Console.WriteLine();

        for (int generation = 1; generation <= 5; generation++)
        {
            Console.WriteLine("第 {0} 代:", generation);
            game.NextGeneration();
            game.PrintGrid();
            Console.WriteLine();
        }

        Console.ReadLine();
    }
}

在示例代码中,我们定义了一个GameOfLife类来表示生命游戏。该类包含以下方法:

  • InitializeGrid:随机初始化细胞的状态。
  • PrintGrid:打印当前细胞的生死分布图。
  • NextGeneration:根据生存定律计算下一代细胞的状态。
  • CountNeighbors:计算指定细胞周围存活细胞的数量。

Main方法中,我们创建一个GameOfLife对象,并进行5代的迭代。每一代都会打印当前细胞的生死分布图。

请注意,示例代码中的细胞状态使用整数表示,其中1表示生,0表示死。打印时,生细胞用黑色方块表示,死细胞用空格表示。


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

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