Alphabet Teaching Game
Here is a game I wrote lazily some time ago in a poor style. However, it was very simple but effective.
Sub GuessMyCapitalLetter()
'Educational Game!
Dim strLetter As String
Dim intRnd As Integer, intTries As Integer
Randomize
StartHere:
intTries = 0
intRnd = Int(Rnd * 26) + 65
BackHere:
strLetter = InputBox("Enter your Capital Letter guess", "Capital Letter Guessing Game")
If Asc(strLetter) < 65 Or Asc(strLetter) > 90 Then
MsgBox strLetter & " is not a Capital letter!", vbOKOnly, "Capital Letter Guessing Game"
GoTo BackHere
End If
intTries = intTries + 1
If Asc(strLetter) < intRnd Then
MsgBox strLetter & " is too low" & vbCrLf & vbCrLf & "Tries: " _
& intTries, vbOKOnly, "Capital Letter Guessing Game"
GoTo BackHere
ElseIf Asc(strLetter) > intRnd Then
MsgBox strLetter & " is too high" & vbCrLf & vbCrLf & "Tries: " _
& intTries, vbOKOnly, "Capital Letter Guessing Game"
GoTo BackHere
Else
MsgBox strLetter & " is correct" & vbCrLf & vbCrLf & "Tries: " _
& intTries, vbOKOnly, "Capital Letter Guessing Game"
End If
If MsgBox("Play again?", vbQuestion + vbYesNo, _
"Capital Letter Guessing Game") = vbYes Then
GoTo StartHere
Else
MsgBox "Good Bye!", vbOKOnly, "Capital Letter Guessing Game"
End If
End Sub
This style of programming of using GoTo statements is frowned upon but my first 32K computer did not have the Loop statement.
So now I've brought it up to scratch but it's required a few extra statements!
I also found that the InputBox could crash the code and there was no way to stop in the middle of the game so that is corrected as well.
Sub GuessMyCapitalLetter()
'Educational Game!
Dim strLetter As String
Dim intRnd As Integer, intTries As Integer
Dim blnOK As Boolean, blnAgain As Boolean
Randomize
Do
intTries = 0
intRnd = Int(Rnd * 26) + 65
Do
blnAgain = False
Do
blnOK = True
strLetter = InputBox("Enter your Capital Letter guess", "Capital Letter Guessing Game")
If strLetter = "" Then
If MsgBox("I need a letter. Click Yes if you want to stop, or No to continue", _
vbYesNo, "Stop the Game?") = vbYes Then Exit Sub
blnOK = False
ElseIf Asc(strLetter) < 65 Or Asc(strLetter) > 90 Then
MsgBox strLetter & " is not a Capital letter!", vbOKOnly, "Capital Letter Guessing Game"
blnOK = False
End If
Loop Until blnOK = True
intTries = intTries + 1
If Asc(strLetter) < intRnd Then
MsgBox strLetter & " is too low" & vbCrLf & vbCrLf & "Tries: " _
& intTries, vbOKOnly, "Capital Letter Guessing Game"
blnAgain = True
ElseIf Asc(strLetter) > intRnd Then
MsgBox strLetter & " is too high" & vbCrLf & vbCrLf & "Tries: " _
& intTries, vbOKOnly, "Capital Letter Guessing Game"
blnAgain = True
Else
MsgBox strLetter & " is correct" & vbCrLf & vbCrLf & "Tries: " _
& intTries, vbOKOnly, "Capital Letter Guessing Game"
End If
Loop Until blnAgain = False
Loop Until MsgBox("Play again?", vbQuestion + vbYesNo, _
"Capital Letter Guessing Game") = vbNo
MsgBox "Good Bye!", vbOKOnly, "Capital Letter Guessing Game"
End Sub
The question is: should one do do the Do statement?