160
Dim objWshShell, strAnswer, strCardImage, strResults, intGetRandomNumber
Set objWshShell = WScript.CreateObject(“WScript.Shell”)
strResults = “None”
‘Prompt the user to select a choice
strAnswer = InputBox(“Please type Paper, Rock, or Scissors.” & _
vbCrLf & vbCrLf & “Rules:” & vbCrLf & vbCrLf & _
“1. Guess the same thing as the computer to tie.” & vbCrLf & _
“2. Paper covers rock and wins.” & vbCrLf & _
“3. Rock break scissors and wins.” & vbCrLf & _
“4. Scissors cut paper and win.” & vbCrLf, “Let’s play a game!”)
‘Time for the computer to randomly pick a choice
Randomize
intGetRandomNumber = Round(FormatNumber(Int((3 * Rnd) + 1)))
If intGetRandomNumber = 3 then strCardImage = “rock”
If intGetRandomNumber = 2 then strCardImage = “scissors”
If intGetRandomNumber = 1 then strCardImage = “paper”
Select Case strAnswer
Case “rock”
If intGetRandomNumber = 3 Then strResults = “Tie”
If intGetRandomNumber = 2 Then strResults = “You Win”
If intGetRandomNumber = 1 Then strResults = “You Lose”
Case “scissors”
If intGetRandomNumber = 3 Then strResults = “You Lose”
If intGetRandomNumber = 2 Then strResults = “Tie”
If intGetRandomNumber = 1 Then strResults = “You Win”
Case “paper”
If intGetRandomNumber = 3 Then strResults = “You Win”
If intGetRandomNumber = 2 Then strResults = “You Lose”
If intGetRandomNumber = 1 Then strResults = “Tie”
Case Else
End If
In this example, the VBScript InputBox() function was used to collect the user’s age and
assign it to a variable called
intUserAge. An If statement then checks to see whether
intUserAge is less than 19, and if it is, the game is stopped. Another way you could write the
previous example is using the VBScript
Less Than or Equal To operator, like this:
If intUserAge <= 18 Then
161
Chapter 5 • Conditional Logic
162
Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition
If you use the Less Than or Equal To operator, this statement will not execute if the user is
18 or fewer years old. VBScript also supplies Greater Than and Greater Than or Equal To oper-
ators, allowing you to invert the logic used in the preceding example.
intUserAge = InputBox(“How old are you?”)
If intUserAge > 18 Then
MsgBox “OK. Let’s play!”
Else
MsgBox “Sorry but you are too young to play this game.”
WScript.Quit()
End If
Table 5.1 lists VBScript comparison operators.
VBScript does not impose an order or precedence on comparison operators like it does with
arithmetic operators. Instead, each comparison operation is performed in the order in
which it appears, going from left to right.
Back to the Star Trek Quiz Game
Now let’s return to where we began this chapter, by developing the Star Trek Quiz game. In
this program, you will create a VBScript that presents the player with a quiz about Star Trek.
The game presents questions, collects the player’s answers, scores the final results, assigns
Star Trek Quiz game.
‘*************************************************************************
‘Script Name: StarTrekQuiz.vbs
‘Author: Jerry Ford
‘Created: 11/17/02
‘Description: This script creates a Star Trek Quiz game.
‘*************************************************************************
‘Perform script initialization activities
Option Explicit
Setting Up Constants and Variables
The next step is to define the variables and constants used by the script.
Dim intPlayGame, strSplashImage, strAnswerOne, strAnswerTwo, strAnswerThree
Dim strAnswerFour, strAnswerFive, intNumberCorrect, strFederationRank
Dim objFsoObject
163
Chapter 5 • Conditional Logic
164
Const cTitlebarMsg = “The Star Trek Quiz Game”
‘Start the user’s score at zero
intNumberCorrect = 0
The intNumberCorrect variable is used to count the number of quiz answers the player gets
right. I set
intNumberCorrect equal to zero here to ensure that it has a value because it is
always possible that the player will miss every answer and this variable might not otherwise
get set. I’ll explain what each of these variables is used for as we go through the rest of the
script development process.
Creating a Splash Screen
Let’s create a spiffy splash screen that asks the user whether he or she wants to play the
game. As you can see, I added a graphic to spice up things a bit. Graphic development of this
type takes a little time, as well as some trial and error.
vbCrLf & vbCrLf & “Live long and prosper!”, , cTitlebarMsg
WScript.Quit()
End If
As you can see, the first statement checks to see whether the user clicked on the Yes button.
I left some room to mark the area where you will need to add the statements that actually
make up the game, in case the user does want to play. If the user clicked No, then the
VBScript displays a “thank you” message and terminates its execution using the
WScript
object’s Quit() method.
Display Quiz Questions and Collect the Player’s Answers
The next step is to add the questions that make up the game. The following questions make
up the quiz:
• What was the Science Officer’s name in the original Star Trek series?
• What Star Trek villain appeared in both the original series and a Star Trek movie?
• What was the numeric designation of Voyager’s on-board Borg?
• Name the only Star Trek character to regularly appear on two series and at least two
Star Trek movies.
• What is the last name of your favorite Captain?
The statements that display and grade the first quiz questions are as follows:
strAnswerOne = InputBox(“What was the Science Officer’s name in the “ & _
“original Star Trek series?”, cTitlebarMsg)
If LCase(strAnswerOne) = “spock” Then
intNumberCorrect = intNumberCorrect + 1
End If
165
Chapter 5 • Conditional Logic
166
First the VBScript InputBox() function displays the question. The answer typed by the user is
then assigned to a variable named
strAnswerOne. Next, an If statement is used to interrogate
“movies?”, cTitlebarMsg)
If LCase(strAnswerFour) = “worf” Then
intNumberCorrect = intNumberCorrect + 1
End If
Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition
The construction of the fifth question, shown next, merits some additional examination.
First of all, the fourth statement uses the VBScript
LCase() function to convert the player’s
answer to all lowercase. The VBScript
Instr() function then takes the answer and searches
the string
“kirkpicardsiscojanewayarcher” to see whether it can find a match. This string
contains a list of last names belonging to various Star Fleet captains.
strAnswerFive = InputBox(“What is the last name of your favorite “ & _
“Captain?”, cTitlebarMsg)
If Len(strAnswerFive) > 3 Then
If Instr(1, “kirkpicardsiscojanewayarcher”, LCase(strAnswerFive), 1) _
<> 0 Then
intNumberCorrect = intNumberCorrect + 1
End If
End If
So the InStr() function begins its search starting with the first character of the string to see
whether it can find the text string that it’s looking for (that is,
kirk, picard, janeway, sisco,
or
archer). The syntax of the Instr() function is as follows:
InStr([start, ]string1, string2[, compare])
Start
specifies the character position in the script, from left to right, where the search
should begin.
answers. What you need to do next is add logic to assign the player a rank based on the num-
ber of correctly answered questions. This can be done using a
Select Case statement, like this:
Select Case intNumberCorrect
Case 5 ‘User got all five answers right
strFederationRank = “Admiral”
Case 4 ‘User got 4 of 5 answers right
strFederationRank = “Captain”
Case 3 ‘User got 3 of 5 answers right
strFederationRank = “Commander”
Case 2 ‘User got 2 of 5 answers right
strFederationRank = “Lieutenant-Commander”
Case 1 ‘User got 1 of 5 answers right
strFederationRank = “Lieutenant”
Case 0 ‘User did not get any answers right
strFederationRank = “Ensign”
End Select
The variable intumberCorrect contains the number of answers that the player has correctly
answered. The value of this variable is then compared against six possible cases, each of
which represents a different score the player could have gotten from the game. When a
match is found, the player’s rank is assigned based on the values listed in Table 5.2.
Number of Correctly Federation Rank
Answered Questions
5 Admiral
4 Captain
3 Commander
2 Lieutenant-Commander
1 Lieutenant
0 Ensign
TABLE 5.2 DETERMINING THE P LAYER’S FEDERATION R ANK
space(20) & vbCrLf & “*” & space(35) & “*” & space(18) & _
169
Chapter 5 • Conditional Logic