6 Replies - 3434 Views - Last Post: 17 April 2012 - 06:40 AM Rate Topic: -----

#1 kcin00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-April 12

Random Number Guessing Game

Posted 15 April 2012 - 03:51 PM

Visual Basic provides a function that can generate random numbers. This function, Rnd, will return a random number, of type Single, between 0 and 1. To get truly random numbers, you must initialize the random number generator using the Randomize statement. Place this statement in the Form Load event.

Create a program that will “think” of a number between 1 and 10. In a Do While loop, use the InputBox function to ask the user to guess the number. Keep track of how many tries it takes the user to guess the number.

Tell the user how many tries it took him to guess the number. Use Select Case to display an appropriate message, based on the number of tries (see below). You may use a label on the form or MsgBox for the output.

To create a random number between 1 and 10, multiply the result of Rnd by 10 and add 1. Rnd * 10 + 1
Use the Int function to convert the result of Rnd to an integer.

Here’s help (use Option Explicit, Val function, appropriate data types and standard prefixes):

Private Sub cmdStart_click( )

‘Declare RandomNumber variable

‘Declare InputNumber variable

‘Declare Counter variable

‘Create random number between 1 and 10
RandomNumber = Int(Rnd * 10 + 1)

‘Use this line of code during testing, then remove it!
Print RandomNumber

‘Get numeric value of user’s guess and accumulate number of guesses until there’s a match
Do While InputNumber <> RandomNumber
InputNumber = Val(InputBox (“Guess my number between 1 and 10”,”Guessing Game”))
Counter = Counter + 1
Loop

‘Display output
Select Case Counter
Case 1
MsgBox (“Wow! You guessed my number in 1 try!”)
Case 2 to 3
MsgBox (“Great job! You guessed my number in “ & Counter & “ tries!”)
Case 4 to 5
.
.
.
End Select
End Sub

Every time that I press the start button to begin guessing numbers, the answer is revealed on the side. How do I stop this?


Is This A Good Question/Topic? 0
  • +

Replies To: Random Number Guessing Game

#2 Alderdragon  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 50
  • Joined: 06-November 09

Re: Random Number Guessing Game

Posted 15 April 2012 - 03:59 PM

I've never used VB, but are you using some type of control to show the answer on the side? If so, check that control's properties for a visibility property. You can toggle it on and off appropriately.
Was This Post Helpful? 0
  • +
  • -

#3 kcin00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-April 12

Re: Random Number Guessing Game

Posted 15 April 2012 - 04:10 PM

View PostAlderdragon, on 15 April 2012 - 03:59 PM, said:

I've never used VB, but are you using some type of control to show the answer on the side? If so, check that control's properties for a visibility property. You can toggle it on and off appropriately.


I appreciate the help but I am not using a control to show the answer. I have been messing around with visibility properties but just can't seem to figure it out.
Was This Post Helpful? 0
  • +
  • -

#4 kcin00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-April 12

Re: Random Number Guessing Game

Posted 15 April 2012 - 04:49 PM

Even more bizarre, when i drag the input box over the answer that is being displayed it magically disappears
Was This Post Helpful? 0
  • +
  • -

#5 kcin00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-April 12

Re: Random Number Guessing Game

Posted 15 April 2012 - 05:02 PM

Private Sub cmdStart_Click()

    'Declare RandomNumber varible
    
    'Declare InputNumber varible
    
    'Declare Counter varible
    
    'Create random number between 1 and 10
    RandomNumber = Int(Rnd * 10 + 1
    
    'Use this line of code during testing, then remove it!
    Print RandomNumber
    
    'Get numeric value of user's guess and accumulate number of guesses until there's a match
    Do While InputNumber <> RandomNumber
        InputNumber = Val(InputBox("Guess my number between 1 and 10", "Guessing Game"))
        Counter = Counter + 1
    Loop
    
    'Display Output
    Select Case Counter
        Case 1
            MsgBox ("Wow! You guessed my number in 1 try!")
        Case 2 To 3
            MsgBox ("Great job! You guessed my number in " & Counter & " tries!")
        Case 4 To 5
            MsgBox ("Good job. You guessed my number in " & Counter & " tries.")
        Case 6 To 9
            MsgBox ("Ouch! You guessed my number in " & Counter & " tries.")
        Case 10
            MsgBox ("Really? You guessed my number in " & Counter & " tries.")
    End Select
End Sub

Private Sub Form_Load()

    Dim RandomNumber As Integer
    Randomize 'Initialize random-number generator.
    'Generate random number between 1 and 10
    RandomNumber = Int((Rnd * 10) + 1)
    
    
End Sub



Can someone tell me if I'm on the right track?
Was This Post Helpful? 0
  • +
  • -

#6 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 299
  • View blog
  • Posts: 1,768
  • Joined: 26-March 09

Re: Random Number Guessing Game

Posted 15 April 2012 - 11:45 PM

You have a line that says Print RandomNumber, which is printing your answer to screen.

If you read the code you posted originally, there is even a comment above it telling you to remove it!

‘Use this line of code during testing, then remove it!
Print RandomNumber

Was This Post Helpful? 0
  • +
  • -

#7 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Random Number Guessing Game

Posted 17 April 2012 - 06:40 AM

maj beats me to it again. :) A prof that went to this much trouble to hold your hand through the process of writing your first simple program is going to feel hurt that you don't even read his stuff. If you go to the trouble of actually studying the example given, you'll find some good techniques you can use to keep yourself on track while writing programs. This Print statement is one of them: it helps you make sure that you've done your random number generation correctly.

Just so you know, the Print statement prints directly on the form. If you drag something over it, well, that thing is on top of it. That's why it disappears. it's analogous to painting a logo on your desk and putting a piece of paper on top of it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1