Ejemplo n.º 1
0
        /** Ganarate field with buttons */
        void GenerateField()
        {
            Random random = new Random();

            buttonsField = new CellButton[fieldWidth, fieldHeight];

            for (int i = 10; (i - 10) < fieldWidth * distanceBetweenButtons; i += distanceBetweenButtons)
            {
                for (int j = 30; (j - 30) < fieldHeight * distanceBetweenButtons; j += distanceBetweenButtons)
                {
                    CellButton button = new CellButton
                    {
                        Location = new Point(i, j),
                        Size     = new Size(30, 30)
                    };

                    buttonsField[(i - 10) / distanceBetweenButtons, (j - 30) / distanceBetweenButtons] = button;
                    button.XCoord1 = (i - 10) / distanceBetweenButtons;
                    button.YCoord1 = (j - 30) / distanceBetweenButtons;
                    Controls.Add(button);
                    button.MouseClick += new MouseEventHandler(FieldClick);
                }
            }

            AddBombs(bombCount);
        }
Ejemplo n.º 2
0
        /** Count neighbours around a cell */
        int CountNeighbours(CellButton button)
        {
            int neighbours = 0;

            for (int i = button.XCoord1 - 1; i <= button.XCoord1 + 1; i++)
            {
                for (int j = button.YCoord1 - 1; j <= button.YCoord1 + 1; j++)
                {
                    if (i >= 0 && i < fieldWidth && j >= 0 && j < fieldHeight)
                    {
                        if (buttonsField[i, j].HasBomb)
                        {
                            neighbours++;
                        }
                    }
                }
            }

            button.NeighboursCount = neighbours;
            return(button.NeighboursCount);
        }
Ejemplo n.º 3
0
        /** Explode all bombs on the field */
        void BombExplode(CellButton button)
        {
            button.Text = "*";

            for (int i = 0; i < fieldWidth; i++)
            {
                for (int j = 0; j < fieldHeight; j++)
                {
                    if (buttonsField[i, j].HasBomb)
                    {
                        buttonsField[i, j].Text = "*";
                    }

                    buttonsField[i, j].Enabled = false;
                }
            }

            if (DialogResult.OK == MessageBox.Show("You loose!"))
            {
                GameSetup();
            }
        }
Ejemplo n.º 4
0
        /** Open a cell area with cells wihout numbers */
        void OpenEmptyCells(CellButton button)
        {
            EmptyCellClick(button);

            for (int i = button.XCoord1 - 1; i <= button.XCoord1 + 1; i++)
            {
                for (int j = button.YCoord1 - 1; j <= button.YCoord1 + 1; j++)
                {
                    if (i >= 0 && i < fieldWidth && j >= 0 && j < fieldHeight)
                    {
                        if (!buttonsField[i, j].IsActivated &&
                            CountNeighbours(buttonsField[i, j]) == 0)
                        {
                            OpenEmptyCells(buttonsField[i, j]);
                        }
                        else if (!buttonsField[i, j].HasBomb)
                        {
                            EmptyCellClick(buttonsField[i, j]);
                        }
                    }
                }
            }
        }
Ejemplo n.º 5
0
        /** Handle click on cell without a bomb */
        void EmptyCellClick(CellButton button)
        {
            button.BackColor = Color.DarkGray;
            CountNeighbours(button);

            if (button.NeighboursCount != 0)
            {
                button.Text = button.NeighboursCount.ToString();
            }

            if (!button.IsActivated)
            {
                openCellsCount++;
            }

            button.IsActivated = true;

            if (openCellsCount == fieldHeight * fieldWidth - bombCount)
            {
                for (int i = 0; i < fieldWidth; i++)
                {
                    for (int j = 0; j < fieldHeight; j++)
                    {
                        if (buttonsField[i, j].HasBomb)
                        {
                            buttonsField[i, j].BackColor = Color.LightGreen;
                        }
                    }
                }

                if (DialogResult.OK == MessageBox.Show("Congratulations! You win!"))
                {
                    Application.Exit();
                }
            }
        }
Ejemplo n.º 6
0
        /** Handle click on the field */
        void FieldClick(object sender, MouseEventArgs e)
        {
            CellButton button = (CellButton)sender;

            switch (e.Button)
            {
            case (MouseButtons.Left):
            {
                if (button.IsActivated || button.Text == "X")
                {
                    break;
                }

                if (button.HasBomb)
                {
                    if (firstClick)
                    {
                        button.HasBomb = false;

                        // add new bomb
                        AddBombs(1);
                        EmptyCellClick(button);
                    }
                    else
                    {
                        BombExplode(button);
                    }
                }
                else
                {
                    EmptyCellClick(button);
                    if (button.NeighboursCount == 0)
                    {
                        OpenEmptyCells(button);
                    }
                }

                firstClick         = false;
                button.IsActivated = true;

                break;
            }

            case MouseButtons.Right:
            {
                if (button.IsActivated)
                {
                    break;
                }

                if (button.BackColor != Color.LightPink)
                {
                    button.BackColor = Color.LightPink;
                    button.Text      = "X";
                }
                else
                {
                    button.BackColor = Control.DefaultBackColor;
                    button.UseVisualStyleBackColor = true;
                    button.Text = "";
                }

                break;
            }
            }
        }