小白學習ing......數組
今天寫了一下控制檯的生命遊戲:dom
百度了一下規則:
1. 若是一個細胞周圍有3個細胞爲生(一個細胞周圍共有8個細胞),則該細胞爲生(即該細胞若原先爲死,則轉爲生,若原先爲生,則保持不變) 。
2. 若是一個細胞周圍有2個細胞爲生,則該細胞的生死狀態保持不變;
3. 在其它狀況下,該細胞爲死(即該細胞若原先爲生,則轉爲死,若原先爲死,則保持不變)。函數
主要思路:學習
1.初始化地圖;線程
2.改變生命狀態;索引
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.主函數定義地圖的行和列、存儲地圖的二維數組、臨時變化的二維數組;執行上面的方法;