jeudi 20 octobre 2016

Guessing Game Random Number using C# Winform

I'm new to coding, but I'm open to suggestions, but please keep the recommendations simple to understand. This is my criteria -> The application will automatically generate a random number between 0 and 100 and keep this as a "secret number". The user can guess up to 10 times and the application will keep record each guess in an array. After the first guess, the application displays "Good guess.. keep going." If the next guess is closer to the secret number than the previous guess, the application display "Warmer" to the user. If the next guess is farther away from the secret number than the previous guess, the application will display "Colder" to the user. If the next guess is the same distance from the secret number, then the application displays "the same" to the user. When the user reaches 10 guesses, display the message "No more guesses." If the user clicks the restart button, a new random number is generated and the number of guesses made by the user is set back to zero.

public partial class Form1 : Form
{
    // calling the random number constructor
    Random rand = new Random();
    // declaring secret number
    int secretNum;
    // allowed 10 guesses in the array
    int[] guesses = new int[10];

    int guessCount = 0;
    int DistancePrevGuess;
    int DistanceThisGuess;
    int DistCurGuess;
    int DistPrevGuess;


    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // take out when I'm done!!!

        // generate our secret number
        secretNum = rand.Next(101);
        // to display the secret number
        MessageBox.Show("Secret number is " + secretNum + " ..shhh");
        MessageBox.Show("Try to guess the number between 1 to 100");
    }

    private void btnGuess_Click(object sender, EventArgs e)
    {
        int guess = Convert.ToInt32(txtNumBox.Text);

        if (guess > secretNum)
        // numbers will be from 1-100
        // 1. Parse user entry
        if (int.TryParse(txtNumBox.Text, out guess))
        {

            //    2. Check if user still has guesses left
            //       2a. add the guess to the array
            guesses[guessCount] = guess;
            //        2b. increase the number of guesses
            guessCount++;
            //        2c. Call Guessing Messenger
 //  I don't know what to call my method
            GuessingMessenger(result1);
            //      ELSE - Error Process "Out of guesses"
            //  1. ELSE - Error Processing "Invalid Input"
        }
        else
        {

        }


    }

    private void btnStartOver_Click(object sender, EventArgs e)
    {
        // generate our secret number
        secretNum = rand.Next(101);
        // to display the secret number
        MessageBox.Show("Secret number is " + secretNum + " ..shhh");
    }
    private void GuessingMessenger(int[] thisGuessArray, int count)
    {
        // if guess  = secretNum
        if (count == secretNum)
        {
            //      return "You guessed it! message - Reset to play again
            thisGuessArray.ToString();                
            // MessageBox.Show("You guess it! Reset to play again");
        }
        // else if (guess #1?)
        else if (count == 1)
        {
            //      return "Keep guessing!"
            // MessageBox.Show("Keep Guessing");
            count.ToString("Keep Guessing");
        }
        // else if (guess count == 10)
        else if (count == 10)
        {
            //      return "Nope & you are out of guesses and the secret number is ""
            thisGuessArray.ToString();                
            // MessageBox.Show("Nope & you're out of guess and the secret number is" + secretNum);
        }
        else
        {
            //   - did not guess secretNum & guess #2 of 9
            // MessageBox.Show("Sorry, you didn't guess the " + secretNum + guesses[guessCount - 1]);
            count.ToString("Sorry, you didn't guess the " + thisGuessArray + guesses[count - 1]);
        }

        // calculate distance of this guess
        // current guess
        DistCurGuess = Math.Abs(guesses[guessCount - 1] - secretNum);
        // calculate distance for prev guess
        // previous guess
        DistPrevGuess = Math.Abs(guesses[guessCount - 2] - secretNum);
        // if DistCurGuess > DistPrevGuess

        if (DistCurGuess > DistPrevGuess)
        {



        // if (DinstanceThisGuess < DistancePrevGuess)
        if (DistanceThisGuess < DistancePrevGuess)
        {
            //      return "Warmer"
            // MessageBox.Show("Warmer");
               // txtNumBox.Text = ("Warmer");
                DistanceThisGuess.ToString("Warmer");
        }
        // else if (DistancePrevGuess < distanceThisGuess)
        else if (DistancePrevGuess < DistanceThisGuess)
        {
            //      return "Colder"
            // MessageBox.Show("Colder");
               // txtNumBox.Text = ("Colder");
                DistanceThisGuess.ToString("Colder");
            }
        else
            //      return "Same"
           // MessageBox.Show("Same");
           // txtNumBox.Text = ("Same");
            DistancePrevGuess.ToString("Same");
        }
    //This is where I return my results, but there should be two, right?
        return guessCount;
    }
}




Aucun commentaire:

Enregistrer un commentaire