C#控制台—生命游戏

小白学习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);
            }
        }

 









原文转载:http://www.shaoqun.com/a/504470.html

跨境电商:https://www.ikjzd.com/

史泰博:https://www.ikjzd.com/w/2112

敦煌网:https://www.ikjzd.com/w/189


小白学习ing......今天写了一下控制台的生命游戏:百度了一下规则:1.如果一个细胞周围有3个细胞为生(一个细胞周围共有8个细胞),则该细胞为生(即该细胞若原先为死,则转为生,若原先为生,则保持不变)。2.如果一个细胞周围有2个细胞为生,则该细胞的生死状态保持不变;3.在其它情况下,该细胞为死(即该细胞若原先为生,则转为死,若原先为死,则保持不变)。主要思路:1.初始化地图;staticvoi
blackbird:blackbird
跨境通电子商务平台:跨境通电子商务平台
奥地利风味美食 - :奥地利风味美食 -
桂林旅游在交通方面要注意什么?:桂林旅游在交通方面要注意什么?
"让未来,现在就来"跨境电商高峰论坛:"让未来,现在就来"跨境电商高峰论坛

Comments

Popular posts from this blog

指纹浏览器定制开发全面助力企业安全与智能升级

跨境电商资讯:一文带你走进亚马逊19大海

利用 Google 购物广告促进销量的初学者指南