Example #1
0
 static void Main()
 {
     Application.EnableVisualStyles();
     Application.SetCompatibleTextRenderingDefault(false);
     minesweeperForm = new MinesweeperForm();
     MinesweeperGame.Initialize(minesweeperForm);
     Application.Run(minesweeperForm);
 }
        // reveal tiles based on tile state
        private static void Reveal(int i, int j)
        {
            MinesweeperGrid g = grid[i, j];
            Button          b = MinesweeperGame.GetButton(i, j);

            b.FlatStyle = FlatStyle.Flat;
            b.BackColor = System.Drawing.Color.LightGray;

            // show bomb if its a mine
            if (g.IsMine)
            {
                b.Text = "\uD83D\uDCA3";
            }
            // show number with colors
            else if (g.Value > 0)
            {
                b.Text = g.Value.ToString();
                switch (g.Value)
                {
                case 1:
                    b.ForeColor = System.Drawing.Color.Blue;
                    break;

                case 2:
                    b.ForeColor = System.Drawing.Color.Green;
                    break;

                case 3:
                    b.ForeColor = System.Drawing.Color.Red;
                    break;

                case 4:
                    b.ForeColor = System.Drawing.Color.DarkBlue;
                    break;

                case 5:
                    b.ForeColor = System.Drawing.Color.DarkRed;
                    break;

                case 6:
                    b.ForeColor = System.Drawing.Color.DarkCyan;
                    break;
                }
            }
        }
 // handle win
 private static void EndGame()
 {
     // reveal all mines and set them green
     for (int k = 0; k < grid.GetLength(0); k++)
     {
         for (int l = 0; l < grid.GetLength(1); l++)
         {
             if (grid[k, l].IsMine)
             {
                 Reveal(k, l);
                 MinesweeperGame.GetButton(k, l).ForeColor = System.Drawing.Color.Black;
                 MinesweeperGame.GetButton(k, l).BackColor = System.Drawing.Color.Green;
             }
         }
     }
     // make cool face
     MinesweeperGame.Control_button.Text = "\uD83D\uDE0E";
     gameOver = true;
 }
        // lost the game
        private static void EndGame(int i, int j)
        {
            // reveal all mines
            Button b = MinesweeperGame.GetButton(i, j);

            for (int k = 0; k < grid.GetLength(0); k++)
            {
                for (int l = 0; l < grid.GetLength(1); l++)
                {
                    if (grid[k, l].IsMine)
                    {
                        Reveal(k, l);
                    }
                }
            }
            b.BackColor = System.Drawing.Color.Red;
            // made dead face
            MinesweeperGame.Control_button.Text = "\u2620";
            gameOver = true;
        }
 // reset game
 public static void Reset()
 {
     // reset board state
     Initialize(_r, _c, _m);
     // reset button styles
     for (int i = 0; i < grid.GetLength(0); i++)
     {
         for (int j = 0; j < grid.GetLength(1); j++)
         {
             Button b = MinesweeperGame.GetButton(i, j);
             b.FlatStyle = FlatStyle.Popup;
             b.ForeColor = System.Drawing.Color.Black;
             b.BackColor = default(System.Drawing.Color);
             b.Text      = String.Empty;
         }
     }
     // reset control face
     MinesweeperGame.Control_button.Text = "\uD83D\uDE42";
     // enable control
     gameOver = false;
 }
        // check to see if the player has won
        private static void CheckEnd()
        {
            int count = 0;

            for (int i = 0; i < grid.GetLength(0); i++)
            {
                for (int j = 0; j < grid.GetLength(1); j++)
                {
                    if (MinesweeperGame.GetButton(i, j).FlatStyle == FlatStyle.Flat && grid[i, j].IsMine == false)
                    {
                        count++;
                    }
                }
            }

            // if total opened equals the total - mines
            if (count == (_r * _c - _m))
            {
                EndGame();
            }
        }
        // handles right click (places flag)
        public static void RightClick(int i, int j)
        {
            if (gameOver)
            {
                return;
            }

            // if it is not clicked --> place a flag
            Button b = MinesweeperGame.GetButton(i, j);

            if (b.FlatStyle == FlatStyle.Popup)
            {
                if (b.Text.Equals("\u2691"))
                {
                    b.Text      = "";
                    b.ForeColor = System.Drawing.Color.Black;
                }
                else
                {
                    b.Text      = "\u2691";
                    b.ForeColor = System.Drawing.Color.Red;
                }
            }
        }