C# 扫雷

分享一个扫雷小程序,这个程序是2012年9月的时候,我还在大学的时候写的,年代有点久远了。。。不过还是有些参考价值的

1、主要代码如下

using System;
//using System.Collections.Generic;
//using System.ComponentModel;
//using System.Data;
using System.Drawing;
//using System.Linq;
//using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace 扫雷2
{
    public partial class Form1 : Form
    {
        //*********************************
        public const int buttonSize = 40;
        public const int topSize = 40;
        private int time = 0;
        private int x = 10;//行数
        private int y = 15;//列数
        private int mineNum = 30;
        private int[][] arr;
        private int count;//计算翻开的个数
        private Thread td;//制作款式

        public void initArray()
        {
            arr = new int[x][];
            for (int i = 0; i < x; i++)
            {
                arr[i] = new int[y];
                for (int j = 0; j < y; j++)
                {
                    arr[i][j] = 0;
                }
            }
        }
        public void setMine()
        {
            int num = 0;
            int a,b;
            Random rd = new Random();
            for (int i = 0; i < x; i++) 
            {
                for (int j = 0; j < y; j++)
                {
                    arr[i][j] = 0;
                }
            }
            while (num < mineNum)
            {
                a = rd.Next(x);
                b = rd.Next(y);
                if (arr[a][b] != -1)
                {
                    arr[a][b] = -1;
                    num++;
                }
            }
        }
        private void figureCell(int a,int b)
        {
            if (arr[a][b] == -1)
                return;
            for(int i = a - 1; i < a + 2; i++)
                for (int j = b - 1; j < b + 2; j++)
                {
                    if (i < 0 || j < 0 || i >= x || j >= y)
                        continue;
                    if(arr[i][j]==-1)
                        arr[a][b]++;
                }
        }
        private void figureAll()
        {
            for(int i=0;i<x;i++)
                for (int j = 0; j < y; j++)
                {
                    figureCell(i, j);
                }
        }
        public void initButton()
        {
            for(int i = 0; i < x; i++)
                for (int j = 0; j < y; j++)
                {
                    Button button = new Button();
                    button.Name = "button_" + i +"_"+ j;
                    button.Size = new Size(buttonSize, buttonSize);
                    //Point p = new Point(5 + buttonSize * i, 5 + buttonSize * j);设置位置的时候 "Y要对应width,X对应height",要注意此时是与数组相反的
                    Point p = new Point(5 + buttonSize * j, 5 + topSize + buttonSize * i);
                    button.Location = p;
                    button.Font = new Font("宋体", 9, FontStyle.Bold | FontStyle.Underline);
                    this.Controls.Add(button);//将指定的控件添加到控件集合中
                    button.MouseUp+=new MouseEventHandler(button_MouseUp);
                }
            this.Size = new Size(10 + 16 + buttonSize * y, 10 + 38 + topSize + buttonSize * x);//窗口大小要加上窗体本身的边框,设置窗体大小的时候 "Y要对应width,X对应height",要注意此时是与数组相反的
        }
        public void playButtonBorderStyle(object oab)
        {
            int[] ab = (int[])oab;
            int a=ab[0];
            int b=ab[1];
            if(a<1||b<1)
                return;
            
            for (int j = (y-b)/2; j < (y+b)/2; j++)
            {
                try
                {
                    Button button = (Button)Controls.Find("button_" + (x - a) / 2 + "_" + j, false)[0];
                    //button.FlatStyle=FlatStyle.Flat;
                    button.BackColor = Color.White;
                }
                catch{}
                Thread.Sleep(20);
            }
            for (int i = (x-a)/2+1; i < (x+a)/2; i++)
            {
                try
                {
                    Button button=(Button)Controls.Find("button_"+i+"_"+((y+b)/2-1),false)[0];
                    //button.FlatStyle=FlatStyle.Flat;
                    button.BackColor = Color.White;
                }
                catch{}
                Thread.Sleep(20);
            }
            for (int j = (y+b)/2-2; j >= (y-b)/2; j--)
            {
                try
                {
                    Button button=(Button)Controls.Find("button_"+((x+a)/2-1)+"_"+j,false)[0];
                    //button.FlatStyle=FlatStyle.Flat;
                    button.BackColor = Color.White;
                }
                catch{}
                Thread.Sleep(20);
            }
            for (int i = (x+a)/2-2; i >= (x-a)/2+1; i--)
            {
                try
                {
                    Button button = (Button)Controls.Find("button_" + i + "_" + (y - b) / 2, false)[0];
                    //button.FlatStyle=FlatStyle.Flat;
                    button.BackColor = Color.White;
                }
                catch { }
                Thread.Sleep(20);
            }
            ab[0] -= 2;
            ab[1] -= 2;
            playButtonBorderStyle(ab);
        }
        public void showButton(Button button,int a, int b)
        {
            //SetControlEnabled(button, false);
            button.Enabled = false;
            button.BackColor = Color.White;
            switch (arr[a][b])
            {
                case -1:
                    button.Enabled = true;
                    button.ForeColor = Color.Red;
                    button.Text = "地雷";
                    showAll();
                    DialogResult result=MessageBox.Show( "不好意思,您输了!\n\n      再次挑战?","游戏失败",MessageBoxButtons.OKCancel,MessageBoxIcon.Information);//内容,标题,按钮,图标
                    if (result == DialogResult.OK)
                        reStart();
                    else
                        //this.Dispose();//释放所有资源
                        Application.Exit();//通知消息,在处理了消息后关闭所有应用程序的窗口
                    break;
                case 0:
                    count++;
                    button.Text = "";
                    for (int i = a - 1; i < a + 2;i++ )
                    {
                        for (int j = b - 1; j < b + 2;j++ )
                        {
                            if (i < 0 || j < 0 || i >= x || j >= y)
                                continue;
                            Button btn = (Button)Controls.Find("button_" + i + "_" + j,false)[0];//通过名称属性在窗体中搜索控件
                            if (btn.Enabled == false)//避免重复调用自己引起死循环
                                continue;
                            showButton(btn, i, j);
                        }
                    }
                    break;
                default :
                    count++;
                    button.Text = arr[a][b].ToString();
                    break;
            }
            Label label1 = (Label)Controls.Find("labelComplete", false)[0];
            label1.Text = String.Format("{0}%",(int)((float)count/(float)x/(float)y*100));
            if (count + mineNum == x * y)
            {
                DialogResult result=MessageBox.Show("恭喜您获得了胜利\n    再次挑战?", "游戏成功", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (result == DialogResult.Cancel)
                {
                    //this.Dispose();
                    Application.Exit();
                }
                else
                {
                    reStart();
                }
            }
        }
        public void showAll()
        {
            for(int i=0;i<x;i++)
                for (int j = 0; j < y; j++)
                {
                    Button button = (Button)Controls.Find("button_" + i + "_" + j,false)[0];
                    if (arr[i][j] == -1)
                    {
                        button.ForeColor = Color.Red;
                        button.Text = "地雷";
                    }
                    else if (arr[i][j] == 0)
                    {
                        button.Text = "";
                    }
                    else
                        button.Text = arr[i][j].ToString();
                }
        }
        public void button_MouseUp(Object sender, MouseEventArgs e)
        {
            Button button = (Button)sender;
            String[] str=button.Name.Split('_');
            int a = int.Parse(str[1]);
            int b = Convert.ToInt32(str[2]);
            if (e.Button == MouseButtons.Right&&e.Clicks==1)
            {
                if (button.Text == "红旗")
                {
                    button.Text = "";
                    button.ForeColor = Color.Black;
                }
                else
                {
                    button.ForeColor = Color.Red;
                    button.Text = "红旗";
                }
                return;
            }
            if (e.Button == MouseButtons.Left && e.Clicks == 1)
                showButton(button,a,b);
        }
        public void reStart()
        {
            setMine();
            figureAll();
            count = 0;
            time = 0;
            Label label1 = (Label)Controls.Find("labelTime", false)[0];
            label1.Text = "0";
            Label label2 = (Label)Controls.Find("labelComplete", false)[0];
            label2.Text = "0%";
            if (td != null)
            {
                td.Abort();
                td = null;
            }
            for (int i = 0; i < x; i++)
                for (int j = 0; j < y; j++)
                {
                    Button button = (Button)Controls.Find("button_" + i + "_" + j, false)[0];
                    button.Enabled = true;
                    button.BackColor = Color.Black;
                    button.ForeColor = Color.Black;
                    button.Text = "";
                }
            int[] xy={x,y};
            td = new Thread(playButtonBorderStyle);
            td.IsBackground = true;
            try
            {
                td.Start(xy);//如果在线程还没运行结束的时候关闭程序会引发异常
            }
            catch(Exception ex) { }//括号内的Exception ex不可省略,否则
        }
        public void initTop()
        {
            Label label1 = new Label();
            label1.Name = "label1";
            label1.Font=new Font("宋体",15);
            label1.Text = "时间:";
            //label1.BackColor = Color.Red;
            label1.TextAlign = ContentAlignment.MiddleCenter;//设置字体居中显示
            label1.Size = new Size(60, 30);
            label1.Location = new Point(12, 7);
            this.Controls.Add(label1);
            
            Label labelTime = new Label();
            labelTime.Name = "labelTime";
            labelTime.Font = new Font("宋体", 15);
            labelTime.Text = "0";
            labelTime.TextAlign = ContentAlignment.MiddleCenter;//设置字体居中显示
            labelTime.Size = new Size(60, 30);
            labelTime.Location = new Point(80, 7);
            labelTime.BorderStyle = BorderStyle.Fixed3D;
            this.Controls.Add(labelTime);
            
            Button btnRetart = new Button();
            btnRetart.Name = "btnRetart";
            btnRetart.Text = "重新开局";
            btnRetart.Size = new Size(100, 30);
            btnRetart.Location = new Point(150, 7);
            btnRetart.Click += new EventHandler(btnRestart_Click);
            this.Controls.Add(btnRetart);

            Label label2 = new Label();
            label2.Name = "label2";
            label2.Font = new Font("宋体", 15);
            label2.Text = "完成度:";
            label2.TextAlign = ContentAlignment.MiddleCenter;//设置字体居中显示           
            label2.Location = new Point(250, 7);//要先设置location,再设置size不然会错位
            label2.Size = new Size(80, 30);
            this.Controls.Add(label2);

            Label labelComplete = new Label();
            labelComplete.Name = "labelComplete";
            labelComplete.Font = new Font("宋体", 15);
            labelComplete.Text = "100%";
            labelComplete.TextAlign = ContentAlignment.MiddleCenter;//设置字体居中显示
            labelComplete.Location = new Point(330, 7);
            labelComplete.Size = new Size(60, 30);  
            labelComplete.BorderStyle = BorderStyle.Fixed3D;
            this.Controls.Add(labelComplete);
        }
        public void btnRestart_Click(object sender, EventArgs e)
        {
            reStart();
        }
        public void timeTick(object sender, EventArgs e)
        {
            time++;
            Label label = (Label)Controls.Find("labelTime", false)[0];
            label.Text = time.ToString();
        }

        public Form1()
        {
            InitializeComponent();
            this.Text = "扫雷";
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void Form1_Load(object sender, EventArgs e)
        {  
            initTop();
            initArray();
            setMine();
            figureAll();
            initButton();
            reStart();            
            System.Windows.Forms.Timer tm = new System.Windows.Forms.Timer();
            tm.Enabled = true;
            tm.Interval = 1000;
            tm.Tick += new EventHandler(timeTick);
            tm.Start();  
        }
    }
}
2、程序效果图如下:





  • 8
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值