Ejemplo n.º 1
0
        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 (BombsAround(currentCell.xCoord, currentCell.yCoord) == 0)
                {
                    for (int j = currentCell.yCoord - 1; j <= currentCell.yCoord + 1; j++)
                    {
                        for (int i = currentCell.xCoord - 1; i <= currentCell.xCoord + 1; i++)
                        {
                            if (i == currentCell.xCoord && j == currentCell.yCoord)
                            {
                                continue;
                            }
                            if (i >= 0 && i < width && j < height && j >= 0)
                            {
                                if (!field[i, j].wasAdded)
                                {
                                    queue.Enqueue(field[i, j]);
                                    field[i, j].wasAdded = true;
                                }
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
 void EmptyFieldButtonClick(FieldButton clickedButton)
 {
     for (int y = 0; y < height; y++)
     {
         for (int x = 0; x < width; x++)
         {
             if (field[x, y] == clickedButton)
             {
                 //bombsAround = CountBombsAround(x, y);
                 OpenRegion(x, y, clickedButton);
             }
         }
     }
 }
Ejemplo n.º 3
0
        void OpenCell(int x, int y, FieldButton clickedButton)
        {
            int bombsAround = BombsAround(x, y);

            if (bombsAround == 0)
            {
                //если 0 бомб вокруг, ничего не пишем
            }
            else
            {
                clickedButton.Text = "" + bombsAround; //записываем количество бомб вокруг
            }
            clickedButton.Enabled = false;
        }
Ejemplo n.º 4
0
        void FieldButtonClick(object sender, MouseEventArgs e)
        //превое нажатие всегда на пустую клетку
        //генерация заново значения isBomb клеток, если нажал на бомбу
        {
            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.Image = Image.FromFile("C:/Users/пк/Desktop/з.png");
                }
                else
                {
                    clickedButton.Image = Image.FromFile("C:/Users/пк/Desktop/4.png");
                }
            }
            Win();
        }
Ejemplo n.º 5
0
        private void сложныйToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Random random = new Random();

            for (int j = 0; j < height; j++)
            {
                for (int i = 0; i < width; i++)
                {
                    FieldButton newbutton = new FieldButton();
                    newbutton.Location    = new Point(i * size, j * size);
                    newbutton.Size        = new Size(size, size);
                    newbutton.isClickable = true;
                    if (random.Next(0, 100) <= 50)
                    {
                        newbutton.isBomb = true;
                    }
                    newbutton.xCoord = i;
                    newbutton.yCoord = j;
                    Controls.Add(newbutton);
                    newbutton.MouseUp += new MouseEventHandler(FieldButtonClick);
                    field[i, j]        = newbutton;
                }
            }
        }