当前位置: 首页 > 开发语言 > C# > C#控制台—生命游戏

C#控制台—生命游戏

时间:2021-01-09 14:03来源:未知 点击:
分享到:

小白学习ing......

 

今天写了一下控制台的生命游戏:

百度了一下规则:
1. 如果一个细胞周围有3个细胞为生(一个细胞周围共有8个细胞),则该细胞为生(即该细胞若原先为死,则转为生,若原先为生,则保持不变) 。
2. 如果一个细胞周围有2个细胞为生,则该细胞的生死状态保持不变;
3. 在其它情况下,该细胞为死(即该细胞若原先为生,则转为死,若原先为死,则保持不变)。

主要思路:

1.初始化地图;

        static void InitMapData(int[,] map, int[,] temp , int rowCount, int colCount)
        {
            for (int row = 0; row < rowCount; row++)
            {
                for (int col = 0; col < colCount; col++)
                {
                    Thread.Sleep(10);//每次进来让主线程休息  停10毫秒
                    Random ran = new Random();
                    int val = ran.Next(0, 2);
                    map[row, col] = val;
                    temp[row, col] = val;
                }
            }
        }

2.改变生命状态;

        static void ChangLifeState(int[,] map, int[,] temp, int rowCount, int colCount)
        {
            //跳过第0个,保证最外圈不会变,避免超过索引
            for (int row = 1; row < rowCount-1; row++)
            {
                for (int col = 1; col < colCount-1; col++)
                {
                    //周围生命的个数
                    int count = 0;
                    //找周围的个数
                    for (int a = row - 1; a <= row + 1; a++)
                    {
                        for (int b = col - 1; b <= col + 1; b++)
                        {
                            if (row == a && col == b)
                            {
                            }
                            else if (map[a, b] == 1)
                            {
                                count++;
                            }
                        }
                    }
                    if (count == 3)
                    {
                        //修改临时的数组的生命状态
                        temp[row, col] = 1;
                    }
                    else if (count == 2)
                    {
                    }
                    else
                    {
                        temp[row, col] = 0;
                    }
                }
            }
        }

3.把临时变化的数组替换到地图的数组;

        static void CopyTempToMap(int[,] map, int[,] temp, int rowCount, int colCount)
        {
            for (int row = 0; row < rowCount; row++)
            {
                for (int col = 0; col < colCount; col++)
                {
                    map[row, col] = temp[row, col];
                }
            }
        }

4.画地图;

        static void DrawWorld(int[,] map,int rowCount,int colCount)
        {
            Console.Clear();
            for (int row = 0; row < rowCount; row++)
            {
                for (int col = 0; col < colCount; col++)
                {
                    if (map[row, col] == 1)//活的生命
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                    }
                    else//死的生命
                    {
                        Console.ForegroundColor = ConsoleColor.White;
                    }
                    Console.Write("■");
                }
                Console.WriteLine();
            }
        }

5.主函数定义地图的行和列、存储地图的二维数组、临时变化的二维数组;执行上面的方法;

        static void Main(string[] args)
        {
            //Console.WriteLine("输入行数");
            //int rowCount = int.Parse(Console.ReadLine());
            //Console.WriteLine("输入列数");
            //int colCount = int.Parse(Console.ReadLine());
            int rowCount = 25;
            int colCount = 25;
            //定义世界地图的二维数组
            int[,] map = new int[rowCount, colCount];
            //临时修改的二维数组
            int[,] tempMap = new int[rowCount, colCount];
            //初始化地图
            InitMapData(map ,tempMap , rowCount, colCount);
 
            while (true)
            {
                ChangLifeState(map, tempMap, rowCount, colCount);
                CopyTempToMap(map, tempMap, rowCount, colCount);
                //画地图
                DrawWorld(map, rowCount, colCount);
                //Console.ReadKey();
                Thread.Sleep(100);
            }
        }
(责任编辑:admin)
推荐教程
关于我们--广告服务--免责声明--本站帮助-友情链接--版权声明--联系我们--网站地图          黑ICP备17004983号-1