Exemplo n.º 1
0
 protected void setFlag(FieldButton bt)
 {
     //поставить или снять флажок
     if (bt.isClicable)
     {
         if (FieldButton.bombCount > 0)
         {
             bt.Text       = "▲";
             bt.isClicable = false;
             bt.isFlag     = true;
             FieldButton.bombCount--;
             FieldCount--;
             tb.Text = FieldButton.bombCount.ToString();
         }
     }
     else
     {
         bt.Text       = "";
         bt.isClicable = true;
         bt.isFlag     = false;
         FieldButton.bombCount++;
         FieldCount++;
         tb.Text = FieldButton.bombCount.ToString();
     }
 }
Exemplo n.º 2
0
        public void GenerateField()       //Генерация кнопок по полю
        {
            Random random = new Random(); //для бомб

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    FieldButton newButton = new FieldButton();
                    newButton.Location    = new Point(x * offset, (y + 1) * offset); //позиция кнопки
                    newButton.Size        = new Size(offset, offset);                //размер кнопки
                    newButton.isClickable = true;                                    //установка кликабельности
                    newButton.TabStop     = false;                                   //отключение перевода фокуса
                    if (random.Next(0, 100) <= bombPercent)                          //расставление бомб
                    {
                        newButton.isBomb = true;                                     //установка бомбы на клетку
                        bombs++;                                                     //количество бомб
                    }
                    newButton.xCoord = x;
                    newButton.yCoord = y;
                    gameForm.Controls.Add(newButton);                             //добавление на поле
                    newButton.MouseUp += new MouseEventHandler(FieldButtonClick); //приязываем клик к мышке
                    field[x, y]        = newButton;                               //добавление в массив кнопку
                }
            }
        }
Exemplo n.º 3
0
        //открываем все нули и все соседние клетки нулей
        void OpenRegion(int xCoord, int yCoord, FieldButton clickedButton) //для открытия соседних клеток
        {
            Queue <FieldButton> queue = new Queue <FieldButton>();

            queue.Enqueue(clickedButton);//добавляем нажатый элемент
            while (queue.Count > 0)
            {
                FieldButton currentCell = queue.Dequeue();                     //получаем первый элемент в очереди
                OpenCell(currentCell.xCoord, currentCell.yCoord, currentCell); //открытие самой ячейки
                cellsOpened++;
                if (CountBombsAround(currentCell.xCoord, currentCell.yCoord) == 0)
                {
                    for (int y = currentCell.yCoord - 1; y <= currentCell.yCoord + 1; y++)
                    {
                        for (int x = currentCell.xCoord - 1; x <= currentCell.xCoord + 1; x++)
                        {
                            if (x >= 0 && x < width && y >= 0 && y < height)
                            {
                                //если уже не была добавлена, то добавляем в очередь
                                if (!field[x, y].wasAdded)
                                {
                                    queue.Enqueue(field[x, y]);
                                    field[x, y].wasAdded = true;
                                }
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 4
0
        protected void openRegion(FieldButton bt)
        {
            //Открывает все 0 ячейки в округе, если нажата ячейка с 0
            var str = bt.Name.Split(' ');
            var x1  = Convert.ToInt32(str[0]) - bWidth;
            var x2  = x1 + bWidth * 3;
            var y1  = Convert.ToInt32(str[1]) - bHight;
            var y2  = y1 + bHight * 3;

            var buttons = new List <FieldButton>();

            for (int i = x1; i < x2; i += bWidth)
            {
                for (int j = y1; j < y2; j += bHight)
                {
                    FieldButton tempBt = fb.Find(button => button.Name == $"{i} {j}");
                    if (tempBt != null && tempBt.isClicable && !tempBt.Bomb())
                    {
                        int b = bombAround(tempBt);
                        openCell(tempBt, b);
                        if (b == 0)
                        {
                            buttons.Add(tempBt);
                        }
                    }
                }
            }

            foreach (FieldButton button in buttons)
            {
                openRegion(button);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Metoda, która wlaśnie tworzy z pomocą 2ch pętl przyciski a uzupewnia wirtualnie
        /// naszą 2-wymirawo masyw przyciskami
        /// </summary>
        public void GenerateField()
        {
            Random random = new Random();

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    FieldButton newButton = new FieldButton();
                    newButton.Location    = new Point(x * offset, y * offset);
                    newButton.Size        = new Size(offset, offset);
                    newButton.isClickable = true;
                    if (random.Next(0, 100) <= bombPercent)
                    {
                        newButton.isBomb = true;
                        bombs++;
                    }
                    newButton.xCoord = x;
                    newButton.yCoord = y;
                    Controls.Add(newButton);
                    newButton.MouseUp += new MouseEventHandler(FieldButtonClick);
                    field[x, y]        = newButton;
                }
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Metoda, która otwira danej przyczisknientej klatki u sprawdza ile bomb dowkola
        /// także sprawdza ktore klatki można otwirać "wodospadem" wszystkich pustych klatek bez
        /// sąsiędnich bomb z pomocą "kolejki"
        /// </summary>
        /// <param name="xCoord">integer X koordynaty danego przyciska</param>
        /// <param name="yCoord">integer Y koordynaty danego przyciska</param>
        /// <param name="clickedButton">Object, czyli sam przycisk</param>
        void OpenRegion(int xCoord, int yCoord, FieldButton clickedButton)
        {
            Queue <FieldButton> queue = new Queue <FieldButton>();

            queue.Enqueue(clickedButton);
            clickedButton.wasAdded = true;
            while (queue.Count > 0)
            {
                FieldButton currentCell = queue.Dequeue();
                OpenCell(currentCell.xCoord, currentCell.yCoord, currentCell);
                cellsOpened++;
                if (CountBombsAround(currentCell.xCoord, currentCell.yCoord) == 0)
                {
                    for (int y = currentCell.yCoord - 1; y <= currentCell.yCoord + 1; y++)
                    {
                        for (int x = currentCell.xCoord - 1; x <= currentCell.xCoord + 1; x++)
                        {
                            if (x == currentCell.xCoord && y == currentCell.yCoord)
                            {
                                continue;
                            }
                            if (x >= 0 && x < width && y >= 0 && y < height)
                            {
                                if (!field[x, y].wasAdded)
                                {
                                    queue.Enqueue(field[x, y]);
                                    field[x, y].wasAdded = true;
                                }
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 7
0
        protected void createField()
        {
            for (int i = 0; i < 12; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    x = 15 + bWidth * j;
                    y = 45 + bHight * i;
                    FieldButton bt = new FieldButton();
                    bt.Size     = new Size(bWidth, bHight);
                    bt.Location = new Point(x, y);
                    bt.Name     = $"{x} {y}";
                    bt.Text     = "";
                    tb.TabIndex = 1;

                    fb.Add(bt);
                    Controls.Add(bt);

                    bt.MouseUp  += new Core().Bt_Click;
                    bt.GotFocus += new Core().Bt_GotFocus;
                }
            }

            FieldCount = fb.Count();

            tb.Size     = new Size(bWidth, bHight);
            tb.Location = new Point(15, 15);
            tb.Text     = setBombs().ToString();
            tb.ReadOnly = true;
            tb.Enabled  = false;
            tb.TabIndex = 0;
            Controls.Add(tb);
        }
Exemplo n.º 8
0
 protected void openCell(FieldButton bt, int b)
 {
     //открыть ячейку
     bt.FlatStyle = FlatStyle.Popup;
     bt.Text      = b.ToString();
     FieldCount--;
     bt.Enabled    = false;
     bt.isClicable = false;
 }
Exemplo n.º 9
0
 void EmptyFieldButtonClick(FieldButton clickedButton) //если не бомба
 {
     for (int y = 0; y < height; y++)
     {
         for (int x = 0; x < width; x++)
         {
             if (field[x, y] == clickedButton)
             {
                 OpenRegion(x, y, clickedButton);
             }
         }
     }
 }
Exemplo n.º 10
0
        /// <summary>
        /// otwieranie danej klatki i zapisywanie ilości bomb w sąsiadnich klatkach
        /// </summary>
        /// <param name="x">X coordynata klatki</param>
        /// <param name="y">Y coordynata klatki</param>
        /// <param name="clickedButton">object, czyli sama klatka/przycisk </param>
        void OpenCell(int x, int y, FieldButton clickedButton)
        {
            int bombsAround = CountBombsAround(x, y);

            if (bombsAround == 0)
            {
            }
            else
            {
                clickedButton.Text = "" + bombsAround;
            }
            clickedButton.Enabled = false;
        }
Exemplo n.º 11
0
        void FieldButtonClick(object sender, MouseEventArgs e) //при клике
        {
            if (!isStartGame)                                  //начать игру
            {
                isStartGame = true;
                Task.Run(() => Timer()); //поток запускает метод Таймера
            }

            if (!game_end)
            {
                FieldButton clickedButton = (FieldButton)sender;                //преобразуем object в button
                if (e.Button == MouseButtons.Left && clickedButton.isClickable) // отклик на левую кнопку и если кликабельный
                {
                    if (clickedButton.isBomb)
                    {
                        // чтобы не было проигрыша при первом клике
                        if (isFirstClick)//если первый клик
                        {
                            //убираем с этой клетки бомбу
                            clickedButton.isBomb = false;
                            isFirstClick         = false;
                            bombs--;
                            OpenRegion(clickedButton.xCoord, clickedButton.yCoord, clickedButton);
                        }
                        else
                        {
                            Explode();//взрыв
                        }
                    }
                    else
                    {
                        EmptyFieldButtonClick(clickedButton);
                    }
                    isFirstClick = false;
                }
                if (e.Button == MouseButtons.Right) //если правой мышкой, то ставим "В"
                {
                    clickedButton.isClickable = !clickedButton.isClickable;
                    if (!clickedButton.isClickable)
                    {
                        clickedButton.Text = "B";
                    }
                    else
                    {
                        clickedButton.Text = "";
                    }
                }
                CheckWin();
            }
        }
Exemplo n.º 12
0
        void OpenCell(int x, int y, FieldButton clickedButton) //открытие ячейки
        {
            //считаем количество бомб и выводим это количество, если не 0
            int bombsAround = CountBombsAround(x, y);

            if (bombsAround == 0)
            {
            }
            else
            {
                clickedButton.Text = "" + bombsAround;
            }
            clickedButton.Enabled = false;
        }
Exemplo n.º 13
0
        /// <summary>
        /// Metoda, która sprawdza jakim przyciskiem był przycisknięty przycisk i
        /// wyłowa rużne metody w zależności od rużnych faktorów i po przycisku sprawdza czy wygraliśmy
        /// </summary>
        /// <param name="sender">to objeck, czyli sam nasz przycisk</param>
        /// <param name="e">event, czyli jak przycisknieliśmy</param>
        void FieldButtonClick(object sender, MouseEventArgs e)
        {
            FieldButton clickedButton = (FieldButton)sender;

            if (e.Button == MouseButtons.Left && clickedButton.isClickable)
            {
                if (clickedButton.isBomb)
                {
                    if (isFirstClick)
                    {
                        clickedButton.isBomb = false;
                        isFirstClick         = false;
                        bombs--;
                        OpenRegion(clickedButton.xCoord, clickedButton.yCoord, clickedButton);
                    }
                    else
                    {
                        Explode();
                    }
                }
                else
                {
                    EmptyFieldButtonClick(clickedButton);
                }
                isFirstClick = false;
            }
            if (e.Button == MouseButtons.Right)
            {
                clickedButton.isClickable = !clickedButton.isClickable;
                if (!clickedButton.isClickable)
                {
                    clickedButton.Text = "B";
                }
                else
                {
                    clickedButton.Text = "";
                }
            }
            CheckWin();
        }
Exemplo n.º 14
0
        protected int bombAround(FieldButton bt)
        {
            //Считает бомбы вокруг нажатой кнопки
            int bombs = 0;
            var str   = bt.Name.Split(' ');
            var x1    = Convert.ToInt32(str[0]) - bWidth;
            var x2    = x1 + bWidth * 3;
            var y1    = Convert.ToInt32(str[1]) - bHight;
            var y2    = y1 + bHight * 3;

            for (int i = x1; i < x2; i += bWidth)
            {
                for (int j = y1; j < y2; j += bHight)
                {
                    FieldButton tempBt = fb.Find(button => button.Name == $"{i} {j}");
                    if (tempBt != null && tempBt.Bomb())
                    {
                        bombs++;
                    }
                }
            }
            return(bombs);
        }