使用C#(Winform)实现扫雷的核心代码

6 篇文章 1 订阅
3 篇文章 0 订阅

使用C#(Winform)实现扫雷的核心代码

扫雷作为一款风靡全世界的游戏,赢得了许多玩家的热爱。我们今天将来做一个程序员该做的,扫雷是如何实现的?
我的游戏制作是一个C#小白所能看懂的,并没有涉及高深的数据结构和算法。各位有兴趣可以使用一些算法来优化和完善我的程序,但是本程序有许多不足之处也是我们有目共睹的。
对于游戏的优化可以将所有的遍历换成图形结构的广度或深度优先算法,而顺序存储在我的程序中是能够很好的解决时间复杂度的问题的。当然如果方块增多我们也可以使用链式存储的方法配合一系列树形查找方法来进行游戏优化。

开始游戏界面:

界面

设计代码:

namespace 扫雷
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.pictureBox6 = new System.Windows.Forms.PictureBox();
            this.pictureBox5 = new System.Windows.Forms.PictureBox();
            this.pictureBox4 = new System.Windows.Forms.PictureBox();
            this.pictureBox3 = new System.Windows.Forms.PictureBox();
            this.pictureBox2 = new System.Windows.Forms.PictureBox();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox6)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox5)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox4)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // pictureBox6
            // 
            this.pictureBox6.Image = global::扫雷.Properties.Resources.maxsocre___副本__2_;
            this.pictureBox6.Location = new System.Drawing.Point(-5, 225);
            this.pictureBox6.Name = "pictureBox6";
            this.pictureBox6.Size = new System.Drawing.Size(201, 80);
            this.pictureBox6.TabIndex = 5;
            this.pictureBox6.TabStop = false;
            this.pictureBox6.Click += new System.EventHandler(this.pictureBox6_Click);
            // 
            // pictureBox5
            // 
            this.pictureBox5.Image = global::扫雷.Properties.Resources.maxsocre;
            this.pictureBox5.Location = new System.Drawing.Point(-5, 155);
            this.pictureBox5.Name = "pictureBox5";
            this.pictureBox5.Size = new System.Drawing.Size(201, 75);
            this.pictureBox5.TabIndex = 4;
            this.pictureBox5.TabStop = false;
            this.pictureBox5.Click += new System.EventHandler(this.pictureBox5_Click);
            // 
            // pictureBox4
            // 
            this.pictureBox4.Image = global::扫雷.Properties.Resources.maxsocre___副本;
            this.pictureBox4.Location = new System.Drawing.Point(-5, 79);
            this.pictureBox4.Name = "pictureBox4";
            this.pictureBox4.Size = new System.Drawing.Size(201, 80);
            this.pictureBox4.TabIndex = 3;
            this.pictureBox4.TabStop = false;
            // 
            // pictureBox3
            // 
            this.pictureBox3.Image = global::扫雷.Properties.Resources.exitgame1;
            this.pictureBox3.Location = new System.Drawing.Point(-5, 301);
            this.pictureBox3.Name = "pictureBox3";
            this.pictureBox3.Size = new System.Drawing.Size(201, 83);
            this.pictureBox3.TabIndex = 2;
            this.pictureBox3.TabStop = false;
            this.pictureBox3.Click += new System.EventHandler(this.pictureBox3_Click);
            // 
            // pictureBox2
            // 
            this.pictureBox2.Image = global::扫雷.Properties.Resources.startgame;
            this.pictureBox2.Location = new System.Drawing.Point(-5, 0);
            this.pictureBox2.Name = "pictureBox2";
            this.pictureBox2.Size = new System.Drawing.Size(201, 82);
            this.pictureBox2.TabIndex = 1;
            this.pictureBox2.TabStop = false;
            this.pictureBox2.Click += new System.EventHandler(this.pictureBox2_Click);
            // 
            // pictureBox1
            // 
            this.pictureBox1.Image = global::扫雷.Properties.Resources._1_13092R12412951;
            this.pictureBox1.Location = new System.Drawing.Point(195, 0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(493, 384);
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(687, 380);
            this.Controls.Add(this.pictureBox6);
            this.Controls.Add(this.pictureBox5);
            this.Controls.Add(this.pictureBox4);
            this.Controls.Add(this.pictureBox3);
            this.Controls.Add(this.pictureBox2);
            this.Controls.Add(this.pictureBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox6)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox5)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox4)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.PictureBox pictureBox2;
        private System.Windows.Forms.PictureBox pictureBox3;
        private System.Windows.Forms.PictureBox pictureBox4;
        private System.Windows.Forms.PictureBox pictureBox5;
        private System.Windows.Forms.PictureBox pictureBox6;
    }
}


后台操作核心代码:

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



namespace 扫雷
{
    public partial class Form1 : Form
    {
        public Form1()
        {

            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.MaximizeBox = false;
            InitializeComponent();
            this.Text = "扫雷";
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            Form3 f3 = new Form3();
            this.Hide();
            f3.Show();
        }

        private void pictureBox6_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.Show();
        }

        private void pictureBox5_Click(object sender, EventArgs e)
        {
            MessageBox.Show("1、基本操作,鼠标左键点击,右键进行标注。注意右键可标注雷和问号。问号代表不确定。游戏左上方数字代表雷数,右上方数字代表时间。左方的数字你每标注一个雷就少一个,就算标注错误也会少。\n2、如果我们运气不是很差的话,应该会点出一片空白区,以下图为例子有一个数字是1,然后旁边的7个方块全部消失,只有一个方块还存在,那么这个方块就是地雷,我们点击右键标注。\n", "游戏帮助");
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
    public class nandu
    {
        static public int flag = 1;
        static public int[,] ttArray1 = new int[10,10];
        static public int[,] ttArray2 = new int[25,16];
        static public int[,] ttArray3 = new int[20,45];
    }
}

这里的难度1的方块个数为1010=100个。
难度2的方块个数为25
16=400个。
难度3的方块个数为20*45=900个。
都是动态生成的。所以当游戏开的那一刻,CPU会有很大压力。

难度选择设计

界面:
难度选择

设计界面代码:

namespace 扫雷
{
    partial class Form2
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.pictureBox4 = new System.Windows.Forms.PictureBox();
            this.pictureBox3 = new System.Windows.Forms.PictureBox();
            this.pictureBox2 = new System.Windows.Forms.PictureBox();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox4)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // pictureBox4
            // 
            this.pictureBox4.Image = global::扫雷.Properties.Resources._9f69e4a4f5d12f02d1615615003e1d33___副本__2_;
            this.pictureBox4.Location = new System.Drawing.Point(77, 287);
            this.pictureBox4.Name = "pictureBox4";
            this.pictureBox4.Size = new System.Drawing.Size(361, 83);
            this.pictureBox4.TabIndex = 3;
            this.pictureBox4.TabStop = false;
            this.pictureBox4.Click += new System.EventHandler(this.pictureBox4_Click);
            // 
            // pictureBox3
            // 
            this.pictureBox3.Image = global::扫雷.Properties.Resources._9f69e4a4f5d12f02d1615615003e1d33___副本__3_;
            this.pictureBox3.Location = new System.Drawing.Point(83, 182);
            this.pictureBox3.Name = "pictureBox3";
            this.pictureBox3.Size = new System.Drawing.Size(348, 85);
            this.pictureBox3.TabIndex = 2;
            this.pictureBox3.TabStop = false;
            this.pictureBox3.Click += new System.EventHandler(this.pictureBox3_Click);
            // 
            // pictureBox2
            // 
            this.pictureBox2.Image = global::扫雷.Properties.Resources._9f69e4a4f5d12f02d1615615003e1d33___副本;
            this.pictureBox2.Location = new System.Drawing.Point(75, 82);
            this.pictureBox2.Name = "pictureBox2";
            this.pictureBox2.Size = new System.Drawing.Size(348, 81);
            this.pictureBox2.TabIndex = 1;
            this.pictureBox2.TabStop = false;
            this.pictureBox2.Click += new System.EventHandler(this.pictureBox2_Click);
            // 
            // pictureBox1
            // 
            this.pictureBox1.Image = global::扫雷.Properties.Resources._9f69e4a4f5d12f02d1615615003e1d33;
            this.pictureBox1.Location = new System.Drawing.Point(0, 0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(517, 451);
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            // 
            // Form2
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(514, 448);
            this.Controls.Add(this.pictureBox4);
            this.Controls.Add(this.pictureBox3);
            this.Controls.Add(this.pictureBox2);
            this.Controls.Add(this.pictureBox1);
            this.Name = "Form2";
            this.Text = "Form2";
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox4)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.PictureBox pictureBox2;
        private System.Windows.Forms.PictureBox pictureBox3;
        private System.Windows.Forms.PictureBox pictureBox4;
    }
}

后台操作核心代码:

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

namespace 扫雷
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.MaximizeBox = false;
            InitializeComponent();
            this.Text = "难度选择";

        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("目前难度已经调节为:“深渊”", "提醒");
            nandu.flag = 3;
            this.Hide();
            
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            MessageBox.Show("目前难度已经调节为:“困难”", "提醒");
            nandu.flag = 2;
            this.Hide();
        }

        private void pictureBox4_Click(object sender, EventArgs e)
        {
            MessageBox.Show("目前难度已经调节为:“简单”", "提醒");
            nandu.flag = 1;
            this.Hide();
        }
    }
}

这里都使用了全局Form类中的flag私有变量来传递給Form3界面让其生成方块。

游戏界面

游戏界面设计器代码:
namespace 扫雷
{
partial class Form3
{
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.label1 = new System.Windows.Forms.Label();
        this.label2 = new System.Windows.Forms.Label();
        this.label3 = new System.Windows.Forms.Label();
        this.SuspendLayout();
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(0, 0);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(0, 12);
        this.label1.TabIndex = 0;
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.Font = new System.Drawing.Font("楷体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
        this.label2.Location = new System.Drawing.Point(231, 19);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(62, 16);
        this.label2.TabIndex = 1;
        this.label2.Text = "label2";
        // 
        // label3
        // 
        this.label3.AutoSize = true;
        this.label3.Font = new System.Drawing.Font("楷体", 12F, System.Drawing.FontStyle.Bold);
        this.label3.Location = new System.Drawing.Point(299, 19);
        this.label3.Name = "label3";
        this.label3.Size = new System.Drawing.Size(62, 16);
        this.label3.TabIndex = 2;
        this.label3.Text = "label3";
        // 
        // Form3
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(800, 450);
        this.Controls.Add(this.label3);
        this.Controls.Add(this.label2);
        this.Controls.Add(this.label1);
        this.Name = "Form3";
        this.Text = "Form3";
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label label3;
}

游戏界面操作代码:

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

namespace 扫雷
{

    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();

            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            if (nandu.flag == 1)
            {
                this.Text = "扫雷 难度[简单]";
            }
            else if (nandu.flag == 2)
            {
                this.Text = "扫雷 难度[困难]";
            }
            else if (nandu.flag == 3)
            {
                this.Text = "扫雷 难度[深渊]";
            }
            int mines = (nandu.flag * nandu.flag) * 10;
            this.WindowState = FormWindowState.Maximized;
            label2.Text = "目前雷数为";
            label3.Text = Convert.ToString(mines);
            this.Form3_Load();
            GetArray();
        }
        
        public static int GetArray()
            {
                int x;
                int y;
                int mines = (nandu.flag * nandu.flag) * 10; ;

                Random seed = new Random(DateTime.Now.Second);  //设置随机
                if (nandu.flag == 1)
                {
                    for (int i = 0; i < 10; i++)
                    {
                        for (int j = 0; j < 10; j++)
                        {
                            nandu.ttArray1[i,j] = 1;
                        }
                    }
                }
                else if (nandu.flag == 2)
                {
                    for (int i = 0; i < 25; i++)
                    {
                        for (int j = 0; j < 16; j++)
                        {
                            nandu.ttArray2[i, j] = 1;
                        }
                    }
                }
                else if (nandu.flag == 3)
                {
                    for (int i = 0; i < 20; i++)
                    {
                        for (int j = 0; j < 45; j++)
                        {
                        nandu.ttArray3[i, j] = 1;
                        }
                    }
                }

                if (nandu.flag == 1){
                    while (mines-- != 0)
                    {
                    A:
                        x = seed.Next(0, 10);
                        y = seed.Next(0, 10);
                        if (nandu.ttArray1[x, y] == 0)
                        {
                            goto A;
                        }
                        else {
                        nandu.ttArray1[x,y] = 0;
                        } 
                    }
                }else if (nandu.flag == 2){
                    while (mines-- != 0)
                    {
                    B:
                        x = seed.Next(0, 25);
                        y = seed.Next(0, 16);
                        if (nandu.ttArray2[x, y] == 0)
                        {
                            goto B;
                        }
                        else
                        {
                        nandu.ttArray2[x, y] = 0;
                        }
                    }
                }else if (nandu.flag == 3) {
                    while (mines-- != 0)
                    {
                    C:
                        x = seed.Next(0, 20);
                        y = seed.Next(0, 45);
                        if (nandu.ttArray3[x, y] == 0)
                        {
                            goto C;
                        }
                        else
                        {
                        nandu.ttArray3[x, y] = 0;
                        }
                    nandu.ttArray3[x, y] = 0;
                    }
                }
                return 0;
            }

        public List<Button> list = new List<Button>();
        public Button buttons = new Button();
        private void Form3_Load()
        {
            int x = 0;
            if (nandu.flag == 1)
            {
                x = 380;
            }
            else if (nandu.flag == 2)
            {
                x = 200;
            } else if (nandu.flag == 3)
            {
                x = 120;
            }
            int y = 60;//起始位置的坐标的y的值
            for (int i = 1; i <= (nandu.flag*10)*(nandu.flag*10); i++)//循环使其动态添加button
            {
                AddButton(x, y, i);
                list.Add(buttons);
                x += 70 - nandu.flag * 15;
                if (nandu.flag == 1)
                {
                    if (i % (nandu.flag * 10) == 0)
                    {
                        y += 70 - nandu.flag * 15;
                        x = 380;
                    }
                }else if (nandu.flag == 2)
                {
                    if (i % (nandu.flag * 12.5) == 0)
                    {
                        y += 70 - nandu.flag * 15;
                        x = 200;
                    }
                }else if (nandu.flag == 3)
                {
                    if (i % ((nandu.flag/ nandu.flag)*45) == 0)
                    {
                        y += 70 - nandu.flag * 15;
                        x = 120;
                    }
                }
            }
        }
        private void AddButton(int x, int y, int i)
        {
            buttons = new Button();
            buttons.BackColor = Color.White;
            buttons.Name = string.Format("{0}", i);
            buttons.Location = new System.Drawing.Point(x, y);
            buttons.Size = new System.Drawing.Size(70-nandu.flag*15, 70 - nandu.flag * 15);
            buttons.TabIndex = i;
            buttons.Text = string.Format("{0}", "");
            this.Controls.Add(buttons);
            buttons.Click += new EventHandler(buttons_Click);
            buttons.MouseDown += new MouseEventHandler(buttons_MouseDown);
        }

        private void buttons_MouseDown(object sender, MouseEventArgs e)
        {
            Button but = (Button)sender;
            int number = Convert.ToInt32(label3.Text);
            if (e.Button == MouseButtons.Right && (but.Text == " " || but.Text == ""))
            {
                but.BackColor = Color.Red;
                but.Text = "雷";
                label3.Text = Convert.ToString(--number);
            }
            else if (e.Button == MouseButtons.Right && but.Text == "雷")
            {
                but.Text = "未知";
                but.BackColor = Color.Purple;
                label3.Text = Convert.ToString(++number);
            }
            else if (e.Button == MouseButtons.Right && but.Text == "未知") {
                but.Text = " ";
                but.BackColor = Color.White;
            }
        }

        void buttons_Click(object sender, EventArgs e)
        {
            MouseEventArgs Mouse_e = (MouseEventArgs)e;
            Button but = (Button)sender;
            if (Mouse_e.Button == MouseButtons.Left)
            {
                int number =Convert.ToInt32(but.Name);
                int num_x = 0;
                int num_y = 0;
                if (nandu.flag == 1)
                {
                    if (number == 10) {
                        num_x = 0;
                    }
                    else if(number %10 == 0){
                        num_x = (number / 10) -1;
                    }
                    else { 
                        num_x = (number / 10);
                    }
                    if (num_x == 0)
                    {
                        num_y = number - 1;
                    }
                    else if (number % 10 == 0) {
                        num_y = 9;
                    }
                    else
                    {
                        num_y = (number - (10 * num_x)) % 10 - 1;
                    }
                }
                else if (nandu.flag == 2)
                {
                    if (number == 25)
                    {
                        num_x = 0;
                    }
                    else if (number % 25 == 0)
                    {
                        num_x = (number / 25) - 1;
                    }
                    else { 
                        num_x = number / 25;
                    }
                    if (num_x == 0)
                    {
                        num_y = number -1;
                    }
                    else if (number % 16 == 0)
                    {
                        num_y = 15;
                    }
                    else
                    {
                        num_y = (number - (25 * num_x)) - 1;
                    }
                }
                else if (nandu.flag == 3)
                {
                    if (number == 45)
                    {
                        num_x = 0;
                    }
                    else if (number % 45 == 0)
                    {
                        num_x = (number / 45) - 1;
                    }
                    else
                    {
                        num_x = number / 45;
                    }
                    if (num_x == 0)
                    {
                        num_y = number -1;
                    }
                    else if (number % 45 == 0)
                    {
                        num_y = 44;
                    }
                    else
                    {
                        num_y = (number - (45 * num_x)) - 1;
                    }
                }
                if (nandu.flag == 1)
                {
                    if (nandu.ttArray1[num_x, num_y] == 0)
                    {
                        MessageBox.Show("游戏结束!", "提示");
                    }
                    else
                    {
                        int count = 0;
                        if (num_x - 1 >= 0 && num_x - 1 < 10 && num_y - 1 >= 0 && num_y - 1 < 10 && nandu.ttArray1[num_x - 1, num_y - 1] == 0)
                        {
                            count++;
                        }
                        if (num_x - 1 >= 0 && num_x - 1 < 10 && num_y >= 0 && num_y < 10 && nandu.ttArray1[num_x - 1, num_y] == 0)
                        {
                            count++;
                        }
                        if (num_x - 1 >= 0 && num_x - 1 < 10 && num_y + 1 >= 0 && num_y + 1 < 10 && nandu.ttArray1[num_x - 1, num_y + 1] == 0)
                        {
                            count++;
                        }
                        if (num_x >= 0 && num_x < 10 && num_y - 1 >= 0 && num_y - 1 < 10 && nandu.ttArray1[num_x, num_y - 1] == 0)
                        {
                            count++;
                        }
                        if (num_x >= 0 && num_x < 10 && num_y + 1 >= 0 && num_y + 1 < 10 && nandu.ttArray1[num_x, num_y + 1] == 0)
                        {
                            count++;
                        }
                        if (num_x + 1 >= 0 && num_x + 1 < 10 && num_y - 1 >= 0 && num_y - 1 < 10 && nandu.ttArray1[num_x + 1, num_y - 1] == 0)
                        {
                            count++;
                        }
                        if (num_x + 1 >= 0 && num_x + 1 < 10 && num_y >= 0 && num_y < 10 && nandu.ttArray1[num_x + 1, num_y] == 0)
                        {
                            count++;
                        }
                        if (num_x + 1 >= 0 && num_x + 1 < 10 && num_y + 1 >= 0 && num_y + 1 < 10 && nandu.ttArray1[num_x + 1, num_y + 1] == 0)
                        {
                            count++;
                        }
                        if (count == 0) {
                            but.BackColor = Color.Yellow;
                            but.Text = " ";
                        }
                        else {
                            but.BackColor = Color.Yellow;
                            but.Text = Convert.ToString(count);
                        }
                    }
                }
                else if (nandu.flag == 2)
                {
                    if (nandu.ttArray2[num_x, num_y] == 0)
                    {
                        MessageBox.Show("游戏结束!", "提示");
                    }
                    else
                    {
                        int count = 0;
                        if (num_x - 1 >= 0 && num_x - 1 < 25 && num_y - 1 >= 0 && num_y - 1 < 16 && nandu.ttArray2[num_x - 1, num_y - 1] == 0)
                        {
                            count++;
                        }
                        if (num_x - 1 >= 0 && num_x - 1 < 25 && num_y >= 0 && num_y < 16 && nandu.ttArray2[num_x - 1, num_y] == 0)
                        {
                            count++;
                        }
                        else if (num_x - 1 >= 0 && num_x - 1 < 25 && num_y + 1 >= 0 && num_y + 1 < 16 && nandu.ttArray2[num_x - 1, num_y + 1] == 0)
                        {
                            count++;
                        }
                        if (num_x >= 0 && num_x < 25 && num_y - 1 >= 0 && num_y - 1 < 16 && nandu.ttArray2[num_x, num_y - 1] == 0)
                        {
                            count++;
                        }
                        if (num_x >= 0 && num_x < 25 && num_y + 1 >= 0 && num_y + 1 < 16 && nandu.ttArray2[num_x, num_y + 1] == 0)
                        {
                            count++;
                        }
                        if (num_x + 1 >= 0 && num_x + 1 < 25 && num_y - 1 >= 0 && num_y - 1 <16 && nandu.ttArray2[num_x + 1, num_y - 1] == 0)
                        {
                            count++;
                        }
                        if (num_x + 1 >= 0 && num_x + 1 < 25 && num_y >= 0 && num_y < 16 && nandu.ttArray2[num_x + 1, num_y] == 0)
                        {
                            count++;
                        }
                        if (num_x + 1 >= 0 && num_x + 1 < 25 && num_y + 1 >= 0 && num_y + 1 < 16 && nandu.ttArray2[num_x + 1, num_y + 1] == 0)
                        {
                            count++;
                        }
                        if (count == 0)
                        {
                            but.BackColor = Color.Yellow;
                            but.Text = " ";
                        }
                        else
                        {
                            but.BackColor = Color.Yellow;
                            but.Text = Convert.ToString(count);
                        }
                    }
                }
                else if (nandu.flag == 3) {
                    if (nandu.ttArray3[num_x, num_y] == 0)
                    {
                        MessageBox.Show("游戏结束!", "提示");
                    }
                    else
                    {
                        int count = 0;
                        if (num_x - 1 >= 0 && num_x - 1 <20 && num_y - 1 >= 0 && num_y - 1 <45 && nandu.ttArray3[num_x - 1, num_y - 1] == 0)
                        {
                            count++;
                        }
                        if (num_x - 1 >= 0 && num_x - 1 <20 && num_y >= 0 && num_y <45 && nandu.ttArray3[num_x - 1, num_y] == 0)
                        {
                            count++;
                        }
                        if (num_x - 1 >= 0 && num_x - 1 <20 && num_y + 1 >= 0 && num_y + 1 <45 && nandu.ttArray3[num_x - 1, num_y + 1] == 0)
                        {
                            count++;
                        }
                        if (num_x >= 0 && num_x <20 && num_y - 1 >= 0 && num_y - 1 <45 && nandu.ttArray3[num_x, num_y - 1] == 0)
                        {
                            count++;
                        }
                        if (num_x >= 0 && num_x <20 && num_y + 1 >= 0 && num_y + 1 <45 && nandu.ttArray3[num_x, num_y + 1] == 0)
                        {
                            count++;
                        }
                        if (num_x + 1 >= 0 && num_x + 1 <20 && num_y - 1 >= 0 && num_y - 1 <45&& nandu.ttArray3[num_x + 1, num_y - 1] == 0)
                        {
                            count++;
                        }
                        if (num_x + 1 >= 0 && num_x + 1 <20 && num_y >= 0 && num_y <45 && nandu.ttArray3[num_x + 1, num_y] == 0)
                        {
                            count++;
                        }
                        if (num_x + 1 >= 0 && num_x + 1 <20 && num_y + 1 >= 0 && num_y + 1 <45 && nandu.ttArray3[num_x + 1, num_y + 1] == 0)
                        {
                            count++;
                        }
                        if (count == 0)
                        {
                            but.BackColor = Color.Yellow;
                            but.Text = " ";
                        }
                        else
                        {
                            but.BackColor = Color.Yellow;
                            but.Text = Convert.ToString(count);
                        }
                    }
                }
            }    
        }

    }

}

以上代码都是经过详细的数学计算得出的。应为我们都知道,当一个按钮的四周9个格子,只要有一个存在雷,那么当前格子就会出现一个0加上雷数的数字之和。
而我们依旧采用了flag值代表雷数的方法。
每次游戏开始时,系统自动根据难度分配雷数。整体界面使用二维数组实现。当然,若雷数和普通方格的雷数差距过大,我们可以使用稀疏矩阵来存储这些数组以实现程序优化。

  • 13
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序小黑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值