dimanche 26 février 2017

VBScript Number Guessing Game

I am at a complete loss here. I have been beating my head against a wall all weekend and I cannot for the life of me figure out how to make my program work. This is a college class assignment and it is my first programming assignment ever, so this is a real challenge for me. I have Googled until my fingers were bleeding (not really) and I have only come across marginally helpful information. Here is my assignment (I am deeply sorry if I get the formatting wrong):

'Initialization Section
Option Explicit
Const cGreetingMsg = "Pick a number between 1 - 100"
Dim intUserNumber, intRandomNo, strOkToEnd, intNoGuesses
intNoGuesses = 0

'Main Processing Section
'Generate a random number
Randomize
intRandomNo = FormatNumber(Int((100 * Rnd) + 1))

'Loop until either the user guesses correctly or the user clicks on  Cancel
Do Until strOkToEnd = "yes"

  'Prompt user to pick a number
  intUserNumber = InputBox("Type your guess:",cGreetingMsg)
  intNoGuesses = intNoGuesses + 1

  'See if the user provided an answer
  If Len(intUserNumber) <> 0 Then

'Make sure that the player typed a number
If IsNumeric(intUserNumber) = True Then

  'Test to see if the user's guess was correct
  If FormatNumber(intUserNumber) = intRandomNo Then
    MsgBox "Congratulations! You guessed it. The number was " & _
      intUserNumber & "." & vbCrLf & vbCrLf & "You guessed it " & _
      "in " & intNoGuesses & " guesses.", ,cGreetingMsg
    strOkToEnd = "yes"
  End If

  'Test to see if the user's guess was too low
  'Assignment 3: Add another If/ElseIf/Else Condition to check for if the user is less than 
  '80,60,40,20,10 or farther from the correct guess, but still too low        
  If FormatNumber(intUserNumber) < intRandomNo Then 

  strOkToEnd = "no"
  End If

  'Test to see if the user's guess was too high
  'Assignment 3: Add another If/ElseIf/Else Condition to check for if the user is more than 
  '80,60,40,20,10 or closer to the correct guess, but still too high  
  If FormatNumber(intUserNumber) > intRandomNo Then

  strOkToEnd = "no"
  End If      

Else
  MsgBox "Sorry. You did not enter a number. Try again.", , cGreetingMsg
End If

  Else
    MsgBox "You either failed to type a value or you clicked on Cancel. " & _
      "Please play again soon!", , cGreetingMsg
    strOkToEnd = "yes"
  End If

Loop

This is blowing my mind. The only thing that has been remotely helpful has been this link: VBScript Guess a number and with that being said, I have no earthly idea how to even implement that code into my own. It looks like it would work, but whenever I try it VBScript throws tons of "expected end of statement" errors. My own code is near worthless in the effort, but here it is:

'Initialization Section

Option Explicit

Const cGreetingMsg = "Pick a number between 1 - 100"

Dim intUserNumber, intRandomNo, strOkToEnd, intNoGuesses

intNoGuesses = 0

'Main Processing Section

'Generate a random number
Randomize
intRandomNo = FormatNumber(Int((100 * Rnd) + 1))

'Loop until either the user guesses correctly or the user clicks on Cancel
Do Until strOkToEnd = "yes"

'Prompt user to pick a number
intUserNumber = InputBox("Type your guess:",cGreetingMsg)
intNoGuesses = intNoGuesses + 1

'See if the user provided an answer
If Len(intUserNumber) <> 0 Then
'Make sure that the player typed a number
If IsNumeric(intUserNumber) = True Then

'Test to see if the user's guess was correct
If FormatNumber(intUserNumber) = intRandomNo Then
MsgBox "Congratulations! You guessed it. The number was " & _
intUserNumber & "." & vbCrLf & vbCrLf & "You guessed it " & _
 "in " & intNoGuesses & " guesses.", ,cGreetingMsg
strOkToEnd = "yes"
End If

'Test to see if the user's guess was too low
If FormatNumber(intUserNumber) < intRandomNo - 80 Then
    MsgBox "Your guess was too low by 80. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) < intRandomNo - 60 Then
    MsgBox "Your guess was too low by 60. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) < intRandomNo - 40 Then
    MsgBox "Your guess was too low by 40. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) < intRandomNo - 20 Then
    MsgBox "Your guess was too low by 20. Try again", ,cGreetingMsg
Elseif FormatNumber(intUserNumber) < intRandomNo - 10 Then
    MsgBox "Your guess was too low by 10. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) > intRandomNo Then
    MsgBox "Your guess was still too low. Try again, please", ,cGreetingMsg
    strOkToEnd = "no"
End If

'Test to see if the user's guess was too high
If FormatNumber(intUserNumber) > intRandomNo - 80 Then
    MsgBox "Your guess was too high by 80. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) > intRandomNo + 60 Then
    MsgBox "Your guess was too high by 60. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) > intRandomNo + 40 Then
    MsgBox "Your guess was too high by 40. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) > intRandomNo + 20 Then
    MsgBox "Your guess was too high by 20. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) > intRandomNo + 10 Then
    MsgBox "Your guess was too high by 10. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) < intRandomNo Then
    MsgBox "Your guess was still too low. Try again, please", ,cGreetingMsg
    strOkToEnd = "no"
End If

Else
    MsgBox "Sorry. You did not enter a number. Try again.", , cGreetingMsg
End If

Else
    MsgBox "You either failed to type a value or you clicked on Cancel. " & _
    "Please play again soon!", , cGreetingMsg
    strOkToEnd = "yes"
End If

Loop

Any and all help is greatly appreciated with this. Thank you.

Aucun commentaire:

Enregistrer un commentaire