Exemple #1
0
        //event handler for right mouse clicks so user can place 'flags'
        void gridEvent_RightClick(object sender, MouseEventArgs e)
        {
            Button rghtBtn = sender as Button;
            Save saveScore = new Save(diff, sec, player);

            int x = int.Parse(rghtBtn.Name.Split()[0]);                  //parse button name x cordinate to an int
            int y = int.Parse(rghtBtn.Name.Split()[1]);                  //parse button name x cordinate to an int
            rghtBtn.Enabled = true;
            if(e.Button == MouseButtons.Right && rghtBtn.BackColor == Color.LightGray)
            {
                if (!lost)
                {
                    rghtBtn.BackColor = Color.OrangeRed;                     //change colour of button to mark its been flagged
                    isMrkd[x, y] = true;                                     //set isMarkd value to true
                    if (won = checkWon())                                     //check to see if game has been won
                    {
                        timer1.Enabled = false;
                        saveScore.save();
                        playerW.Play();                                      //play winning sound
                        MessageBox.Show("Congratulations " + player + " you won!");
                    }
                }
                else if (lost)
                {
                    MessageBox.Show("Game over " + player + "! You hit a mine..."); //if lost is true display msg box
                }
            }
            else if(e.Button == MouseButtons.Right && rghtBtn.BackColor == Color.OrangeRed)
            {
                rghtBtn.BackColor = Color.LightGray;                    //change colour of button back to normal
                isMrkd[x, y] = false;                                   //set isMrkd value to false
            }
        }
Exemple #2
0
        //event handler to deal with button clicks
        void gridEvent_Click(object sender, EventArgs e)
        {
            Button btnClk = sender as Button;                           //create a new button object and set sender as the object
            Save saveScore = new Save(diff, sec, player);

            if (lost) { MessageBox.Show("Game over " + player + "! You hit a mine..."); }//if lost is true display msg box

            int x = int.Parse(btnClk.Name.Split()[0]);                  //parse button name x cordinate to an int
            int y = int.Parse(btnClk.Name.Split()[1]);                  //parse button name x cordinate to an int

            if (gridNum[x, y] == -1)                                    //check if grid space contains a mine
            {
                showAll();                                              //show all mines if true
                timer1.Enabled = false;                                 //deactivate the timer
                playerL.Play();                                         //play losing sound
                MessageBox.Show("Game over " + player + "! You hit a mine...");                           //display Game Over message box if user clicks on a mine
                lost = true;                                            //set lost to true if a mine is discovered
            }

            if (gridNum[x, y] == 0 && lost != true) { clrNeighbour(x, y); }//if gridNum[x,y] is 0 call clrNeighbour to clear adjacent 0 values

            if (!lost) { btnClk.Visible = false; }                      //set visibility to false so the button is not seen once clicked

            if (won = checkWon())                                       //check to see if game has been won
            {
                timer1.Enabled = false;                                 //stop timer if game is won
                saveScore.save();
                playerW.Play();                                         //play winning sound
                MessageBox.Show("Congratulations " + player + " you won!");
            }
        }