Example #1
0
        public void GenerateField() ///tworzy pole
        {
            Random random = new Random();

            for (int y = 0; y < height; y++)                                             ///tworzy pole przyciskow(button) (kolumny)
            {
                for (int x = 0; x < width; x++)                                          ///tworzy pole przyciskow(button) (wiersz)
                {
                    FieldButton newButton = new FieldButton();                           /// tworzy obiekt
                    newButton.Location    = new Point(x * offset + 40, y * offset + 40); ///ustawienie pozycje przyciskow
                    newButton.Size        = new Size(offset, offset);                    ///ustawienie rozmiaru przuciskow
                    newButton.isClickable = true;                                        ///przycisk jest klikalny
                    if (random.Next(0, 100) <= bombPercent)                              ///tworzy bomby
                    {
                        newButton.isBomb = true;
                        bombs++;
                    }
                    newButton.xCoord = x;
                    newButton.yCoord = y;
                    Controls.Add(newButton);                                      /// dodawanie przuciskow
                    newButton.MouseUp += new MouseEventHandler(FieldButtonClick); ///zapisuje na kazde pole zdarzenie, które wywoła metodę FieldButtonClick
                    field[x, y]        = newButton;
                }
            }
        }
Example #2
0
        void OpenRegion(int xCoord, int yCoord, FieldButton clickedButton)/// otwiera pola
        {
            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 < height && y >= 0)
                            {
                                if (!field[x, y].wasAdded)
                                {
                                    queue.Enqueue(field[x, y]);
                                    field[x, y].wasAdded = true;
                                }
                            }
                        }
                    }
                }
            }
        }
Example #3
0
void OpenCell(int x, int y, FieldButton clickedButton)
        {
            int bombsAround = CountBombsAround(x, y);
            if (bombsAround == 0)
            {

            }
            else
            {
                clickedButton.Text = "" + bombsAround;
            }
            clickedButton.Enabled = false;
        }
Example #4
0
 void EmptyFieldButtonClick(FieldButton clickedButton) ///otwiera pola, jeśli nie ma bomb
 {
     for (int y = 0; y < height; y++)
     {
         for (int x = 0; x < width; x++)
         {
             if (field[x, y] == clickedButton)
             {
                 OpenRegion(x, y, clickedButton);
             }
         }
     }
 }
Example #5
0
        void FieldButtonClick(object sender, MouseEventArgs e) /// funkcja, która zostanie wywołana po naciśnięciu przycisku
        {
            FieldButton clickedButton = (FieldButton)sender;   /// zmieniamy object na FeildButton

            if (e.Button == MouseButtons.Left && clickedButton.isClickable)
            {
                if (clickedButton.isBomb)
                {
                    if (isFirstClick) /// robi to, aby nie wybuchnac od razu
                    {
                        clickedButton.isBomb = false;
                        bombs--;
                        OpenRegion(clickedButton.xCoord, clickedButton.yCoord, clickedButton); ///otwarcie pola
                    }
                    else
                    {
                        Explode(); // wybuch
                    }
                }
                else
                {
                    EmptyFieldButtonClick(clickedButton); ///otwiera pola, jeśli nie ma bomb
                }
                isFirstClick = false;
            }
            if (e.Button == MouseButtons.Right)  ///jeśli klikniesz prawym przyciskiem myszy
            {
                clickedButton.isClickable = !clickedButton.isClickable;
                if (!clickedButton.isClickable)
                {
                    clickedButton.Text = "B";
                }
                else
                {
                    clickedButton.Text = "";
                }
            }
            CheckWin();
        }
Example #6
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);
                    }
                }
            }

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 < height && y >= 0)
                            {
                                if (!field[x, y].wasAdded)
                                {
                                    queue.Enqueue(field[x, y]);
                                    field[x, y].wasAdded = true;
                                }
                            }
                        }
                    }
                }
            }

        }


void OpenCell(int x, int y, FieldButton clickedButton)
        {
            int bombsAround = CountBombsAround(x, y);
            if (bombsAround == 0)
            {

            }
            else
            {
                clickedButton.Text = "" + bombsAround;
            }
            clickedButton.Enabled = false;
        }

        int CountBombsAround(int xCoord, int yCoord)
        {
            int bombsAround = 0;
            for (int x = xCoord - 1; x <= xCoord + 1; x++)
            {
                for (int y = yCoord - 1; y <= yCoord + 1; y++)
                {
                    if (x >= 0 && x < width && y >= 0 && y < height)
                    {
                        if (field[x, y].isBomb == true)
                        {
                            bombsAround++;
                        }
                    }
                }
            }
            return bombsAround;
        }

        void CheckWin()
        {
            int cells = width * height;
            int emptyCells = cells - bombs;
            if (cellsOpened == emptyCells)
            {
                MessageBox.Show("you win! ;)");
                Application.Restart();
                
            }
        }