VB Program Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jrezak321
    New Member
    • Jul 2007
    • 4

    #1

    VB Program Help

    Hi I was wondering if anyone could help me with this homework problem of mine? I need to create a program that does this:

    Write a program that plays “guess the number” as follows: Your program chooses the number to be guessed by selecting an Integer at random in the range 1–1000. The program then displays the following text in a label:

    I have a number between 1 and 1000--can you guess my number?
    Please enter your first guess.

    A TextBox should be used to input the guess. As each guess is input, the background color should change to red or blue. Red indicates that the user is getting “warmer,” blue that the user is getting “colder.” A Label should display either “Too High” or “Too Low,” to help the user “zero-in” on the correct answer. When the user guesses the correct answer, display “Correct!” in a message box, change the form’s background color to green and disable the TextBox. Provide a Button that allows the user to play the game again. When the Button is clicked, generate a new random number, change the background to the default color and enable the TextBox.

    Anyones help would be greatly appreciated...
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    I think it would be better if you give it a try by yourself first and then ask here some questions about particular troubles you had while making your program.

    If you have no idea, I recomend you to make the whole code into the Command Button click's sub. remember you're working with an object orientated language, dim an integer to generate the number (this should be done outside the command button since you dont want to re-generate it each time the user makes a guess).

    Hope that helps (and if it doesnt, take a quick view to the VB help archive to learn the properties and methods and how to use their sintaxis).

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

      Please read the Posting Guidelines and particularly the Coursework Posting Guidelines.

      Then when you are ready post a new question in this thread.

      MODERATOR

      Comment

      • Jrezak321
        New Member
        • Jul 2007
        • 4

        #4
        The main problem I am having is setting up the number generator. I know it goes something like this.

        Dim randomobject As Random = New Random()
        Dim nrand As Integer = 0


        nrand = randomobject.ne xt(1,1000)

        but what else do I need to start?

        Comment

        • Jrezak321
          New Member
          • Jul 2007
          • 4

          #5
          Here is what I got so far... I am lost though. First off I do not know if I declared everything correctly? Also how do I get the guess to input after it is typed in the box? And how do you configure the warm/cold colors? Please help!

          [CODE=vbnet]Public Class Form1
          Dim randomobject As Random = New Random()
          Dim nrand As Integer = randomobject.Ne xt(1, 1001)

          Dim guess As Integer



          Private Sub TextBox1_TextCh anged(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles TextBox1.TextCh anged
          guess = TextBox1.Text



          End Sub

          Private Sub Label2_Click(By Val sender As System.Object, ByVal e As System.EventArg s) Handles Label2.Click
          If guess < nrand Then
          Label2.Text = ("Too Low")
          If guess > nrand Then
          Label2.Text = ("Too High")
          Else : MsgBox("Correct ")
          End If
          End If
          End Sub
          End Class[/CODE]

          Comment

          • kadghar
            Recognized Expert Top Contributor
            • Apr 2007
            • 1302

            #6
            Originally posted by Jrezak321
            The main problem I am having is setting up the number generator. I know it goes something like this.

            Dim randomobject As Random = New Random()
            Dim nrand As Integer = 0


            nrand = randomobject.ne xt(1,1000)

            but what else do I need to start?
            its easier than that:

            dim nrand as integer

            nrand = int(rnd * 1000)+1

            ... and that's it

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              Originally posted by kadghar
              nrand = int(rnd * 1000)+1
              Are you sure that syntax still works in VB.Net?

              By the way, if you have a look in the VB Articles area, we have a couple of items there on the subject of generating pseudo-random numbers. (The computer can't generate truly random numbers.)

              Comment

              • kadghar
                Recognized Expert Top Contributor
                • Apr 2007
                • 1302

                #8
                Originally posted by Killer42
                Are you sure that syntax still works in VB.Net?

                By the way, if you have a look in the VB Articles area, we have a couple of items there on the subject of generating pseudo-random numbers. (The computer can't generate truly random numbers.)
                i didnt see the class... my apologies. :(

                Comment

                • Jrezak321
                  New Member
                  • Jul 2007
                  • 4

                  #9
                  Here is what I have so far. I am stuck on getting a play again button to reset the game back to normal and how to get the game to compare the previous guess to the new guess and differenciate between warm or cold. I know i need to use the math.abs() function. Can anyone help.



                  Public Class Form1
                  Dim randomobject As Random = New Random()
                  Dim nrand As Integer = randomobject.Ne xt(1, 1001)

                  Dim guess As Integer
                  Dim previousguess As String = guess










                  Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button2.Click
                  If guess < nrand Then
                  Label3.Text = "Too Low"
                  End If


                  If guess > nrand Then
                  Label3.Text = "Too High"

                  End If

                  If guess = nrand Then
                  MsgBox("Correct !")
                  Me.BackColor = Color.Green
                  TextBox1.Enable d = False
                  End If


                  End Sub

                  Private Sub Label3_Click(By Val sender As System.Object, ByVal e As System.EventArg s) Handles Label3.Click



                  End Sub


                  Private Sub TextBox1_TextCh anged(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles TextBox1.TextCh anged
                  guess = TextBox1.Text

                  End Sub
                  End Class

                  Comment

                  Working...