生命游戏代码(C#)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;


namespace 生命游戏
{
    class Program
    {
        static List<Cell> cells = new List<Cell>();
        static List<Cell> GaiBian = new List<Cell>();
        static void Main(string[] args)
        {            
            Cell[,] CellList = new Cell[10, 10];
            Init(CellList);
            int n = 1;
            Console.WriteLine("num:"+ n++);
            Show(CellList);
            while (true)
            {
                Thread.Sleep(1000);
                
                for (int i = cells.Count - 1; i >= 0; i--)
                {
                    ruleH(cells[i], CellList);
                }              
  
                for (int i = 0; i < GaiBian.Count; i++)
                {
                    GaiBian[i].Bl = GaiBian[i].BlEnd;
                }
                Console.Clear();
                Console.WriteLine("num:" + n++);
                Show(CellList);
            }                  


        }


        public static void ruleH(Cell cell,Cell[,] CellList)
        {
            //细胞周围有两个活着的则不改变
            //细胞周围有三个则转为生
            //其他则转为死
            int n = 0;//活细胞数量
            for (int i = -1; i <= 1; i++)
            {
                for (int j = -1; j <= 1; j++)
                {
                    if (cell.X+i < 0 || cell.X+i > 9)
                    {
                        break;
                    }
                    if (cell.Y+j < 0 || cell.Y+j > 9)
                    {
                        continue;
                    }
                    if (!(0 == i && j == 0))
                    {
                        if (CellList[cell.X + i, cell.Y + j].Bl == true)
                        {
                            n++;
                        }
                        else
                        {
                            ruleS(CellList[cell.X + i, cell.Y + j], CellList);
                        }
                    }
                }
            }
            if (n == 3)
            {
                cell.BlEnd = true;
                if (!cells.Contains(cell))
                {
                    cells.Add(cell);
                }                
            }
            else if (n == 2)
            {
            }
            else
            {
                cell.BlEnd = false;
                if (cells.Contains(cell))
                {
                    cells.Remove(cell);
                }                
                GaiBian.Add(cell);
            }
        }


        public static void ruleS(Cell cell, Cell[,] CellList)
        {
            int n = 0;
            for (int i = -1; i <= 1; i++)
            {
                for (int j = -1; j <= 1; j++)
                {
                    if (cell.X + i < 0 || cell.X + i > 9)
                    {
                        break;
                    }
                    if (cell.Y + j < 0 || cell.Y + j > 9)
                    {
                        continue;
                    }
                    if (!(0 == i && j == 0))
                    {
                        if (CellList[cell.X + i, cell.Y + j].Bl == true)
                        {
                            n++;
                        }
                    }
                }
            }
            if (n == 3)
            {
                cell.BlEnd = true;
                if (!cells.Contains(cell))
                {
                    cells.Add(cell);
                }
                GaiBian.Add(cell);
            }
            else if (n == 2)
            {
            }
            else
            {
                cell.BlEnd = false;
                if (cells.Contains(cell))
                {
                    cells.Remove(cell);
                }
            }
        }


        public static void Init(Cell[,] CellList)
        {
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    Cell cell = new Cell();
                    cell.X = i;
                    cell.Y = j;
                    CellList[i,j] = cell;
                }
            }


            int x, y;
            int n = 1;
            Console.WriteLine("请输入第" + n + "个活细胞细胞的位置");
            Console.WriteLine("X轴坐标:");
            x = Convert.ToInt32(Console.ReadLine()) - 1;
            Console.WriteLine("Y轴坐标:");
            y = Convert.ToInt32(Console.ReadLine()) - 1;
            while(!(x>9||x<0||y>9||y<0||n>100))
            {
                CellList[x,y].Bl = true;
                cells.Add(CellList[x, y]);
                Console.WriteLine("请输入第" + n + "个活细胞细胞的位置");
                Console.WriteLine("X轴坐标:");
                x = Convert.ToInt32(Console.ReadLine()) - 1;
                Console.WriteLine("Y轴坐标:");
                y = Convert.ToInt32(Console.ReadLine()) - 1;                
                n++;
            }             
        }


        public static void Show(Cell[,] CellList)
        {
            for(int i = 0;i < 10;i ++)
            {
                for(int j =0; j < 10;j ++)
                {
                    Console.Write(CellList[j,i].Bl?"#":"*");
                }
                Console.Write("\n");
            }
        }
    }


    class Cell
    {
        private int x; //细胞位置


        public int X
        {
            get { return x; }
            set { x = value; }
        }
        private int y; //细胞位置


        public int Y
        {
            get { return y; }
            set { y = value; }
        }
        private Boolean bl = false;//当前生死状态


        public Boolean Bl
        {
            get { return bl; }
            set { bl = value; }
        }
        private Boolean blEnd; //之后生死状态


        public Boolean BlEnd
        {
            get { return blEnd; }
            set { blEnd = value; }
        }
    }
}
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值