Jump to content

Write a Windows application that randomly generates a number from 0 through 100. Prompt the user to guess the number. Let the user know if the guess is too low, too high, or is the correct number. Give the user another chance to guess the number. The user keeps guessing until he or she gets it correct.
 
 
Form constructor method
Randomly pick a target number between 0 and 100
Evaluate Button Click Event Handler Method
 
If the guess text box is not blank 
Pull the guess from the text box
Add 1 to the number of guesses 
 
If the guess is less than the target 
Make the Evaluate button invisible
Change the label message to "Too Low!!"
Make the label message visible
Change the background color of the form to LightSeaGreen
Make the Try Again button visible
Else if the guess is greater than the target 
Make the Evaluate button invisible
Change the label message to "Too High!!"
Make the label message visible
Change the background color of the form to SlateBlue
Make the Try Again button visible
Else 
Display a message indicating the user guessed correctly and display the number of guesses it took.
End-If
End-If
Try Again Button Click Event handler
 
Clear the guess text box
Make the label message and Try Again buttons invisible
Make the Evaluate button visible

Link to comment
https://linustechtips.com/topic/228911-number-guessing-game-c/
Share on other sites

Link to post
Share on other sites

So this is actually a pretty simple game.

 

I'll go through the concept behind it.

  1. Generate random number inbetween a range. (Have a look around for how to generate a random integer in C#)
  2. Accept user input
  3. On submission compare the given input with the random generated number

Believe it or not if you just look up on google on how to do these steps then you'll easily find the answer with a explanation.

I'll help you out by giving you a pretty good starting point on where to look at. The Visual C# Documentation.

Link to comment
https://linustechtips.com/topic/228911-number-guessing-game-c/#findComment-3133944
Share on other sites

Link to post
Share on other sites

 int count = 0;

            int choice = Convert.ToInt32(guess);

 

 

            if (guess.Text != string.Empty)

            {

                // set counter to keep track of how many tries

                // should this be done by a loop or will it count without a loop?

                count++;

 

                //compare user input against random number

          //Can’t import the random number for comparision

                if (choice < answer)

                {

                    Evaluate.Visible = false;

                    lblMessage.Visible = true;

                    lblMessage.Text = "Too Low!";

                    Clear.Visible = true;

                    BackColor = Color.LightSeaGreen;

                }

                else if (choice > answer)

                {

                    Evaluate.Visible = false;

                    lblMessage.Visible = true;

                    lblMessage.Text = "Too High!";

                    Clear.Visible = true;

                    BackColor = Color.SlateBlue;

                }

                else

                {

                    //Display correct message along with how many times it took to get it

                    MessageBox.Show(" Eso es CORRECTO! It took you {0} tries. ", count);

                }

            }

        }

 

        private void Clear_Click(object sender, EventArgs e)

        {

            guess.Text = "";

            Evaluate.Visible = true;

            lblMessage.Visible = false;

            Clear.Visible = false;

            BackColor = Color.PowderBlue;

        }

    }

}

       

private int answer;

private int count;

 

public game()

{

  InitializeComponent();

 

  //Generate Random number between 1 and 100

  Random random= new Random();

  // no need for num1 and num2, it's just as random

  answer = random.Next(1,101);

}

       

public partial class game : Form

    {

        int answer;

        public game()

        {

        }

    }

Link to comment
https://linustechtips.com/topic/228911-number-guessing-game-c/#findComment-3140052
Share on other sites

Link to post
Share on other sites

 

 int count = 0;
            int choice = Convert.ToInt32(guess);
 
 
            if (guess.Text != string.Empty)
            {
                // set counter to keep track of how many tries
                // should this be done by a loop or will it count without a loop?
                count++;
 
                //compare user input against random number
          //Can’t import the random number for comparision
                if (choice < answer)
                {
                    Evaluate.Visible = false;
                    lblMessage.Visible = true;
                    lblMessage.Text = "Too Low!";
                    Clear.Visible = true;
                    BackColor = Color.LightSeaGreen;
                }
                else if (choice > answer)
                {
                    Evaluate.Visible = false;
                    lblMessage.Visible = true;
                    lblMessage.Text = "Too High!";
                    Clear.Visible = true;
                    BackColor = Color.SlateBlue;
                }
                else
                {
                    //Display correct message along with how many times it took to get it
                    MessageBox.Show(" Eso es CORRECTO! It took you {0} tries. ", count);
                }
            }
        }
 
        private void Clear_Click(object sender, EventArgs e)
        {
            guess.Text = "";
            Evaluate.Visible = true;
            lblMessage.Visible = false;
            Clear.Visible = false;
            BackColor = Color.PowderBlue;
        }
    }
}
       
private int answer;
private int count;
 
public game()
{
  InitializeComponent();
 
  //Generate Random number between 1 and 100
  Random random= new Random();
  // no need for num1 and num2, it's just as random
  answer = random.Next(1,101);
}
       
public partial class game : Form
    {
        int answer;
        public game()
        {
        }
    }

 

What exactly were you trying to accomplish with the OP lol? 

Link to comment
https://linustechtips.com/topic/228911-number-guessing-game-c/#findComment-3140086
Share on other sites

Link to post
Share on other sites

Here are some comments about your code. Future tip, use the code tags for the forum.

 

First, you missed part of the first piece of code. There's no class or method included so it's hard to tell which file the code is in. If it's in the same file as your constructor game(), you shouldn't be having any trouble accessing the answer variable.

 

Also you wouldn't include the type in

int count = 0;// just docount = 0;

However because you're not doing this I'm assuming it's located in another class/file. You could make the instances public which should give you access like so

public count;public answer;// Then get the value from them usinggame.countgame.answer

Some other notes about your code.

 

I'm not sure if error handling is too advanced for you, but a handy method for type conversion instead of

Convert.ToInt32(guess.Text)

Would be to use the TryParse methods like so

Integer.TryParse(guess.Text, choice)

This way if guess.Text isn't a valid integer it wont throw an exception.

 

I also like using the following method for checking if I string is empty or not empty. There are other methods like Equals and Compare for testing specific equality.

string.IsNullOrEmpty()// or!string.IsNullOrEmpty()

And I wouldn't bother converting the value before you do the empty test. Why convert the value if it's empty right? So I would rewrite the first few lines as

int choice;if (string.IsNullOrEmpty(guess.Text) || !Integer.TryParse(guess.Text, choice)){    return;}

If you're not sure what these methods do you can look them up. But basically they check to make sure you have a valid guess. If you don't, it exits the method. A benefit of exiting early is the rest of your method isn't nested in an if statement. But these are just my preferences and others may have differing opinions.

 

It looks like your choices are done right.

 

So altogether you're first piece of code should look something like

int choice;if (string.IsNullOrEmpty(guess.Text) || !Integer.TryParse(guess.Text, choice)){    // You could display an error message for an invalid guess here before you return if you want    return;}game.count++;if (choice < game.answer){    Evaluate.Visible = false;    lblMessage.Visible = true;    lblMessage.Text = "Too Low!";    Clear.Visible = true;    BackColor = Color.LightSeaGreen;}else if (choice > game.answer){    Evaluate.Visible = false;    lblMessage.Visible = true;    lblMessage.Text = "Too High!";    Clear.Visible = true;    BackColor = Color.SlateBlue;}else{    //Display correct message along with how many times it took to get it    MessageBox.Show(" Eso es CORRECTO! It took you {0} tries. ", game.count);    // You can generate this code here or in the button event that restarts the game    // But you'll want to reset the value of count, and get a new answer value    game.count = 0;    //Generate Random number between 1 and 100    Random random= new Random();    answer = random.Next(1,101);}

And the constructor code

private int answer;private int count;public game(){    InitializeComponent();    count = 0; // Initialize the value of count    //Generate Random number between 1 and 100    Random random= new Random();    // no need for num1 and num2, it's just as random    answer = random.Next(1,101);}

I hope this is somewhat easy to follow/understand and isn't too much for your current level.

Link to comment
https://linustechtips.com/topic/228911-number-guessing-game-c/#findComment-3143788
Share on other sites

Link to post
Share on other sites

Here are some comments about your code. Future tip, use the code tags for the forum.

 

First, you missed part of the first piece of code. There's no class or method included so it's hard to tell which file the code is in. If it's in the same file as your constructor game(), you shouldn't be having any trouble accessing the answer variable.

 

Also you wouldn't include the type in

int count = 0;// just docount = 0;

However because you're not doing this I'm assuming it's located in another class/file. You could make the instances public which should give you access like so

public count;public answer;// Then get the value from them usinggame.countgame.answer

Some other notes about your code.

 

I'm not sure if error handling is too advanced for you, but a handy method for type conversion instead of

Convert.ToInt32(guess.Text)

Would be to use the TryParse methods like so

Integer.TryParse(guess.Text, choice)

This way if guess.Text isn't a valid integer it wont throw an exception.

 

I also like using the following method for checking if I string is empty or not empty. There are other methods like Equals and Compare for testing specific equality.

string.IsNullOrEmpty()// or!string.IsNullOrEmpty()

And I wouldn't bother converting the value before you do the empty test. Why convert the value if it's empty right? So I would rewrite the first few lines as

int choice;if (string.IsNullOrEmpty(guess.Text) || !Integer.TryParse(guess.Text, choice)){    return;}

If you're not sure what these methods do you can look them up. But basically they check to make sure you have a valid guess. If you don't, it exits the method. A benefit of exiting early is the rest of your method isn't nested in an if statement. But these are just my preferences and others may have differing opinions.

 

It looks like your choices are done right.

 

So altogether you're first piece of code should look something like

int choice;if (string.IsNullOrEmpty(guess.Text) || !Integer.TryParse(guess.Text, choice)){    // You could display an error message for an invalid guess here before you return if you want    return;}game.count++;if (choice < game.answer){    Evaluate.Visible = false;    lblMessage.Visible = true;    lblMessage.Text = "Too Low!";    Clear.Visible = true;    BackColor = Color.LightSeaGreen;}else if (choice > game.answer){    Evaluate.Visible = false;    lblMessage.Visible = true;    lblMessage.Text = "Too High!";    Clear.Visible = true;    BackColor = Color.SlateBlue;}else{    //Display correct message along with how many times it took to get it    MessageBox.Show(" Eso es CORRECTO! It took you {0} tries. ", game.count);    // You can generate this code here or in the button event that restarts the game    // But you'll want to reset the value of count, and get a new answer value    game.count = 0;    //Generate Random number between 1 and 100    Random random= new Random();    answer = random.Next(1,101);}

And the constructor code

private int answer;private int count;public game(){    InitializeComponent();    count = 0; // Initialize the value of count    //Generate Random number between 1 and 100    Random random= new Random();    // no need for num1 and num2, it's just as random    answer = random.Next(1,101);}

I hope this is somewhat easy to follow/understand and isn't too much for your current level.

Okay I understand a little bit. However I am getting errors and then a dialog box comes up saying it cant run because of my debugging target? :/

Link to comment
https://linustechtips.com/topic/228911-number-guessing-game-c/#findComment-3148170
Share on other sites

Link to post
Share on other sites

Barnacules Nerdgasam did a tutorial on how to make a guessing game.

 

He even uploaded the Source Code to the internet, here you go

 

I'm sure that's what you're looking for, Instead of the replies being "Yes!" & "No!" you can change them to "1" & "2" and so on.

 

 

Hope this helped you out.

 

 

Gamer & PC Enthusiast


Gaming Community's TeamSpeak: ts.the-eux.uk


If I've helped you out, Click that "like" button to show me some love :)


Link to comment
https://linustechtips.com/topic/228911-number-guessing-game-c/#findComment-3154835
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×