示例#1
0
        private void MineButton_Click(object sender, EventArgs e)
        {
            MineButton     mineButton = (MineButton)sender;
            MouseEventArgs args       = e as MouseEventArgs;

            if (mineButton.IsDisposed)
            {
                // Not sure if this will come into effect
                // It's just incase of weirdness with this method and its button's events
                // causing this being called twice on the same button
                return;
            }

            int PosX = mineButton.x, PosY = mineButton.y;

            switch (args?.Button)
            {
            case null:                     // The case of the automatic click :/
            case MouseButtons.Left:
                if (mineButton.IsFlagged)
                {
                    // Can't click flagged buttons
                    return;
                }

                // Making it so your first click can't be a mine
                if (isFirstClick)
                {
                    isFirstClick = false;
                    while (gameMap[PosX, PosY])
                    {
                        gameMap.ReGenerateMines();
                    }
                }

                int minesAroundCount = gameMap.MinesAroundPosition(PosX, PosY);

                if (gameMap[PosX, PosY] == true)
                {
                    // Lose; Clicked on mine
                    foreach (MineButton button in explosiveButtons)
                    {
                        button.BackColor = Color.Black;
                        button.ForeColor = Color.White;
                        button.Text      = "💣";
                    }
                    mineButton.BackColor = Color.Red;
                    mineButton.ForeColor = Color.Black;

                    MakeAllButtonsUnclickable();
                    restartButton.Text      = "💀";
                    restartButton.ForeColor = Color.Red;
                }
                else if (minesAroundCount == 0)
                {
                    mineButton.Dispose();
                    spawnedMineButtons.Remove(mineButton);
                    // No mines around button, Going to click them automatically
                    List <MineButton> mineButtons = GetControlsAroundPoint <MineButton>(PosX, PosY);                           // Creating a list of buttons to click

                    // Click Each of the mineButtons around me
                    foreach (MineButton button in mineButtons)
                    {
                        // Unflagging these buttons because It's 100% true that these have no mines under them
                        button.IsFlagged = false;

                        // Doing the weird thing with adding and removing from the event
                        // because PerformClick only works with the Click event
                        // And I am using this because this method is usually in
                        // the MouseUp event, and that is because the Click event
                        // won't f*****g trigger on right clicks! FFFUUUUUUUU
                        button.Click += MineButton_Click;
                        button.PerformClick();                                 // Clicks with EventArgs not MouseEventArgs
                        button.Click -= MineButton_Click;
                    }
                }
                else if (minesAroundCount != 0)
                {
                    // It has some mines around it, so tell it to just put a label with the number of mines
                    mineButton.Dispose();

                    Label label = SpawnLabel(PosX, PosY, minesAroundCount);
                    label.ScaleFont();
                    label.ForeColor = NumberColors[minesAroundCount];
                    label.MouseUp  += Label_Click;
                }

                if (spawnedMineButtons.SequenceEqual(explosiveButtons))
                {
                    // Player Won! Do winning stuff
                    MakeAllButtonsUnclickable();

                    restartButton.BackColor = Color.LightYellow;
                    restartButton.ForeColor = Color.DarkOrange;
                    restartButton.Text      = "😎";

                    if (!playerWon)
                    {
                        MessageBox.Show("A Winnar Is You!", "WIN!", MessageBoxButtons.OK);

                        System.Diagnostics.Debug.WriteLine("Player Won!");
                        playerWon = true;
                    }
                }

                break;

            case MouseButtons.Right:
                // Do Flag Stuff
                mineButton.IsFlagged = !mineButton.IsFlagged;

                UpdateFlagLabelDisplay();

                break;
            }
        }