Jump to content
  • Advertisement
Sign in to follow this  
vanidosa27

[.net] Guessing Game using Random.Next in .NET

This topic is 4482 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need to create a guessing game that generates a random number that the user has to guess. The user gets 10 chances to guess the number, and the computer tells the user if to guess lower or higher based on their guess and the actual number. If the user guess correctly the application should display a "congratualtions!" message. If the user does not guess the number within the amount of tries, the application should display the random number. Here is what I have so far... Private Sub uiGuessButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uiGuessButton.Click Dim number As Integer Dim guess As Integer Dim randomGenerator As New Random 'generator Dim count As Integer ' counter Try 'get the number guessed guess = Convert.ToInt32(Me.uiGuessTextBox.Text) 'verify that the user entered a number Do While guess <> number number = randomGenerator.Next(1, 100) Do While count <= 10 'update counter count = count + 1 Loop Loop If guess > number Then 'display the guessed number in the label Me.uiNumberYouGuessed.Text = Convert.ToString(guess) 'display the guess needs to be lower Me.UiDisplayResultsLabel.Text = Convert.ToString("Please guess a lower number.") ElseIf guess < number Then 'display the guessed number in the label Me.uiNumberYouGuessed.Text = Convert.ToString(guess) 'display the guess needs to be higher Me.UiDisplayResultsLabel.Text = Convert.ToString("Please guess a higher number.") ElseIf guess = number Then 'display the guessed number in the label Me.uiNumberYouGuessed.Text = Convert.ToString(guess) 'tell the user they guessed correctly Me.UiDisplayResultsLabel.Text = Convert.ToString("Congratulations!") Else 'display try again Me.UiDisplayResultsLabel.Text = Convert.ToString("Try again.") End If 'after 10 tries, display the random number Me.UiDisplayResultsLabel.Text = Convert.ToString("The number was " & number & ".") Catch exformat As FormatException MessageBox.Show("The number must be numeric.", "Guessing Game", _ MessageBoxButtons.OK, MessageBoxIcon.Information) Catch ex As Exception MessageBox.Show(ex.Message, "Guessing Game", _ MessageBoxButtons.OK, MessageBoxIcon.Information) End Try Something is not in the right order her, because my code is giving me the answer after the first guess...

Share this post


Link to post
Share on other sites
Advertisement
I don't know if this is homework or not, so I'll just give a hint.

Do While count <= 10
'update counter
count = count + 1
Loop

The problem lies with that code.

Share this post


Link to post
Share on other sites
Yes, this is homework.
Does that Do while loop not need to be nested? Or do I have it in the wrong place and that is why I am terminating the program too soon? Thanks!

Share this post


Link to post
Share on other sites
if you have a loop inside another loop, the inside loop has to exit before the outer loop continues.

Share this post


Link to post
Share on other sites
OH YEAH! I remember learning that! LOL! That would be the inner loop contined within the outer loop, which must be true for the loops to be nested and to work correctly.

Hmmmmmm...
I don't need a nested loop do I?

Share this post


Link to post
Share on other sites
Hey vanidosa27,

There are a lot of logic errors in you code which makes it behave other than what you want it to do. Since it is a homework, I will throw you hints so you have something to think about.

- Notice that the number to guess is recalculated every time the button is clicked, so is the counter.

- "Do While guess <> number" at this point number was only declared... so it's 0!

- Think what Nypyren said

- Your condition " If guess > number ", do you see that it will always be executed only once?

- Notice that the lines

Else
'display try again
Me.UiDisplayResultsLabel.Text = Convert.ToString("Try again.")
End If

will never be executed because you already have smaller than, larger than, or equal.

Share this post


Link to post
Share on other sites
I think you need to rethink your code.

Try writing the program down logically on paper and the program will stare you right in the face. Now just a little hint I wouldn't write the entire logic of the game in a button click handler. Good luck



Share this post


Link to post
Share on other sites
- Notice that the number to guess is recalculated every time the button is clicked, so is the number of tries.

I can't seem to figure this one out

- "Do While guess <> number" at this point number was only declared... so it's 0!

Okay, so I need to initialize my number

- Think what Nypyren said

- Your condition " If guess > number ", do you see that it will always be executed only once?

No, I don't see this either

- Notice that the lines

Else
'display try again
Me.UiDisplayResultsLabel.Text = Convert.ToString("Try again.")
End If

I think I understand this one, I need to take out the =, or move it to the else statement?


Share this post


Link to post
Share on other sites
To Adam, I asked my teacher if I should use a Call method and she said that a user-defined sub procedure should only be used when two or more objects needs to call it?

Share this post


Link to post
Share on other sites
Pseudocode

1. declare variables
2. Generate a random number
3. convert the guess to and integer
4. Compare the guess with the random number
5. count the guess
6. tell the user if the guess is higher or lower
7. end after 10 guesses or correct answer

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!