Exemplo n.º 1
0
        public void MineLeftClick(MineButton btn)
        {
            if (btn.state == MineButton.State.Empty ||
                btn.state == MineButton.State.Number)
            {
                return;
            }

            if (btn.IsBomb)
            {
                btn.SetState(MineButton.State.Exploded);
                EndGame(false);
            }
            else
            {
                if (btn.Value == 0)
                {
                    btn.SetState(MineButton.State.Empty);
                }
                else
                {
                    btn.SetState(MineButton.State.Number);
                }

                ClearOpenCells(btn.X, btn.Y);
            }

            SetRemainingMines();
        }
Exemplo n.º 2
0
        private void flagSquare(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                MineButton clickedButton = sender as MineButton;

                if (clickedButton.flagged)
                {
                    clickedButton.BackColor = default(Color);

                    clickedButton.flagged = false;
                }
                else
                {
                    clickedButton.BackColor = Color.Red;

                    clickedButton.flagged = true;
                }

                if (gameComplete())
                {
                    MessageBox.Show("You Win!");
                }
            }
        }
 private void InitializeMineButton(MineButton button, int posX, int posY)
 {
     button.BackColor   = SystemColors.ButtonFace;
     button.Location    = new Point(posX, posY);
     button.Size        = new Size(20, 20);
     button.MouseDown  += new MouseEventHandler(MineButton_Click);
     button.MouseEnter += new EventHandler(MineButton_Hover);
     Controls.Add(button);
 }
Exemplo n.º 4
0
 public FieldForm(int rows, int columns, int mine, int fieldSize)
 {
     InitializeComponent();
     Field = new MineButton[rows, columns];
     Rows = rows;
     Columns = columns;
     Mine = mine;
     FieldSize = FieldSize;
 }
Exemplo n.º 5
0
 private void NewGame(MineButton button)
 {
     ResetTextBoxes();
     ResetButtons();
     RandomizeMines(button);
     CalculateProximityValues();
     running = true;
     StartTimer();
 }
Exemplo n.º 6
0
 void DistroyMembers()
 {
     mineSweeper    = null;
     currentButtons = null;
     timer          = null;
     lblTimer       = null;
     currentMine    = null;
     gameOver       = false;
     pairs          = null;
 }
Exemplo n.º 7
0
        private void Lose(MineButton btn)
        {
            foreach (var button in mineButtons)
            {
                if (button.CellType == CellType.Mine)
                {
                    button.BackColor = Color.Black;
                }
            }

            running = false;
            timer.Stop();
        }
Exemplo n.º 8
0
        private void CheckCell(MineButton button)
        {
            switch (button.CellType)
            {
            case CellType.Mine:
                Lose(button);
                break;

            case CellType.Empty:
                ClearEmptyCells(button);
                break;

            case CellType.Number:
                DisplayNumber(button);
                break;
            }
        }
Exemplo n.º 9
0
 /// <summary>
 /// Handling the event of clicking on a mine
 /// whether it's a right, left or both like in the game
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void button_Click(object sender, MouseEventArgs e)
 {
     if (!gameOver)
     {
         currentMine = (MineButton)sender;
         if (e.Button == MouseButtons.Left)
         {
             leftRightClick = 1;
             timer.Enabled  = true;
         }
         else if (e.Button == MouseButtons.Right)
         {
             leftRightClick = 2;
             timer.Enabled  = true;
         }
     }
 }
Exemplo n.º 10
0
 private void initializeField()
 {
     for (int r = 0; r < Rows; r++) //rows
     {
         for (int c = 0; c < Columns; c++) //columns
         {
             Field[r, c] = new MineButton()
             {
                 Width = FieldSize,
                 Height = FieldSize,
                 Left = FieldSize * c,
                 Top = FieldSize * r,
             };
             this.Controls.Add(Field[r, c]);
         }
     }
     randomMines();
 }
Exemplo n.º 11
0
        private void ClearEmptyCells(MineButton button)
        {
            List <MineButton> searchButtons = new List <MineButton>();
            List <MineButton> foundButtons  = new List <MineButton>();
            List <MineButton> keepButtons   = new List <MineButton>();

            searchButtons.Add(button);

            do
            {
                foundButtons.Clear();

                foreach (var btn in searchButtons)
                {
                    foundButtons.AddRange(mineButtons
                                          .Where(b => b.XPosition >= (btn.XPosition - 1) &&
                                                 b.XPosition <= (btn.XPosition + 1) &&
                                                 b.YPosition >= (btn.YPosition - 1) &&
                                                 b.YPosition <= (btn.YPosition + 1) &&
                                                 b.CellType != CellType.Mine)
                                          .Except(keepButtons));
                }

                searchButtons.Clear();
                searchButtons.AddRange(foundButtons
                                       .Where(b => b.CellType == CellType.Empty));

                keepButtons.AddRange(foundButtons);
            } while (searchButtons.Count > 0);

            foreach (var btn in keepButtons)
            {
                if (btn.CellType == CellType.Empty)
                {
                    btn.Enabled  = false;
                    btn.FlagType = FlagType.Number;
                }
                else if (btn.CellType == CellType.Number)
                {
                    DisplayNumber(btn);
                }
            }
        }
Exemplo n.º 12
0
        private void RandomizeMines(MineButton button)
        {
            Random            random  = new Random();
            List <MineButton> rngList = new List <MineButton>();

            rngList.AddRange(mineButtons);

            var btnIndex = mineButtons.IndexOf(button);

            rngList.RemoveAt(btnIndex);

            for (int i = 0; i < nrMines; i++)
            {
                int randomIndex = random.Next(0, rngList.Count);
                var randomMine  = rngList[randomIndex];
                var newButton   = mineButtons.FirstOrDefault(b => b.XPosition == randomMine.XPosition && b.YPosition == randomMine.YPosition);
                newButton.CellType = CellType.Mine;
                rngList.Remove(randomMine);
            }
        }
Exemplo n.º 13
0
        private void InitializeButtons(object sender, EventArgs e)
        {
            mineButtons = new List <MineButton>();

            int posX = textBoxNrMines.Left;
            int posY = textBoxNrMines.Bottom + 5;

            for (int y = 1; y <= nrRows; y++)
            {
                for (int x = 1; x <= nrColumns; x++)
                {
                    MineButton button = new MineButton(x, y);
                    InitializeMineButton(button, posX, posY);
                    mineButtons.Add(button);

                    posX += 20;
                }
                posX -= 20 * nrColumns;
                posY += 20;
            }
        }
Exemplo n.º 14
0
        public GameProcessor(int width, int height, ref Grid mainGrid, Label counterLabel)
        {
            grid    = new MineButton[width][];
            _width  = width;
            _height = height;

            _counterLabel = counterLabel;

            for (var i = 0; i < width; ++i)
            {
                mainGrid.ColumnDefinitions.Add(new ColumnDefinition());
                grid[i] = new MineButton[height];
            }

            for (var i = 0; i < height; ++i)
            {
                mainGrid.RowDefinitions.Add(new RowDefinition());
            }

            GenerateMap(width, height, ref mainGrid);
        }
Exemplo n.º 15
0
        private void DisplayNumber(MineButton button)
        {
            switch (button.Number)
            {
            case 1:
                button.ForeColor = Color.Green;
                break;

            case 2:
                button.ForeColor = Color.Blue;
                break;

            case 3:
                button.ForeColor = Color.DarkOrange;
                break;

            case 4:
                button.ForeColor = Color.DarkRed;
                break;

            case 5:
                button.ForeColor = Color.Purple;
                break;

            case 6:
                button.ForeColor = Color.Brown;
                break;

            case 7:
            case 8:
                button.ForeColor = Color.Black;
                break;
            }
            button.Font     = new Font("Microsoft Sans Serif", 8.25F, FontStyle.Bold, GraphicsUnit.Point, 0);
            button.Text     = button.Number.ToString();
            button.FlagType = FlagType.Number;
        }
Exemplo n.º 16
0
 private void lbl_Click(object sender, MouseEventArgs e)
 {
     if (!gameOver)
     {
         ClickResult   aResult = ClickResult.DoNothing;
         NumberedLabel aLabel  = (NumberedLabel)sender;
         currentMine = currentButtons[aLabel.RowNumber, aLabel.ColumnNumber];
         if (e.Button == MouseButtons.Left)
         {
             if (lblTimer.Enabled && leftRightClick == 2)
             {
                 leftRightClick = 0;
                 pairs.Clear();
                 aResult          = mineSweeper.DoubleClickEvent(currentMine.RowNumber, currentMine.ColumnNumber, pairs);
                 lblTimer.Enabled = false;
                 HandleButtonClicked(aResult, -1);
                 return;
             }
             leftRightClick   = 1;
             lblTimer.Enabled = true;
         }
         else if (e.Button == MouseButtons.Right)
         {
             if (lblTimer.Enabled && leftRightClick == 1)
             {
                 leftRightClick = 0;
                 pairs.Clear();
                 aResult          = mineSweeper.DoubleClickEvent(currentMine.RowNumber, currentMine.ColumnNumber, pairs);
                 lblTimer.Enabled = false;
                 HandleButtonClicked(aResult, -1);
                 return;
             }
             leftRightClick   = 2;
             lblTimer.Enabled = true;
         }
     }
 }
Exemplo n.º 17
0
        /// <summary>
        /// Continue of the initialization of each button and set each button attributes, drawing and event handling
        /// </summary>
        private void DrawButtons()
        {
            Point currentPoint;

            for (int i = 0; i < mineSweeper.CurrentHeight; i++)
            {
                for (int j = 0; j < mineSweeper.CurrentWidth; j++)
                {
                    currentButtons[i, j]        = new MineButton();
                    currentButtons[i, j].Width  = 20;
                    currentButtons[i, j].Height = 20;
                    currentPoint = new Point(j * 20, i * 20 + 25);
                    currentButtons[i, j].Location = currentPoint;
                    currentButtons[i, j].UseVisualStyleBackColor = true;
                    this.Controls.Add(currentButtons[i, j]);
                    currentButtons[i, j].RowNumber    = i;
                    currentButtons[i, j].ColumnNumber = j;
                    currentButtons[i, j].MouseDown   += new MouseEventHandler(this.button_Click);
                }
            }
            this.Width           = mineSweeper.CurrentWidth * 20 + 10;
            this.Height          = mineSweeper.CurrentHeight * 20 + 60;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
        }
Exemplo n.º 18
0
 public void MineRightClick(MineButton btn)
 {
     SetRemainingMines();
 }
Exemplo n.º 19
0
        private void GenerateMap(int width, int height, ref Grid mainGrid)
        {
            for (var i = 0; i < width; ++i)
            {
                for (var j = 0; j < height; ++j)
                {
                    var btn = new MineButton((ushort)i, (ushort)j)
                    {
                        Name = $"cell_{i}_{j}"
                    };

                    btn.Click += btn.OnLeftClick;
                    btn.MouseRightButtonDown += btn.OnRightClick;
                    btn.LeftClick             = MineLeftClick;
                    btn.RightClick            = MineRightClick;
                    btn.FontWeight            = FontWeights.UltraBold;

                    Grid.SetColumn(btn, j);
                    Grid.SetRow(btn, i);
                    mainGrid.Children.Add(btn);

                    grid[i][j] = btn;
                }
            }

            var mineCounter = (int)(0.2249 * (_width * _height) - 11);

            MineCount = mineCounter;

            var rand = new Random((new DateTime()).Millisecond);

            while (mineCounter > 0)
            {
                var xLoc = rand.Next(0, _width);
                var yLoc = rand.Next(0, _height);

                if (grid[xLoc][yLoc].IsBomb)
                {
                    continue;
                }

                grid[xLoc][yLoc].IsBomb = true;
                //update numeric values
                for (var i = -1; i <= 1; ++i)
                {
                    if (xLoc + i < 0 || xLoc + i >= _width)
                    {
                        continue;
                    }
                    for (var j = -1; j <= 1; ++j)
                    {
                        if (yLoc + j < 0 || yLoc + j >= _height)
                        {
                            continue;
                        }
                        ++grid[xLoc + i][yLoc + j].Value;
                    }
                }

                --mineCounter;
            }

            SetRemainingMines();

            //Test view
            for (int i = 0; i < _width && false; ++i)
            {
                for (int j = 0; j < _height; ++j)
                {
                    var btn = grid[i][j];
                    if (btn.IsBomb)
                    {
                        btn.Content = "B";
                        //btn.Content = btn.Value.ToString();
                    }
                    else
                    {
                        btn.Content = btn.Value.ToString();
                    }
                }
            }
        }
Exemplo n.º 20
0
        private void ButtonClick(object sender, EventArgs e)
        {
            MineButton clickedButton = sender as MineButton;

            if (!clickedButton.flagged)
            {
                int clickedButtonX = clickedButton.x;
                int clickedButtonY = clickedButton.y;

                if (clickedButton.mine)
                {
                    MessageBox.Show("Game over!");

                    flowLayoutPanel1.Visible = false;
                }
                else if (clickedButton.mineCount > 0)
                {
                    clickedButton.Enabled = false;

                    clickedButton.Text = clickedButton.mineCount.ToString();
                }
                else
                {
                    clickedButton.Enabled = false;

                    foreach (MineButton c in flowLayoutPanel1.Controls)
                    {
                        MineButton currentButton = c;

                        // UP CLICK
                        if ((currentButton.x + 1 == clickedButton.x) && (currentButton.y == clickedButton.y) && !currentButton.mine)
                        {
                            if (c.mineCount == 0)
                            {
                                c.PerformClick();

                                c.Enabled = false;
                            }
                            else
                            {
                                c.Enabled = false;

                                currentButton.Text = currentButton.mineCount.ToString();
                            }
                        }

                        // DOWN CLICK
                        if ((currentButton.x - 1 == clickedButton.x) && (currentButton.y == clickedButton.y) && !currentButton.mine)
                        {
                            if (c.mineCount == 0)
                            {
                                c.PerformClick();

                                c.Enabled = false;
                            }
                            else
                            {
                                c.Enabled = false;

                                currentButton.Text = currentButton.mineCount.ToString();
                            }
                        }

                        // RIGHT CLICK
                        if ((currentButton.x == clickedButton.x) && (currentButton.y - 1 == clickedButton.y) && !currentButton.mine)
                        {
                            if (c.mineCount == 0)
                            {
                                c.PerformClick();

                                c.Enabled = false;
                            }
                            else
                            {
                                c.Enabled = false;

                                currentButton.Text = currentButton.mineCount.ToString();
                            }
                        }

                        // LEFT CLICK
                        if ((currentButton.x == clickedButton.x) && (currentButton.y + 1 == clickedButton.y) && !currentButton.mine)
                        {
                            if (c.mineCount == 0)
                            {
                                c.PerformClick();

                                c.Enabled = false;
                            }
                            else
                            {
                                c.Enabled = false;

                                currentButton.Text = currentButton.mineCount.ToString();
                            }
                        }

                        if ((currentButton.x + 1 == clickedButton.x) && (currentButton.y + 1 == clickedButton.y) && !currentButton.mine)
                        {
                            if (c.mineCount == 0)
                            {
                                c.PerformClick();

                                c.Enabled = false;
                            }
                            else
                            {
                                c.Enabled = false;

                                currentButton.Text = currentButton.mineCount.ToString();
                            }
                        }

                        if ((currentButton.x - 1 == clickedButton.x) && (currentButton.y - 1 == clickedButton.y) && !currentButton.mine)
                        {
                            if (c.mineCount == 0)
                            {
                                c.PerformClick();

                                c.Enabled = false;
                            }
                            else
                            {
                                c.Enabled = false;

                                currentButton.Text = currentButton.mineCount.ToString();
                            }
                        }

                        if ((currentButton.x - 1 == clickedButton.x) && (currentButton.y + 1 == clickedButton.y) && !currentButton.mine)
                        {
                            if (c.mineCount == 0)
                            {
                                c.PerformClick();

                                c.Enabled = false;
                            }
                            else
                            {
                                c.Enabled = false;

                                currentButton.Text = currentButton.mineCount.ToString();
                            }
                        }

                        if ((currentButton.x + 1 == clickedButton.x) && (currentButton.y - 1 == clickedButton.y) && !currentButton.mine)
                        {
                            if (c.mineCount == 0)
                            {
                                c.PerformClick();

                                c.Enabled = false;
                            }
                            else
                            {
                                c.Enabled = false;

                                currentButton.Text = currentButton.mineCount.ToString();
                            }
                        }
                    }
                }
            }

            if (gameComplete())
            {
                MessageBox.Show("You Win!");
            }
        }