PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Stuck on code for number guessing game-VBForums
Results 1 to 3 of 3

Thread: Stuck on code for number guessing game

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2007
    Posts
    2

    Stuck on code for number guessing game

    Ok, I am trying to make a number guessing game. The program I have to make must check the number entered against the range of numbers of the game (1 through 10). The user has five tries to guess the number, and I can't use loops. If the user puts in a number that is too high, the computer needs to spit out a message saying "lower" and vice versa by using a function. I must also divide the source code into procedures/functions. So here is what I have so far. Here are my encountered problems:

    1. Whenever I run the program to test a number the number the computer always chooses is 0. I don't even want 0 as a choice, I want it to pick a number between 1 and 10. So why does it pick 0 in the first place? Why is the number the computer picks always the same number (0)? Can you tell me what code I need to put in there in order to make it pick a different random number between 1 and 10?

    2. I want the user to only have 5 guesses. If they get the right number in under 5 guesses they win, if they use up 5 guesses and they don't get the number, I want a message box to poip up saying "Sorry, you are out of guesses" and I want the pop up box to have an exit command. Can you tell me what code I have to put int here to make that happen?

    I would appreciate any help anyone could offer, I am getting tired of this project and desperate to get it done.

    Thanks,

    Siobhan

    Code:
    Private Sub cmdGuess_Click()
        Dim Response As Integer
    
        Select Case Val(txtNumber.Text)
            Case Is = MyNumber
                nResponse = MsgBox("You Won!!!")
            Case Is < MyNumber
                MsgBox "Pick a HIGHER number"
            Case Else
                MsgBox "Pick a LOWER number"
        End Select
    End Sub
    
    Private Sub cmdQuit_Click()
    End
    End Sub
    
    Private Sub Form_Load()
    
    Randomize
        MyNumber = Int(10 * Rnd) + 1
        usercount = 0
    End Sub
    Last edited by x_siobhan_x; Oct 31st, 2007 at 11:49 PM.

  2. #2
    Addicted Member
    Join Date
    Mar 2005
    Posts
    157

    Re: Help creating/debuggin code for a number guessing game

    First of all why don't you use a MsgBox to display the message instead of a textbox? Also

    vb Code:
    1. If userguess > compnumber Then
    2. txtMessage.Text = "Lower"
    3.  
    4. ElseIf userguess < compnumber Then
    5. txtMessage.Text = "Higher"
    6.  
    7. ElseIf txtMessage.Text = "CORRECT" Then
    8. MsgBox "You Won!!!"
    9.  
    10. End If

    Hrm.. "elseif txtmessage.text = "CORRECT" then", you haven't specified nowhere that txtmessage.text changes to correct. Also, compnumber you need to round it so it can be a whole number without the decimal. You have also specified variables inside a sub that should be defined under option explicit. So your code should really look like this:

    vb Code:
    1. Option Explicit
    2. Dim userguess As Integer
    3. Dim compnumber As Integer
    4. Dim usercount As Integer
    5.  
    6. Private Sub cmdGuess_Click()
    7. Dim strYesNo As String
    8. userguess = Val(txtGuess.Text)
    9.  
    10. If userguess > compnumber Then
    11.     txtmessage.Text = "Lower"
    12.     'or
    13.     'MsgBox "Lower", vbokonly, "Title here"
    14. ElseIf userguess < compnumber Then
    15.     txtmessage.Text = "Higher"
    16.     'or
    17.     'MsgBox "Higher", vbokonly, "Title here"
    18. ElseIf userguess = compnumber Then
    19.     MsgBox "You Won!!!"
    20.     strYesNo = MsgBox("Do you want to play again?", vbYesNo, "Game")
    21.    
    22.     If strYesNo = vbYes Then
    23.         lstGuess.Clear
    24.         txtGuess.Text = ""
    25.         txtGuess.SetFocus
    26.         usercount = 0
    27.         txtCount.Text = usercount
    28.         Randomize
    29.         compnumber = Round((11 * Rnd), 0)
    30.         Exit Sub
    31.     ElseIf strYesNo = vbNo Then
    32.         'Either end or do something something else
    33.     End If
    34. End If
    35.  
    36. lstGuess.AddItem txtGuess.Text
    37. txtGuess.Text = ""
    38. txtGuess.SetFocus
    39. usercount = usercount + 1
    40. txtCount.Text = usercount
    41. End Sub
    42.  
    43. Private Sub Form_Load()
    44. Randomize
    45. compnumber = Round((11 * Rnd), 0) 'Round the number to the nearest whole number
    46. usercount = 0
    47. End Sub
    48.  
    49. Private Sub txtCount_Change()
    50. If usercount = 5 Then
    51.     txtCount.Text = "5"
    52.     MsgBox "Game Over"
    53.     cmdGuess.Enabled = False
    54. End If
    55. End Sub
    56.  
    57. Sub cmdExit_Click()
    58. End
    59. End Sub

  3. #3
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    Re: Stuck on code for number guessing game

    Or use recursion.
    vb Code:
    1. Private Sub Form_Load()
    2.     GuessTheNumber RandLng(0, 10), 5
    3. End Sub
    4.  
    5.  
    6. Public Function GuessTheNumber(num As Long, tries As Long) As Boolean
    7. Dim g As Long
    8.    
    9.     If tries <= 0 Then
    10.         MsgBox "Out of tries!"
    11.         GuessTheNumber = False
    12.     Else
    13.         g = InputBox("Guess...", , 0)
    14.        
    15.         If g = num Then
    16.             MsgBox "You got it!"
    17.             GuessTheNumber = True
    18.         Else
    19.             If g < num Then
    20.                 MsgBox "Higher!"
    21.             Else 'guess > num
    22.                 MsgBox "Lower!"
    23.             End If
    24.             GuessTheNumber = GuessTheNumber(num, tries - 1)
    25.         End If
    26.     End If
    27. End Function
    28.  
    29. Public Function RandLng(Optional ByVal Min As Long = 0, Optional ByVal Max As Long = 1) As Long
    30.     RandLng = Min + Fix(Rnd * (Max - Min + 1))
    31. End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width