抖音粉丝群1
『7x24小时有问必答』

AI浪潮下的计算机行业——从业者现状与未来展望

规则(来自百度百科,康威生命游戏词条)
游戏开始时,每个细胞随机地设定为“生”或“死”之一的某个状态。然后,根据某种规则,计算出下一代每个细胞的状态,画出下一代细胞的生死分布图。

应该规定什么样的迭代规则呢?需要一个简单的,但又反映生命之间既协同又竞争的生存定律。为简单起见,最基本的考虑是假设每一个细胞都遵循完全一样的生存定律;再进一步,把细胞之间的相互影响只限制在最靠近该细胞的8个邻居中。

也就是说,每个细胞迭代后的状态由该细胞及周围8个细胞状态所决定。作了这些限制后,仍然还有很多方法来规定“生存定律”的具体细节。例如,在康威的生命游戏中,规定了如下生存定律。
(1)当前细胞为死亡状态时,当周围有3个存活细胞时,则迭代后该细胞变成存活状态(模拟繁殖);若原先为生,则保持不变。
(2)当前细胞为存活状态时,当周围的邻居细胞低于两个(不包含两个)存活时,该细胞变成死亡状态(模拟生命数量稀少)。
(3)当前细胞为存活状态时,当周围有两个或3个存活细胞时,该细胞保持原样。
(4)当前细胞为存活状态时,当周围有3个以上的存活细胞时,该细胞变成死亡状态(模拟生命数量过多)。
控制台实现的关键接口
设置控制台游标的函数:public static void SetCursorPosition (int left, int top); 其中left参数是列,top参数是行。
设置控制台背景色的属性:public static ConsoleColor BackgroundColor { get; set; } 黑色用来表示生存的细胞,白色用来表示死亡的细胞。
代码实现
using System;

using System.Threading;

namespace CellularAutomata

{

    class Program

    {

        private static int gridRowCol = 32;

        private static Cell[,] grid = new Cell[gridRowCol, gridRowCol];

        private static int sleepMs = 33;

        private static int initAlivePossibility = 4; // 4 means 1/4

        static void Main(string[] args)

        {

            try

            {

                Init();

                // Main loop

                while (true)

                {

                    Update();

                    Thread.Sleep(sleepMs);

                }

            }

            catch (Exception e)

            {

                Console.WriteLine(e);

                Console.ReadKey();

            }

        }

        private static void Init()

        {

            // Set Console Size

            Console.BufferHeight = 256;

            Console.BufferWidth = 256;

            Console.WindowWidth = 256;

            Console.WindowHeight = 80;

            Random random = new Random();

            for (int i = 0; i < grid.GetLength(0); i++)

            {

                for (int j = 0; j < grid.GetLength(1); j++)

                {

                    grid[i, j] = new Cell();

                    int value = random.Next(0, initAlivePossibility);

                    if (value == 0)

                    {

                        grid[i, j].Value = true;

                    }

                    else

                    {

                        grid[i, j].Value = false;

                    }

                }

            }

        }

        private static void Update()

        {

            for (int i = 0; i < grid.GetLength(0); i++)

            {

                for (int j = 0; j < grid.GetLength(1); j++)

                {

                    int aliveCount = NeighborAliveCount(i, j);

                    if (grid[i, j].Value) // Alive

                    {

                        if (aliveCount < 2 || aliveCount > 3)

                        {

                            grid[i, j].Value = false;

                        }

                    }

                    else // Death

                    {

                        if (aliveCount == 3)

                        {

                            grid[i, j].Value = true;

                        }

                    }

                    if (grid[i, j].Value)

                    {

                        SetAlive(i, j);

                    }

                    else

                    {

                        SetDeath(i, j);

                    }

                }

            }

        }

        private static int NeighborAliveCount(int i, int j)

        {

            int count = 0;

            for (int m = i - 1; m <= i + 1; m++)

            {

                for (int n = j - 1; n <= j + 1; n++)

                {

                    if (m == i && n == j) continue;

                    if (m < 0 || m >= grid.GetLength(0)) continue;

                    if (n < 0 || n >= grid.GetLength(1)) continue;

                    if (grid[m, n].Value) count++;

                }

            }

            return count;

        }

        private static void SetAlive(int i, int j)

        {

            string aliveStr = "  ";

            Console.SetCursorPosition(j * aliveStr.Length, i);

            Console.BackgroundColor = ConsoleColor.Black;

            Console.Write(aliveStr);

        }

        private static void SetDeath(int i, int j)

        {

            string deathStr = "  ";

            Console.SetCursorPosition(j * deathStr.Length, i);

            Console.BackgroundColor = ConsoleColor.White;

            Console.Write(deathStr);

        }

    }

    public class Cell

    {

        public bool Value { get; set; }

    }

}


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

上一主题上一主题         下一主题下一主题
QQ手机版小黑屋粤ICP备17165530号

Copyright 2010-2015. All rights reserved. 

微信公众号二维码 抖音二维码 百家号二维码 今日头条二维码哔哩哔哩二维码