Example #1
0
        private void frmGuessTheNumber_Load(object sender, EventArgs e)
        {
            frmGameLevel gameLevel = new frmGameLevel();

            gameLevel.ShowDialog();
            this.Hide();
            newGame();
        }
Example #2
0
        // Method to check whether user's guess is too high or too low
        public void checkGuess()
        {
            // Gets the user's guess
            String guessText = txtGuess.Text.ToString();
            String message   = "";

            try
            {
                // Checks the guess
                int guess = int.Parse(guessText);

                // Count this as one try
                triesLeft--;

                // Too high
                if (guess > theNumber)
                {
                    message        = guess + " was too high. Guess again.";
                    lblOutput.Text = message;
                    lblTries.Text  = "You have " + triesLeft + " tries remaining";
                }
                // Too low
                else if (guess < theNumber)
                {
                    message        = guess + " was too low. Guess again.";
                    lblOutput.Text = message;
                    lblTries.Text  = "You have " + triesLeft + " tries remaining";
                }

                // Correct
                else
                {
                    message        = guess + " was correct. You win! Let's play again!";
                    lblOutput.Text = message;
                    if (lblGuess.Text.ToString() == "Guess a number between 1 and 10:")
                    {
                        lblTries.Text = "You guessed in " + (4 - triesLeft) + " tries.";
                    }
                    else if (lblGuess.Text.ToString() == "Guess a number between 1 and 100:")
                    {
                        lblTries.Text = "You guessed in " + (7 - triesLeft) + " tries.";
                    }
                    else if (lblGuess.Text.ToString() == "Guess a number between 1 and 1000:")
                    {
                        lblTries.Text = "You guessed in " + (9 - triesLeft) + " tries.";
                    }
                    DialogResult result = MessageBox.Show("Play again?", "You win!", MessageBoxButtons.YesNo,
                                                          MessageBoxIcon.Question);

                    if (result == DialogResult.Cancel || result == DialogResult.No)
                    {
                        Application.Exit();
                    }
                    else if (result == DialogResult.Yes)
                    {
                        frmGameLevel gameLevel = new frmGameLevel();
                        gameLevel.ShowDialog();
                        newGame();
                    }
                }

                if (triesLeft <= 0)
                {
                    DialogResult result = MessageBox.Show("You ran out of tries. The number was " + theNumber
                                                          + "\nPlay again?", "You lose!", MessageBoxButtons.YesNo,
                                                          MessageBoxIcon.Exclamation);

                    if (result == DialogResult.Cancel || result == DialogResult.No)
                    {
                        Application.Exit();
                    }
                    else if (result == DialogResult.Yes)
                    {
                        frmGameLevel gameLevel = new frmGameLevel();
                        gameLevel.ShowDialog();
                        newGame();
                    }
                }
            }
            catch (Exception e)
            {
                lblOutput.Text = "Enter a whole number in the range listed above.";
            }
            finally
            {
                txtGuess.Focus();
                txtGuess.SelectAll();
            }
        }