It consists of 10 function calls and two loops: SetVariableDefaults ProcessScriptIniFile CollectPlayerInput For intSetCount = 1 to intNoOfPlays Do Until blnAllNumbersPicked = “True” GetR
Trang 1Developing the Logic for the Main Processing Section
The script’s main processing section controls the overall execution of the script It consists
of 10 function calls and two loops:
SetVariableDefaults()
ProcessScriptIniFile()
CollectPlayerInput()
For intSetCount = 1 to intNoOfPlays
Do Until blnAllNumbersPicked = “True”
GetRandomNumber()
ProcessRandomNumber()
DetermineIfSetIsComplete()
Loop
BuildDisplayString()
ResetVariableDefaults()
Next
DisplayFinalResults()
DisplaySplashScreen()
The first loop is controlled by a Forstatement that is responsible for making sure that the script generates the number of sets of lottery numbers specified by the player The second loop
is controlled by a Do Untiland is responsible for making sure that a full count of numbers is generated for each set (or play).
Trang 2Building the SetVariableDefaults() Function
The SetVariableDefaults()function, shown here, is responsible for establishing default val-ues for a number of variables used by the script The first two variables are Booleanand are used to determine when a full set of lottery numbers has been generated and when the player has specified a valid number of plays The second pair of variables is used to store integer data The first variable is used to keep track of the number of lottery numbers generated for each play The second variable is used to track the number of sets of lottery numbers as the script is generating them.
Function SetVariableDefaults()
blnAllNumbersPicked = “False”
blnInputValidated = “False”
intNumberCount = 0
intNoOfValidPicks = 0
End Function
Building the ProcessScriptIniFile() Function
The ProcessScriptIniFile()function, shown here, is responsible for reading in script con-figuration settings from the game’s INI file Because of the unique task assigned to this func-tion, I chose to make it completely self contained Therefore, it begins by defining its own objects and variables To make the purpose of each variable clear, I documented each one by adding comments to the right of each variable when defined as well as to key statements throughout the function.
Function ProcessScriptIniFile()
Dim FsoObject ‘Sets up a reference to the FileSystemObject
Dim OpenFile ‘Sets up a reference to the script’s INI file
Set FsoObject = WScript.CreateObject(“Scripting.FileSystemObject”)
Dim intEquals ‘Used to parse INI file data
Dim strKeyName ‘Represents a key in the script’s INI file
Dim strSourceFile ‘Specifies the name of the script’s INI file
Dim strInput ‘Represents a line in the script’s INI file
Trang 3strSourceFile = “LuckyLotteryMachine.ini” ‘Identify script’s INI file
If (FsoObject.FileExists(strSourceFile)) Then ‘Make sure INI file exists
‘Open for reading
Set OpenFile = FsoObject.OpenTextFile(strSourceFile, 1)
Do Until Mid(strInput, 1, 15) = “[GameControls]” ‘Find right section strInput = OpenFile.ReadLine ‘Read line from the INI file
Loop
‘Read until end of file reached
Do Until OpenFile.AtEndOfStream = “True”
strInput = OpenFile.ReadLine ‘Read a line from the file
If Mid(strInput, 1, 1) = “[“ Then
Exit do ‘If executed, new sections have been found
End If
If Len(strInput) <> 0 Then ‘Executes if a blank line is not found
intEquals = Instr(strInput, “=”) ‘Locate the equals character
strKeyName = Mid(strInput, 1, intEquals - 1) ‘Set key value
Select Case strKeyName ‘Match up key value to script settings Case “Greeting”
strTitleBarMsg = Mid(strInput, intEquals + 1, Len(strInput)) Case “DisplayFormat”
strDisplayType = Mid(strInput, intEquals + 1, Len(strInput)) Case “NoOfPicks”
intNoOfPicksToSelect = Cint(Mid(strInput, intEquals + 1, _ Len(strInput)))
Case “RangeOfNumbers”
intRangeOfNumbers = Cint(Mid(strInput, intEquals + 1, _ Len(strInput)))
Trang 4End Select
End If
Loop
OpenFile.Close()’Close the INI file when done reading it
Else
MsgBox “The INI file is missing Unable to execute.”
WScript.Quit()
End If
End Function
The function begins by instantiating an instance of the FileSystemObject It then specifies the location of its INI file Next, it checks to make sure that the INI file exists and then opens
it The function then reads the INI file until it finds the [GameControls]section Once found, the function begins reading the rest of the INI file The function then parses through the
key=valuepairs and assigns values to matching script variables using a Select Casestatement
Building the CollectPlayerInput() Function
The CollectPlayerInput()function is responsible for collecting and validating player input The overall execution of this function is controlled by the following Do Whileloop, which executes as long as a Booleanvariable named blnInputValidatedis not equal to True:
Function CollectPlayerInput()
Do Until blnInputValidated = “True”
intNoOfPlays = InputBox(“How many sets of numbers do “ & _
“you want?”, strTitleBarMsg)
If IsNumeric(intNoOfPlays) <> True Then
MsgBox “Sorry You must enter a numeric value Please “ & _
“try again.”, ,strTitleBarMsg
Else
Trang 5If Len(intNoOfPlays) = 0 Then
MsgBox “Sorry You must enter a numeric value Please “ & _
“try again.”, ,strTitleBarMsg
Else
If intNoOfPlays = 0 then
MsgBox “Sorry Zero is not a valid selection Please “ & _
“try again.”, ,strTitleBarMsg
Else
blnInputValidated = “True”
End If
End If
End If
Loop
End Function
Three validation tests are performed The first test uses the VBScript IsNumeric()function to ensure that the input is numeric The second test uses the Len()function to ensure that the player actually typed in input, as opposed to simply clicking on OK or Cancel The last valida-tion test checks to make sure that the player did not enter a value of zero If the input pro-vided by the player passes all three of these tests, then a value of True is assigned to
blnInputValidatedand the function finishes executing.
Building the GetRandomNumber() Function
The GetRandomNumber()function, shown here, is responsible for retrieving random numbers for the script It begins with the Randomizestatement to ensure that numbers are randomly generated Next, a random number is generated The range from which the number is cre-ated is dictcre-ated by the value assigned to intRangeOfNumber, which was previously established
by retrieving its value from the script’s INI file.
Function GetRandomNumber()
Randomize
intRandomNo = cInt(FormatNumber(Int((intRangeOfNumbers * Rnd) + 1)))
End Function
Trang 6Building the ProcessRandomNumber() Function
The ProcessRandomNumber()function, shown here, is responsible for ensuring that the same lottery number is not picked twice for a given play or set It accomplishes this by establish-ing an array named aintLotteryArray The array is configured to handle up to 11 entries, based on the assumption that this is large enough to handle any amount of lottery numbers
a given lottery game might require.
Function ProcessRandomNumber()
Select Case intRandomNo
Case aintLotteryArray(0)
Case aintLotteryArray(1)
Case aintLotteryArray(2)
Case aintLotteryArray(3)
Case aintLotteryArray(4)
Case aintLotteryArray(5)
Case aintLotteryArray(6)
Case aintLotteryArray(7)
Case aintLotteryArray(8)
Case aintLotteryArray(9)
Case aintLotteryArray(10)
Case Else
strLotteryList = strLotteryList & “ “ & intRandomNo & vbTab
intNoOfValidPicks = intNoOfValidPicks + 1
aintLotteryArray(intNumberCount) = intRandomNo
intNumberCount = intNumberCount + 1
End Select
End Function
This function begins by comparing the value of the last lottery number that was generated to the numbers stored in the array The first time through, there won’t be any lottery numbers stored in the array yet As a result, the lottery number is stored as the first entry in the array Also, the lottery number is added to a string that is stored in a variable named strLotteryList, which is used elsewhere in the script Finally, the total number of valid lottery numbers is tracked by adding 1to intNoOfValidPickseach time a unique lottery number is generated.
Trang 7Each time this function is called, it checks to see whether the most recently generated random number matches any of the numbers already stored in the array If it does, nothing happens; otherwise, that number is added to the array.
Building the DetermineIfSetIsComplete() Function
The DetermineIfSetIsComplete() function, shown here, compares the value stored in
intNoOfValidPicksto the value stored in intNoOfPicksToSelectto determine whether a com-plete set of lottery numbers has been generated If a comcom-plete set has been generated, then
DetermineIfSetIsComplete() sets the value assigned to blnAllNumbersPicked equal to True Otherwise, the value assigned to this variable remains set equal to False.
Function DetermineIfSetIsComplete
If intNoOfValidPicks = intNoOfPicksToSelect Then
blnAllNumbersPicked = “True”
End If
End Function
Building the BuildDisplayString() Function
The BuildDisplayString()function, shown here, takes the string stored in the strLotteryList
variable (which is created by the ProcessRandomNumber()function) and uses it to build a larger string made up of all the sets of lottery numbers generated by the game This string is later used to display the game’s result to the player To make the displayed output more attractive, this function uses the vbTabconstant to organize output into a multi-column format.
Function BuildDisplayString()
strLotteryList = intSetCount & “)” & vbTab & strLotteryList
strDisplayString = strDisplayString & strLotteryList & _
vbCrLf & vbCrLf & vbCrLf
End Function
Building the ResetVariableDefaults() Function
The ResetVariableDefaults()function, shown here, is used to reset variable values back to their initial default settings after a full set of lottery numbers has been generated This readies the script to begin generating additional sets of numbers
Trang 8Function ResetVariableDefaults()
blnAllNumbersPicked = “False”
intNoOfValidPicks = 0
intNumberCount = 0
strLotteryList = “”
End Function
Building the DisplayFinalResults() Function
The DisplayFinalResults()function, shown here, is responsible for displaying all the sets of lottery numbers that are generated It displays this information in one of two formats based
on the value assigned to strDisplayType, which is a variable whose value was set earlier in the script by retrieving its value from the script’s INI file If strDisplayTypeis equal to Full, then the function displays information regarding the number of lottery numbers that was generated per set as well as the total number of sets that were created, followed by the num-bers that made up each set However, if the value assigned to strDisplayTypeis equal to any-thing other than Full, then only the sets of lottery numbers are displayed.
Function DisplayFinalResults()
If strDisplayType = “Full” Then
MsgBox vbCrLf & _
“L U C K Y L O T T E R Y N U M B E R P I C K E R” & _
vbCrLf & vbCrLf & _
“——————————————————————————-” & _
“——————————————-” & vbCrLf & vbCrLf & _
“Number of plays: “ & intNoOfPlays & vbCrLf &vbCrLf & _
“Number of picks per play: “ & intNoOfPicksToSelect & _
vbCrLf & vbCrLf & _
“——————————————————————————-” & _
“——————————————-” & vbCrLf & vbCrLf & vbCrLf & _
“Your lottery numbers are: “ & vbCrLf & vbCrLf & vbCrLf & _
strDisplayString, , strTitleBarMsg
Else
Trang 9MsgBox vbCrLf & _
“L U C K Y L O T T E R Y N U M B E R P I C K E R” & _
vbCrLf & vbCrLf & _
“——————————————————————————-” & _
“——————————————-” & vbCrLf & vbCrLf & _
“Your lottery numbers are: “ & vbCrLf & vbCrLf & vbCrLf & _
strDisplayString, , strTitleBarMsg
End If
End Function
Building the DisplaySplashScreen() Function
This last function in the script displays the game’s splash screen, providing information about the game and its creator as well as an invitation for the player to return and play again another time.
Function DisplaySplashScreen()
MsgBox “Thank you for using the Lucky Lottery Number Picker “ & _
“© Jerry Ford 2004.” & vbCrLf & vbCrLf & “Please play again “ & _
“soon!”, 4144, strTitleBarMsg
WScript.Quit()
End Function
The last statement in the function terminates the script’s execution using the WScript Quit()method.
The Final Result
That’s it You’re all done Your fully assembled script should look like this:
‘*************************************************************************
‘Script Name: LuckyLotteryNumberPicker.vbs
‘Author: Jerry Ford
‘Created: 11/08/04
‘Description: This script randomly picks lottery numbers
‘*************************************************************************
Trang 10‘Initialization Section
Option Explicit
Dim aintLotteryArray(10) ‘Stores randomly generated lottery numbers
Dim blnAllNumbersPicked ‘Determines when a set of #s has been created
Dim blnInputValidated ‘Set to True when the player enters a valid number
Dim intNumberCount ‘Tracks the number of picks for a given play
Dim intNoOfValidPicks ‘Tracks the # of valid selections for a given set
Dim intNoOfPlays ‘Determines the # of sets of lottery #s to create
Dim intSetCount ‘Used to track how many sets have been generated
Dim intRandomNo ‘Used to store randomly generated lottery #s
Dim intNoOfPicksToSelect ‘Specifies how many #s to generate for each set
Dim intRangeOfNumbers ‘Specifies range to use when generating random #s
Dim strLotteryList ‘Displays a string showing 1 set of lottery #s
Dim strDisplayString ‘Used to display the list of selected lottery #s
Dim strDisplayType ‘Specifies whether to show full or summary data
Dim strTitleBarMsg ‘Specifies title bar message in pop-up dialogs
‘Main Processing Section
————————————————————————-SetVariableDefaults()
ProcessScriptIniFile()
CollectPlayerInput()
For intSetCount = 1 to intNoOfPlays
Do Until blnAllNumbersPicked = “True”
GetRandomNumber()