Exemple #1
0
 static void Main()
 {
     Application.EnableVisualStyles();
     Application.SetCompatibleTextRenderingDefault(false);
     minesweeperForm = new MinesweeperForm();
     MinesweeperGame.Initialize(minesweeperForm);
     Application.Run(minesweeperForm);
 }
Exemple #2
0
 public Main(int size)
 {
     if (size == 10)
     {
         level = "Easy";
     }
     if (size == 15)
     {
         level = "Normal";
     }
     if (size == 20)
     {
         level = "Hard";
     }
     minesweeperGame = new MinesweeperGame(size);
     InitializeComponent(size, minesweeperGame);
 }
        // 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;
                }
            }
        }
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent(int size, MinesweeperGame minesweeperGame)
        {
            this.SuspendLayout();
            //
            // Main
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode       = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize          = new System.Drawing.Size(800, 450);
            this.Name     = "Main";
            this.ShowIcon = false;
            this.Text     = "MineSweeper";
            this.ResumeLayout(false);
            //centers the game, prevents resizing, and makes it the appropriate size for how many buttons there are going to be
            this.StartPosition   = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.MaximizeBox     = false;
            this.MinimizeBox     = false;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Size            = new Size((size * 50) + 16, (size * 50) + 80);
            //
            // timer
            //
            this.timer          = new Timer();
            this.timer.Enabled  = true;
            this.timer.Interval = 1000;
            this.timer.Tick    += new EventHandler(this.timer_Tick);
            //
            // seconds label
            //
            this.seconds          = new Label();
            this.seconds.AutoSize = true;
            this.seconds.Font     = new System.Drawing.Font("Microsoft Sans Serif", 27.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.seconds.Location = new System.Drawing.Point(110, 0);
            this.seconds.Name     = "seconds";
            this.seconds.Size     = new System.Drawing.Size(30, 21);
            this.seconds.Text     = "00";
            this.Controls.Add(this.seconds);
            //
            // minutes label
            //
            this.minutes          = new Label();
            this.minutes.AutoSize = true;
            this.minutes.Font     = new System.Drawing.Font("Microsoft Sans Serif", 27.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.minutes.Location = new System.Drawing.Point(55, 0);
            this.minutes.Name     = "minutes";
            this.minutes.Size     = new System.Drawing.Size(30, 21);
            this.minutes.Text     = "00:";
            this.Controls.Add(this.minutes);
            //
            // hours label
            //
            this.hours          = new Label();
            this.hours.AutoSize = true;
            this.hours.Font     = new System.Drawing.Font("Microsoft Sans Serif", 27.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.hours.Location = new System.Drawing.Point(0, 0);
            this.hours.Name     = "hours";
            this.hours.Size     = new System.Drawing.Size(30, 21);
            this.hours.Text     = "00:";
            this.Controls.Add(this.hours);

            //creates the grid of buttons and adds them to the form according to its position in the 2D array
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    minesweeperGame.grid[j, i].Size            = new Size(50, 50);
                    minesweeperGame.grid[j, i].BackgroundImage = Image.FromFile(Path.Combine(Environment.CurrentDirectory, "Icons/unexplored.png"));
                    minesweeperGame.grid[j, i].ImageResize();
                    minesweeperGame.grid[j, i].Location = new Point(j * 50, (i * 50) + 41);
                    this.Controls.Add(minesweeperGame.grid[j, i]);
                    this.minesweeperGame.grid[j, i].MouseDown += new System.Windows.Forms.MouseEventHandler(this.CellClick);
                }
            }
        }