Skip to content

Instantly share code, notes, and snippets.

@simply-coded
Last active October 26, 2019 08:25
Show Gist options
  • Save simply-coded/1705627cd6b33a7d19eaa9119886031f to your computer and use it in GitHub Desktop.
Save simply-coded/1705627cd6b33a7d19eaa9119886031f to your computer and use it in GitHub Desktop.
Game that has you guess a computer generated number between 0-100. The closer or farther a way you get will let you know if you're getting warmer or colder. You have 10 tries. Good luck.
'***********************
'Name: Guessing Game
'Author: Jeremy England
'Company: SimplyCoded
'Version: rev.001
'Date: 05/17/2014
'***********************
Option Explicit
Dim status, guess, range, guesses, diff, change
'create random number from 0-100
Randomize
Dim answer
answer = Round(100*Rnd)
'loop 10 times
Dim count
For count = 1 To 10 Step 1
'ask for number
guess = Inputbox("Status: " & status & _
"; " & change & String(2,vbLf) & "Guesses: " & _
guesses & String(3,vbLf) & String(3,vbTab) & _
"Tries: " & count & " out of 10", "Guess a Number between 0 and 100!")
'keep track of guesses
guesses = guesses & guess & ", "
'if cancel button clicked
If IsEmpty(guess) Then
MsgBox "Canceled",vbCritical
Exit For
'if valid value
ElseIf IsNumeric(guess) Then
If guess <= 100 And guess >= 0 Then
range = Abs(answer-guess)
'compare with previous answer
If count >= 2 Then
If Abs(answer-guess) < Abs(answer-diff) Then
change = "getting hotter"
ElseIf Abs(answer-guess) > Abs(answer-diff) Then
change = "getting colder"
End If
End If
'save this rounds guess for our comparison
diff=guess
'check how close they are
If range = 0 Then
MsgBox "You got it",vbInformation, "Answer is "&answer
Exit For
ElseIf range >= 1 And range <= 5 Then
status = "Melted"
ElseIf range > 5 And range <= 15 Then
status = "Melting"
ElseIf range > 15 And range <= 25 Then
status = "Blazing"
ElseIf range > 25 And range <= 35 Then
status = "Hotter"
ElseIf range > 35 And range <= 45 Then
status = "Hot"
ElseIf range > 45 And range <= 55 Then
status = "Medium"
ElseIf range > 55 And range <= 65 Then
status = "Cold"
ElseIf range > 65 And range <= 75 Then
status = "Colder"
ElseIf range > 75 And range <= 85 Then
status = "Icy"
ElseIf range > 85 And range <= 95 Then
status = "Freezing"
ElseIf range > 95 And range <= 100 Then
status = "Frozen"
End If
Else
status = "That number was not within the required range! Please try again."
End If
Else
status = "That was not a number value! Please try again."
End If
If count = 10 then
MsgBox "You ran out of tries.", vbCritical
MsgBox "Correct number was: " & answer, vbInformation
Exit For
End If
Next
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment