模擬一下細胞的繁殖

原貼地址 
原帖:

1. 若是一個細胞只有0或1個鄰居,它將由於孤獨而死;
2. 若是一個細胞有4到8個鄰居,它將由於擁擠而死;
3. 若是一個細胞恰有2或者3個鄰居,它將繼續生存下去;
4. 若是一個空格子恰有3個鄰居,將「生」出一個新細胞;
5. 其餘的空格子繼續維持原狀。

提示:

細胞,能夠用對象來存儲, 屬性是: 編號隨機不重複,死,活,鄰居數量,鄰居集合
( 用鏈表來存放其餘細胞集合) 開始輸入隨機個細胞和鄰居隨機組合,而後每1秒一個週期,演示發展結果html

 

個人實現:數組


using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace ArtificialLife
{
    
public partial class FormMain : Form
    {
        
int[,] Cells; //狀態(1生,0死)
        int[,] CellCounts; //周邊細胞數量(最大8個)
        int[,] Temp; //緩衝(切換生死狀態)
        Bitmap memBitmap; //畫布
        Random rand = new Random((int)DateTime.Now.Ticks); 
        System.Windows.Forms.Timer updateTimer;
        
public FormMain()
        {
            InitializeComponent();
            
//寬高
            Width = 320
            Height 
= 240;
            memBitmap 
= new Bitmap(Width, Height);
            Cells 
= new int[memBitmap.Width, memBitmap.Height];
            CellCounts 
= new int[memBitmap.Width, memBitmap.Height];
            
//初始化,隨機決定
            for (var x = 0; x < memBitmap.Width; x++)
                
for (var y = 0; y < memBitmap.Height; y++)
                    Cells[x, y] 
= rand.Next(memBitmap.Width) % 3 == 0 ? 1 : 0;
            Temp 
= (int[,])Cells.Clone();
            updateTimer 
= new Timer();
            updateTimer.Tick 
+= new EventHandler(updateTimer_Tick);
            updateTimer.Interval 
= 1000;
            updateTimer.Start();
        }

        
void updateTimer_Tick(object sender, EventArgs e)
        {
            DoCalc();
            DoDraw();
        }

        
private void DoCalc()
        {
          
/*
           1. 若是一個細胞只有0或1個鄰居,它將由於孤獨而死; 
            2. 若是一個細胞有4到8個鄰居,它將由於擁擠而死; 
            3. 若是一個細胞恰有2或者3個鄰居,它將繼續生存下去; 
            4. 若是一個空格子恰有3個鄰居,將「生」出一個新細胞; 
            
*/
           
int nCount = 0;//用以統計每一個細胞周圍的細胞個數
            for (var x = 0; x < memBitmap.Width; x++)
                
for (var y = 0; y < memBitmap.Height; y++)
                {
                    
//每一個細胞的先後左右八個方向的
                    nCount = 0;
                    
if (x > 0) nCount += Cells[x - 1, y];
                    
if (x > 0 && y > 0) nCount += Cells[x - 1, y - 1];
                    
if (x < memBitmap.Width - 1 && y < memBitmap.Height - 1) nCount += Cells[x + 1, y + 1];
                    
if (y > 0) nCount += Cells[x, y - 1];
                    
if (y > 0 && x < memBitmap.Width - 1) nCount += Cells[x + 1, y - 1];
                    
if (x > 0 && y < memBitmap.Height - 1) nCount += Cells[x - 1, y + 1];
                    
if (x < memBitmap.Width - 1) nCount += Cells[x + 1, y];
                    
if (y < memBitmap.Height - 1) nCount += Cells[x, y + 1];
                    CellCounts[x, y] 
= nCount; //放入計數器
                    if (nCount < 2 || nCount > 3)//決定生死
                        Temp[x, y] = 0;
                    
if (nCount == 3)
                        Temp[x, y] 
= 1;
                }
            
for (var x = 0; x < memBitmap.Width; x++)
                
for (var y = 0; y < memBitmap.Height; y++)
                    Cells[x, y] 
= Temp[x, y];
        }

        
protected override void OnPaint(PaintEventArgs e)
        {
            DoDraw();
        }

        
private void DoDraw()
        {
            
//開啓編譯選項 unsafe+
            BitmapData bmData = memBitmap.LockBits(new Rectangle(00, memBitmap.Width, memBitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            
int stride = bmData.Stride;
            IntPtr Scan0 
= bmData.Scan0;
            
unsafe
            {
                
byte* p = (byte*)(void*)Scan0;
                
int nOffset = stride - memBitmap.Width * 3;
                
for (int y = 0; y < memBitmap.Height; ++y)
                {
                    
for (int x = 0; x < memBitmap.Width; ++x)
                    {
                        
//blue = p[0];
                        
//green = p[1];
                        
//red = p[2];
                        
//當週邊細胞數越多則顏色越亮
                        p[0= p[1= p[2= (byte)Math.Max(Math.Min(CellCounts[x, y] * 30255), 0);
                        
if (Cells[x, y] == 1) p[0= 255//生存的細胞
                        p += 3;
                    }
                    p 
+= nOffset;
                }
            }
            memBitmap.UnlockBits(bmData);
            Graphics g 
= this.CreateGraphics();
            g.DrawImage(memBitmap, ClientRectangle);
            g.Dispose();
        }

    }
}

 

只因此用數組,是爲了簡單和執行效率;運行效果如圖:dom

局部放大效果ide

源碼(visual studio 2008 項目工程)post

 ArtificialLife.rarthis

 

出處:http://www.cnblogs.com/Chinasf/archive/2008/11/26/1341399.htmlspa

相關文章
相關標籤/搜索