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

Started by
33 comments, last by Machaira 18 years, 1 month ago
I don't know if you quite got what I was saying. Your code looks as though the user only gets one guess because all your logic is inside the click handler.

You could always store the number of guesses made in the main form and then you could check the number of guesses when you enter the click handler and if it's less than 10 you can then check whether the number is greater, less, or equal and display the appropriate message.

I hope this helps you out a bit more.



Advertisement
Quote:Original post by Adam Hamilton
I don't know if you quite got what I was saying. Your code looks as though the user only gets one guess because all your logic is inside the click handler.

You could always store the number of guesses made in the main form and then you could check the number of guesses when you enter the click handler and if it's less than 10 you can then check whether the number is greater, less, or equal and display the appropriate message.

I hope this helps you out a bit more.


Can you show me an example of storing something in the main form and then checking?
hehe, that's not pseudocode, it's a logic execution. Pseudocode is when you use the syntax of the programming language mixed with english description.

Did you learn about global variables?

Also, I would suggest to print the value of you variables. For example, in the code that you posted, print the value of number, guess and count after they are declared. Then print them again after guess is initiallized. Then again after the second loop. Then again after the if condition. This is just to let you know what is going on with you program, it will help you spot the errors.
Quote:Original post by Darkneon
hehe, that's not pseudocode, it's a logic execution. Pseudocode is when you use the syntax of the programming language mixed with english description.

Did you learn about global variables?

Also, I would suggest to print the value of you variables. For example, in the code that you posted, print the value of number, guess and count after they are declared. Then print them again after guess is initiallized. Then again after the second loop. Then again after the if condition. This is just to let you know what is going on with you program, it will help you spot the errors.


I am not to good at writing pseudocode. I have not learned global variables yet...
I'm trying here...
I'm not that good at Visual Basic I'm just learning myself I come from a C++ background

Maybe someone with more Visual Basic experience can elaborate on what I'm trying to say but I'll give it a shot


Somewhere in your Form1 class or whatever is the main form declare a variable named guesses or something


Maybe you could put these just before your button click handler

' Correct me if I'm wrong with the Class keyword

Class Form1

Dim guesses As Integer ' Number of guesses the user has made
Dim number As Integer ' The number the computer is thinking of

Private Sub uiGuessButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uiGuessButton.Click

.
.
.

End Sub


End Class

Now in your Form1_load handler do this (double click the main form in your Windows Forms Designer)

----------------------------------
Dim rng as New Random

number = rng.Next(1, 100)

' Set the number of guesses to 0
guesses = 0

-----------------------------------


Now in your button click handler do this

-------------------------------------
dim guess as integer

If guesses < 10 then

guess = Convert.ToInt32(Me.uiGuessTextBox.Text)

if guess < number then
'Do message less than
end if

if guess > number then
'Do message greater than
end if

if guess = number then
'Do message correct
end if


End If


I have to get back to work now but I'll check in later

I hope this somehow helps you out.
Hmmm did your teacher specifically asked to use textboxes and a button? Did she teach how to use the InputBox? Unless someone can correct me, what you try to accomplish is impossible without global variables. I guess static variables could be used, but if you don't know about global variables I doubt you know about static variables.

I think you should review your program design and use a InputBox instead of a button. If your teach asked to use a button, then how to do this without global variables I don't know. You would need to list what you have learned so far maybe we could pin-point on something.
In my last post I forgot to increment the guesses counter

Just after the line

If guesses < 10 Then

' Add increment here

guesses = guesses + 1

.
.
.
.

End If
Quote:Original post by Darkneon
Hmmm did your teacher specifically asked to use textboxes and a button? Did she teach how to use the InputBox? Unless someone can correct me, what you try to accomplish is impossible without global variables. I guess static variables could be used, but if you don't know about global variables I doubt you know about static variables.

I think you should review your program design and use a InputBox instead of a button. If your teach asked to use a button, then how to do this without global variables I don't know. You would need to list what you have learned so far maybe we could pin-point on something.


Yes, I learned how to use the input box, and I learned about static varibles and had to write a code that took the amount owed and the amount paid and returned the change and then broke down the change into dollars, quarters, nickels, and pennies. So I needed my variables to be static in that problem (I needed them to retain their value, even when the procedure in which they were declared ended).
My professor just wants us to utilize all the code that we have learned in preceding chapters. I have coded all the easy stuff, like the exit button, the Textbox.SelectAll(), the Clearing the screen, and the Closing Event procedure.

When we did the inputbox lesson, it only returned a string to be assigned in a label on the form...

Would I put it in the Load event procedure?...

I think something like:
CONST PROMPT As string = "Enter your guess:"
Const TITLE As String = "Guessing Game"
'get the users guess and assign it to the module-level guess variable
guess = INputBox(PROMPT, TITLE)

Yes, if you decide to use the InputBox, you can put you code in Form_Load. In a nutshell, your program will look something like that.

Declare variables
Initialize variables

Do While guesses < MaxNumberOfAllowedGuesses
'Program Logic
Loop

'Final output

Can I just skip the Do While and use a Case Selection?

This topic is closed to new replies.

Advertisement