Пример #1
0
        /// <summary>
        /// 右击事件排雷
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnRight_Click(object sender, MouseEventArgs e)
        {
            BoomButton btn = (BoomButton)sender;

            //右击鼠标
            if (e.Button == MouseButtons.Right)
            {
                //如果已经翻开过,则返回
                if (btn.status == BtnStatus.open)
                {
                    return;
                }

                //未翻开
                if (btn.status == BtnStatus.close)
                {
                    btn.BackgroundImage       = imageList1.Images[0];
                    btn.BackgroundImageLayout = ImageLayout.Stretch;//在按钮中拉伸

                    if (btn.hasBoom == true)
                    {
                        sweepBoomCount++;
                    }

                    btn.status = BtnStatus.mark;
                }
                else  //标记扫雷
                {
                    //若已经标记的,则变回无标记
                    Image image = null;
                    btn.BackgroundImage = image;

                    if (btn.hasBoom == true)
                    {
                        sweepBoomCount--;
                    }

                    btn.status = BtnStatus.close;
                }

                checkGame();
            }
        }
Пример #2
0
        /// <summary>
        /// form1的构造函数
        /// </summary>
        public Form1(int setBoomSize, int setBoomCount)
        {
            //初始化窗口
            InitializeComponent();

            this.BoomCount = setBoomCount;
            this.BoomSize  = setBoomSize;

            //新建一个进程
            thread = new Thread(new ThreadStart(newForm));


            //用于产生雷阵
            boomButton    = new BoomButton[setBoomSize, setBoomSize];
            buttonHasBoom = new bool[setBoomSize, setBoomSize];

            int i, j, boomCount = 0;

            startTime = DateTime.Now;

            Random random = new Random();

            for (i = 0; i < setBoomSize; ++i)
            {
                for (j = 0; j < setBoomSize; ++j)
                {
                    buttonHasBoom[i, j] = false;
                }
            }
            while (boomCount < setBoomCount)
            {
                i = random.Next(setBoomSize);
                j = random.Next(setBoomSize);
                if (buttonHasBoom[i, j] == false)
                {
                    buttonHasBoom[i, j] = true;
                    boomCount++;
                }
            }

            // 用于初始化所有雷
            for (i = 0; i < setBoomSize; ++i)
            {
                for (j = 0; j < setBoomSize; ++j)
                {
                    boomButton[i, j]         = new BoomButton(i, j);
                    boomButton[i, j].hasBoom = buttonHasBoom[i, j];

                    //确定好每个按钮的大小,使其大小各相同
                    boomButton[i, j].Width  = 30;
                    boomButton[i, j].Height = 30;

                    //设置button里面的背景图片,默认为空
                    boomButton[i, j].FlatStyle = FlatStyle.Standard;
                    boomButton[i, j].BackColor = SystemColors.Control;
                    Image image = null;
                    boomButton[i, j].BackgroundImage = image;

                    //确定button里面字体的大小
                    float fontSize = 9f;
                    boomButton[i, j].Font = new Font(boomButton[i, j].Font.Name, fontSize,
                                                     boomButton[i, j].Font.Style, boomButton[i, j].Font.Unit);

                    //确定雷的分布位置
                    boomButton[i, j].Left = i * boomButton[i, j].Width;
                    boomButton[i, j].Top  = j * boomButton[i, j].Height;

                    //确定扫雷的左右击事件
                    boomButton[i, j].Click       += new EventHandler(BtnLeft_Click);
                    boomButton[i, j].MouseUp     += new MouseEventHandler(BtnRight_Click);
                    boomButton[i, j].DoubleClick += new EventHandler(BtnLeft_OnMouseDoubleClick);

                    //将button添加到panel上
                    panel1.Controls.Add(boomButton[i, j]);
                }
            }

            //计算周边雷的数目
            for (i = 0; i < setBoomSize; i++)
            {
                for (j = 0; j < setBoomSize; j++)
                {
                    if (buttonHasBoom[i, j] == true)
                    {
                        if (i - 1 >= 0 && j - 1 >= 0)
                        {
                            boomButton[i - 1, j - 1].countAround++;
                        }
                        if (j - 1 >= 0)
                        {
                            boomButton[i, j - 1].countAround++;
                        }
                        if (i + 1 <= setBoomSize - 1 & j - 1 >= 0)
                        {
                            boomButton[i + 1, j - 1].countAround++;
                        }
                        if (i - 1 >= 0)
                        {
                            boomButton[i - 1, j].countAround++;
                        }
                        if (i + 1 <= setBoomSize - 1)
                        {
                            boomButton[i + 1, j].countAround++;
                        }
                        if (i - 1 >= 0 && j + 1 <= setBoomSize - 1)
                        {
                            boomButton[i - 1, j + 1].countAround++;
                        }
                        if (j + 1 <= setBoomSize - 1)
                        {
                            boomButton[i, j + 1].countAround++;
                        }
                        if (i + 1 <= setBoomSize - 1 && j + 1 <= setBoomSize - 1)
                        {
                            boomButton[i + 1, j + 1].countAround++;
                        }
                    }
                }
            }
        }
Пример #3
0
        /// <summary>
        /// 左击双击事件自动排雷
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnLeft_OnMouseDoubleClick(object sender, EventArgs e)
        {
            BoomButton btn = (BoomButton)sender;
            int        i = btn.x, j = btn.y;
            int        m, n;
            int        count = 0;

            if (btn.status == BtnStatus.open)
            {
                if (i - 1 >= 0 && j - 1 >= 0)
                {
                    if (boomButton[i - 1, j - 1].status == BtnStatus.mark)
                    {
                        count++;
                    }
                }
                if (j - 1 >= 0)
                {
                    if (boomButton[i, j - 1].status == BtnStatus.mark)
                    {
                        count++;
                    }
                }
                if (i + 1 <= BoomSize - 1 & j - 1 >= 0)
                {
                    if (boomButton[i + 1, j - 1].status == BtnStatus.mark)
                    {
                        count++;
                    }
                }
                if (i - 1 >= 0)
                {
                    if (boomButton[i - 1, j].status == BtnStatus.mark)
                    {
                        count++;
                    }
                }
                if (i + 1 <= BoomSize - 1)
                {
                    if (boomButton[i + 1, j].status == BtnStatus.mark)
                    {
                        count++;
                    }
                }
                if (i - 1 >= 0 && j + 1 <= BoomSize - 1)
                {
                    if (boomButton[i - 1, j + 1].status == BtnStatus.mark)
                    {
                        count++;
                    }
                }
                if (j + 1 <= BoomSize - 1)
                {
                    if (boomButton[i, j + 1].status == BtnStatus.mark)
                    {
                        count++;
                    }
                }
                if (i + 1 <= BoomSize - 1 && j + 1 <= BoomSize - 1)
                {
                    if (boomButton[i + 1, j + 1].status == BtnStatus.mark)
                    {
                        count++;
                    }
                }
                if (count == btn.countAround)
                {
                    if (i + 1 == BoomSize)
                    {
                        for (m = i - 1; m < i + 1; ++m)
                        {
                            for (n = j - 1; n <= j + 1; ++n)
                            {
                                if (boomButton[m, n].status == BtnStatus.close)
                                {
                                    if (boomButton[m, n].hasBoom == true)
                                    {
                                        boomButton[m, n].BackgroundImage       = imageList1.Images[2];
                                        boomButton[m, n].BackgroundImageLayout = ImageLayout.Stretch;//在按钮中拉伸
                                        //使得计数显示停止
                                        isFinish = 2;
                                        if (DialogResult.Yes == MessageBox.Show("You lose!\n是否开始新的一局游戏?", "温馨提示:", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk))
                                        {
                                            Close();
                                            thread.Start();
                                        }
                                        else
                                        {
                                            //不做任何事
                                        }
                                    }
                                    boomButton[m, n].status    = BtnStatus.open;
                                    boomButton[m, n].FlatStyle = FlatStyle.Flat;
                                    if (boomButton[m, n].countAround != 0)
                                    {
                                        boomButton[m, n].Text = boomButton[m, n].countAround.ToString();
                                    }
                                }
                                else
                                {
                                    continue;
                                }
                            }
                        }
                    }
                    else if (i == 0)
                    {
                        for (m = i; m <= i + 1; ++m)
                        {
                            for (n = j - 1; n <= j + 1; ++n)
                            {
                                if (boomButton[m, n].status == BtnStatus.close)
                                {
                                    if (boomButton[m, n].hasBoom == true)
                                    {
                                        boomButton[m, n].BackgroundImage       = imageList1.Images[2];
                                        boomButton[m, n].BackgroundImageLayout = ImageLayout.Stretch;//在按钮中拉伸
                                        MessageBox.Show("lose");
                                        //使得计数显示停止
                                        isFinish = 2;
                                    }
                                    boomButton[m, n].status    = BtnStatus.open;
                                    boomButton[m, n].FlatStyle = FlatStyle.Flat;
                                    if (boomButton[m, n].countAround != 0)
                                    {
                                        boomButton[m, n].Text = boomButton[m, n].countAround.ToString();
                                    }
                                }
                                else
                                {
                                    continue;
                                }
                            }
                        }
                    }
                    else if (j == 0)
                    {
                        for (m = i - 1; m <= i + 1; ++m)
                        {
                            for (n = j; n <= j + 1; ++n)
                            {
                                if (boomButton[m, n].status == BtnStatus.close)
                                {
                                    if (boomButton[m, n].hasBoom == true)
                                    {
                                        boomButton[m, n].BackgroundImage       = imageList1.Images[2];
                                        boomButton[m, n].BackgroundImageLayout = ImageLayout.Stretch;//在按钮中拉伸
                                        //使得计数显示停止
                                        isFinish = 2;
                                        if (DialogResult.Yes == MessageBox.Show("You lose!\n是否开始新的一局游戏?", "温馨提示:", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk))
                                        {
                                            Close();
                                            thread.Start();
                                        }
                                        else
                                        {
                                            //不做任何事
                                        }
                                    }
                                    boomButton[m, n].status    = BtnStatus.open;
                                    boomButton[m, n].FlatStyle = FlatStyle.Flat;
                                    if (boomButton[m, n].countAround != 0)
                                    {
                                        boomButton[m, n].Text = boomButton[m, n].countAround.ToString();
                                    }
                                }
                                else
                                {
                                    continue;
                                }
                            }
                        }
                    }
                    else if (j + 1 == BoomSize)
                    {
                        for (m = i - 1; m <= i + 1; ++m)
                        {
                            for (n = j - 1; n < j + 1; ++n)
                            {
                                if (boomButton[m, n].status == BtnStatus.close)
                                {
                                    if (boomButton[m, n].hasBoom == true)
                                    {
                                        boomButton[m, n].BackgroundImage       = imageList1.Images[2];
                                        boomButton[m, n].BackgroundImageLayout = ImageLayout.Stretch;//在按钮中拉伸
                                        //使得计数显示停止
                                        isFinish = 2;
                                        if (DialogResult.Yes == MessageBox.Show("You lose!\n是否开始新的一局游戏?", "温馨提示:", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk))
                                        {
                                            Close();
                                            thread.Start();
                                        }
                                        else
                                        {
                                            //不做任何事
                                        }
                                    }
                                    boomButton[m, n].status    = BtnStatus.open;
                                    boomButton[m, n].FlatStyle = FlatStyle.Flat;
                                    if (boomButton[m, n].countAround != 0)
                                    {
                                        boomButton[m, n].Text = boomButton[m, n].countAround.ToString();
                                    }
                                }
                                else
                                {
                                    continue;
                                }
                            }
                        }
                    }
                    else
                    {
                        for (m = i - 1; m <= i + 1; ++m)
                        {
                            for (n = j - 1; n <= j + 1; ++n)
                            {
                                if (boomButton[m, n].status == BtnStatus.close)
                                {
                                    if (boomButton[m, n].hasBoom == true)
                                    {
                                        boomButton[m, n].BackgroundImage       = imageList1.Images[2];
                                        boomButton[m, n].BackgroundImageLayout = ImageLayout.Stretch;//在按钮中拉伸
                                        //使得计数显示停止
                                        isFinish = 2;
                                        if (DialogResult.Yes == MessageBox.Show("You lose!\n是否开始新的一局游戏?", "温馨提示:", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk))
                                        {
                                            Close();
                                            thread.Start();
                                        }
                                        else
                                        {
                                            //不做任何事
                                        }
                                    }
                                    boomButton[m, n].status    = BtnStatus.open;
                                    boomButton[m, n].FlatStyle = FlatStyle.Flat;
                                    if (boomButton[m, n].countAround != 0)
                                    {
                                        boomButton[m, n].Text = boomButton[m, n].countAround.ToString();
                                    }
                                }
                                else
                                {
                                    continue;
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                return;
            }
        }
Пример #4
0
        /// <summary>
        /// 左击事件排雷
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnLeft_Click(object sender, EventArgs e)
        {
            BoomButton btn = (BoomButton)sender;

            //如果不是关闭的,return
            if (btn.status != BtnStatus.close)
            {
                return;
            }

            btn.status    = BtnStatus.open;
            btn.FlatStyle = FlatStyle.Flat;

            if (btn.hasBoom == false)
            {
                //当前的周围没有雷,就进行递归的排雷
                if (btn.countAround == 0)
                {
                    int i = btn.x, j = btn.y;
                    if (i - 1 >= 0 && j - 1 >= 0)
                    {
                        BtnLeft_Click(boomButton[i - 1, j - 1], e);
                    }
                    if (j - 1 >= 0)
                    {
                        BtnLeft_Click(boomButton[i, j - 1], e);
                    }
                    if (i + 1 <= BoomSize - 1 & j - 1 >= 0)
                    {
                        BtnLeft_Click(boomButton[i + 1, j - 1], e);
                    }
                    if (i - 1 >= 0)
                    {
                        BtnLeft_Click(boomButton[i - 1, j], e);
                    }
                    if (i + 1 <= BoomSize - 1)
                    {
                        BtnLeft_Click(boomButton[i + 1, j], e);
                    }
                    if (i - 1 >= 0 && j + 1 <= BoomSize - 1)
                    {
                        BtnLeft_Click(boomButton[i - 1, j + 1], e);
                    }
                    if (j + 1 <= BoomSize - 1)
                    {
                        BtnLeft_Click(boomButton[i, j + 1], e);
                    }
                    if (i + 1 <= BoomSize - 1 && j + 1 <= BoomSize - 1)
                    {
                        BtnLeft_Click(boomButton[i + 1, j + 1], e);
                    }
                }
                else
                {
                    btn.Text = btn.countAround.ToString();
                }
            }
            else if (btn.hasBoom == true)
            {
                btn.BackgroundImage       = imageList1.Images[1];
                btn.BackgroundImageLayout = ImageLayout.Stretch;//在按钮中拉伸
                //使得计数显示停止
                isFinish = 2;
                if (DialogResult.Yes == MessageBox.Show("You lose!\n是否开始新的一局游戏?", "温馨提示:", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk))
                {
                    Close();
                    thread.Start();
                }
                else
                {
                    //不做任何事
                }
            }

            checkGame();
        }