Пример #1
0
        private void Flag(object sender, MouseEventArgs e)
        {
            ButtonExtended button = (ButtonExtended)sender;

            if (e.Button == MouseButtons.Right && button.Text == "" && status == 0)
            {
                if (!timer.Enabled)
                {
                    timer.Start();
                }
                if (button.isFlag == false)
                {
                    button.isFlag    = true;
                    button.BackColor = flagColor;
                    remainsBombs_Visual--;
                    RemainsToolStripMenuItem_Click(0, null);
                }
                else
                {
                    button.isFlag    = false;
                    button.BackColor = Color.FromArgb(204, 204, 204);
                    remainsBombs_Visual++;
                    RemainsToolStripMenuItem_Click(0, null);
                }
            }
            CheckWin();
        }
Пример #2
0
        private void Explode(ButtonExtended b)
        {
            if (timer.Enabled)
            {
                timer.Stop();
            }
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    if (allButtons[x, y].isBomb)
                    {
                        allButtons[x, y].Text = "*";
                    }
                }
            }
            DialogRestartOrContinue form = new DialogRestartOrContinue($"You lose :-( (\n\rYour time is {Time}");

            form.ShowDialog();
            if (form.DialogResult() == '1')
            {
                RestartToolStripMenuItem_Click(0, null);
            }
            else if (form.DialogResult() == '0')
            {
                status = 1;
            }
            else
            {
                Close();
            }
            //MessageBox.Show($"Вы проиграли (\n\rВаше время - {Time}");
            //status = 1;
        }
Пример #3
0
        private void FieldClick(object sender, EventArgs e)
        {
            ButtonExtended but = (ButtonExtended)sender;

            if (but.Text == "" && but.isFlag == false && status == 0)
            {
                if (!timer.Enabled)
                {
                    timer.Start();
                }
                if (but.isBomb)
                {
                    Explode(but);
                }
                else
                {
                    EmptyFieldClick(but);
                }
            }
            if (but.Text != "" && !but.isFlag && status == 0)
            {
                OpenNeighbors(but);
            }
            CheckWin();
        }
Пример #4
0
        private void OpenNeighbors(ButtonExtended button)
        {
            int xB    = (button.Location.X - 12) / distance;
            int yB    = (button.Location.Y - 38) / distance;
            int bombs = 0;

            if (button.Text != "*")
            {
                bombs = Convert.ToInt32(button.Text);
            }
            else
            {
                bombs++;
            }
            int flags = 0;

            for (int x = xB - 1; x <= xB + 1; x++)
            {
                for (int y = yB - 1; y <= yB + 1; y++)
                {
                    if (x >= 0 && x < width && y >= 0 && y < height && allButtons[x, y].isClearAround == true && allButtons[x, y].isFlag && (x != xB || y != yB))
                    {
                        flags++;//считаем отметки вокруг
                    }
                }
            }
            if (bombs == flags)//если они совпадают , то открываем все клетки вокруг
            {
                for (int x = xB - 1; x <= xB + 1; x++)
                {
                    for (int y = yB - 1; y <= yB + 1; y++)
                    {
                        if (x >= 0 && x < width && y >= 0 && y < height && allButtons[x, y].isClearAround == true && (x != xB || y != yB) && allButtons[x, y].Text == "" && !allButtons[x, y].isFlag)
                        {
                            if (allButtons[x, y].isBomb)//если одна из клеток оказалась бомбой , то отметки были отмечены неправильно => проигрыш
                            {
                                Explode(allButtons[x, y]);
                            }
                            else//если же все клетки оказались пустыми , метки поставлены верно
                            {
                                EmptyFieldClick(allButtons[x, y]);//изменить клетку на открытую
                            }
                        }
                    }
                }
            }
        }
Пример #5
0
 private void EmptyFieldClick(ButtonExtended b)
 {
     for (int x = 0; x < width; x++)
     {
         for (int y = 0; y < height; y++)
         {
             if (allButtons[x, y] == b)
             {
                 b.Text = Convert.ToString(CountBombAround(x, y));
                 if (Convert.ToInt32(b.Text) == 0)
                 {
                     b.Text          = "";
                     b.isClearAround = false;
                     FindSpace(x, y);
                 }
                 b.BackColor = Color.FromArgb(255, 255, 255);
             }
         }
     }
 }
Пример #6
0
        private void Create()
        {
            min = 0;
            h   = 0;
            if (timer.Enabled)
            {
                timer.Stop();
            }
            timerCounter        = 0;
            Clock.Text          = "Timer [ 0:0 ]";
            remainsBombs_Visual = 0;
            status     = 0;
            CountBombs = 0;
            allButtons = new ButtonExtended[width, height];
            Random rand = new Random();

            for (int x = 12; (x - 12) < width * distance; x += distance)
            {
                for (int y = 38; (y - 38) < height * distance; y += distance)
                {
                    ButtonExtended button = new ButtonExtended();
                    defColor         = button.BackColor;
                    button.Font      = new Font("Arial", 15, FontStyle.Bold);
                    button.Location  = new Point(x, y);
                    button.Size      = new Size(30, 30);
                    button.BackColor = Color.FromArgb(204, 204, 204);
                    if (rand.Next(0, 101) < 20)
                    {
                        button.isBomb = true;
                        remainsBombs_Visual++;
                        CountBombs++;
                    }
                    allButtons[(x - 12) / distance, (y - 38) / distance] = button;
                    Controls.Add(button);
                    button.Click   += new EventHandler(FieldClick);
                    button.MouseUp += new MouseEventHandler(Flag);
                }
            }
            RemainsToolStripMenuItem_Click(0, null);
            this.Size = new Size(35 * (width + 1), 35 * (height + 3) - 23);
        }