Exemplo n.º 1
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 * offset);
                    newButton.Size        = new Size(offset, offset);
                    newButton.isClickable = true;

                    if (random.Next(0, 100) <= bombPercent)
                    {
                        newButton.isBomb = true;
                        bombs++;
                    }
                    newButton.xPosition = x;
                    newButton.yPosition = y;
                    Controls.Add(newButton);
                    newButton.MouseUp += new MouseEventHandler(FieldButtonClick);
                    field[x, y]        = newButton;
                }
            }
        }
Exemplo n.º 2
0
        void OpenRegion(int xPosition, int yPosition, FieldButton clickedButton)
        {
            Queue <FieldButton> queue = new Queue <FieldButton>();

            queue.Enqueue(clickedButton);
            clickedButton.wasAdded = true;

            while (queue.Count > 0)
            {
                FieldButton currentCell = queue.Dequeue();
                OpenCell(currentCell.xPosition, currentCell.yPosition, currentCell);
                Opened++;
                if (CountBombsAround(currentCell.xPosition, currentCell.yPosition) == 0)
                {
                    for (int y = currentCell.yPosition - 1; y <= currentCell.yPosition + 1; y++)
                    {
                        for (int x = currentCell.xPosition - 1; x <= currentCell.xPosition + 1; x++)  // функция автоматического раскрытия всех соседних пустых клеток
                        {
                            if (x == currentCell.xPosition && y == currentCell.yPosition)
                            {
                                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;
                                }
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 3
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.º 4
0
 private void FillMineField(int fieldSize)
 {
     for (int i = 0; i < fieldSize; i++)
     {
         for (int j = 0; j < fieldSize; j++)
         {
             var b = new FieldButton(i, j);
             b.Location   = new System.Drawing.Point(ButtonSize * i, ButtonSize * j);
             b.Click     += b_Click;
             b.MouseDown += b_MouseDown;
             this.Controls.Add(b);
         }
     }
 }
Exemplo n.º 5
0
 internal void Init(short boardWidth, short boardHeight, GameTimer gameTimer)
 {
     gameTimer.SetLabel(lblTime);
     this.buttons = new FieldButton[boardWidth, boardHeight];
     for (int i = 0; i < boardWidth; i++)
     {
         for (int j = 0; j < boardHeight; j++)
         {
             buttons[i, j]            = new FieldButton(i, j);
             buttons[i, j].MouseDown += OnFieldClick;
             buttons[i, j].SetBounds(margin + offsetX * i, margin + offsetY * j, buttonSize, buttonSize);
             Controls.Add(buttons[i, j]);
         }
     }
 }
Exemplo n.º 6
0
        void OpenCell(int x, int y, FieldButton clickedButton)
        {
            int bombsAround = CountBombsAround(x, y);

            if (bombsAround == 0)
            {
                clickedButton.Text = "";
            }
            else
            {
                clickedButton.Text = "" + bombsAround;
            }
            clickedButton.Enabled = false;

            CheckWin();
        }
Exemplo n.º 7
0
        private void SetUpFields()
        {
            for (int i = 1; i < 4; i++)
            {
                for (int j = 1; j < 4; j++)
                {
                    var coords = new Coordinates(i, j);
                    var field  = new FieldButton(coords, _game);
                    _fields.Add(field);
                }
            }

            foreach (var fieldButton in _fields)
            {
                Controls.Add(fieldButton);
            }
        }
Exemplo n.º 8
0
        void FieldButtonClick(object sender, MouseEventArgs e)
        {
            FieldButton clickedButton = (FieldButton)sender;

            if (e.Button == MouseButtons.Left && clickedButton.isClickable)
            {
                if (clickedButton.isBomb)
                {
                    if (FirstClick)
                    {
                        clickedButton.isBomb = false;
                        FirstClick           = false;             // условие проверки первого хода, чтобы первое нажатие всегда было на пустую клетку

                        OpenRegion(clickedButton.xPosition, clickedButton.yPosition, clickedButton);
                    }
                    else
                    {
                        Explode();
                    }
                }
                else
                {
                    EmptyFieldButtonClick(clickedButton);
                }
                FirstClick = false;
            }

            if (e.Button == MouseButtons.Right)
            {
                clickedButton.isClickable = !clickedButton.isClickable; // Метка бомбы правой кнопкой мыши
                if (!clickedButton.isClickable)
                {
                    clickedButton.Text = "🏲";
                }
                else
                {
                    clickedButton.Text = "";
                }
            }
            CheckWin();
        }