C#控制檯—生命遊戲

小白學習ing......數組

 

今天寫了一下控制檯的生命遊戲:dom

百度了一下規則:
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];
                }
            }
        }string

4.畫地圖;it

        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);
            }
        }
相關文章
相關標籤/搜索