用C#实现的生命游戏

来源:互联网 发布:windows内核实验教程 编辑:程序博客网 时间:2024/04/30 08:05

最近在学习人工智能,其中涉及到一个生命游戏的算法挺好玩,于是我用C#实现了,现在拿出来和大家共享,由于写的程序不是很多,这个程序肯定有很多不合理的地方,希望大家和我共同讨论,程序如下:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

 

namespace 生命

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private bool[,] struArr = new bool[50, 50];

        private bool[,] struArr2 = new bool[50, 50];

        private Color blockColor=Color.Red ;

        //label中画出格子

        private void label1_Paint(object sender, PaintEventArgs e)

        {

            Graphics gp = e.Graphics;

            gp.Clear(Color.Black);

            Pen p = new Pen(Color.White);

            SolidBrush s = new SolidBrush(Color.White);

            for (int i = 11; i < 551; i = i + 11)

                gp.DrawLine(p,1,i ,550,i );

            for (int i = 11; i < 551; i = i + 11)

                gp.DrawLine(p, i, 1, i, 550);

        }

 

 

        //实现生命游戏初始状态的设置,即用鼠标在画的格子中选择初始的生命格子

        private void label1_MouseClick(object sender, MouseEventArgs e)

        {

            if (e.Button != MouseButtons.Left)

                return;

            int xPos, yPos;

            xPos = e.X / 11;

            yPos = e.Y / 11;

            struArr[xPos ,yPos]=!struArr[xPos,yPos];

            bool b =struArr[xPos,yPos];

            Graphics gp = label1.CreateGraphics();

            SolidBrush s = new SolidBrush(b ? blockColor : Color.Black);

            gp.FillRectangle(s ,11*xPos+1,11*yPos+1,10,10);

            gp.Dispose();

                    

        }

 

        //生命游戏的主程序

        private void button1_Click(object sender, EventArgs e)

        {

            for (int i = 0; i <50; i++)

                for (int j = 0; j <50; j++)

                    struArr2[i, j] = struArr[i, j];

 

            for (int i = 1; i <49; i++)

              for (int j = 1; j < 49; j++)

              {

                 int count = 0;

                 if (struArr[i - 1, j - 1] == true) { count++; }

                 if (struArr[i - 1, j] == true) { count++; }

                 if (struArr[i - 1, j + 1] == true) { count++; }

                 if (struArr[i , j-1] == true) { count++; }

                 if (struArr[i, j +1] == true) { count++; }

                 if (struArr[i + 1, j-1] == true) { count++; }

                 if (struArr[i + 1, j] == true) { count++; }

                 if (struArr[i + 1, j+1] == true) { count++; }

                 switch (count)

                 {case 3:

                     struArr2[i, j] = true;

                     break;

                  case 2:

                      if (struArr[i, j] == true)

                      { struArr2[i, j] = true; }

                      break;

 

                  default:

                      struArr2[i, j] = false;

                     break;

 

 

 

                 }

            }

             for (int i = 0; i <50; i++)

               for (int j = 0; j <50; j++)

               { SolidBrush s = new SolidBrush(struArr2[i,j] ? blockColor : Color.Black);

                 Graphics gp = label1.CreateGraphics();

                   gp.FillRectangle(s ,11*i+1,11*j+1,10,10);

                   gp.Dispose();

               }

           for (int i = 0; i < 50; i++)

               for (int j = 0; j < 50; j++)

                   struArr[i, j] = struArr2[i, j];

        }

 

    }

}

 

原创粉丝点击