1. Trang chủ
  2. » Công Nghệ Thông Tin

Microsoft WSH and VBScript Programming for the Absolute Beginner Part 34 pps

10 209 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 158,02 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

strProcessGuess = FirstLevelValidation‘The player wants to quit the game If strProcessGuess = “ExitFunction” Then Exit Function End If ‘The player typed invalid input If strProcessGuess

Trang 1

astrWordList(6) = “ENTERPRISE”

astrWordList(7) = “ESCALATION”

astrWordList(8) = “HAPPINESS”

astrWordList(9) = “WEDNESDAY”

End Function

Function PlayTheGame()

‘Initialize variables displayed by the game’s initial pop-up dialog

intNoMisses = 0

intNoRight = 0

strWrongGuesses = “”

strRightGuesses = “”

‘Get the game a mystery word

strGameWord = RetrieveWord()

‘Call function that formats the initial pop-up dialog’s display string

strDisplayString = InitialDisplayString()

strTempStringOne = strGameWord

‘Let the player start guessing

Do Until intNoMisses = 6

‘Collect the player’s guess

strChoice = InputBox(vbCrLf & vbTab & strDisplayString & vbCrLf & _

vbCrLf & vbCrLf & “No of Misses: “ & intNoMisses & _

“ “ & vbTab & “Incorrect:” & strWrongGuesses & vbCrLf _

& vbCrLf & vbCrLf & _

“Type a letter and click on OK.” , cTitleBarMsg)

‘Determine if the player has quit

If strChoice = “” Then

Exit Function

End If

Trang 2

strProcessGuess = FirstLevelValidation()

‘The player wants to quit the game

If strProcessGuess = “ExitFunction” Then

Exit Function

End If

‘The player typed invalid input

If strProcessGuess <> “SkipRest” Then

strProcessGuess = SecondLevelValidation()

Select Case strProcessGuess

Case “DuplicateWrongAnswer”

MsgBox “Invalid: You’ve already guessed this incorrect letter.”

Case “DuplicateRightAnswer”

MsgBox “Invalid: You’ve already guessed this correct letter.”

Case Else

strCheckAnswer = TestLetterGuess()

If strCheckAnswer <> “IncorrectAnswer” Then

‘Reset the value of variable used to build a string containing

‘the interim stage of the word as currently guessed by player

strTempStringTwo = “”

NonGuessedString()

‘Check to see if the player has guessed the word

blnGameStatus = CheckIfGameWon()

If blnGameStatus = “True” Then

blnWordGuessed = “True”

Exit Do

End If

‘Set the value of the temporary string equal to the string

‘created by the Previous For Next loop

strTempStringOne = strTempStringTwo

Trang 3

‘Clear out the value of the strDisplayString variable

strDisplayString = “”

FlipString()

End If

End Select

End If

Loop

DisplayGameResults()

End Function

‘This function randomly retrieves a word from an array

Function RetrieveWord()

Randomize

intRandomNo = FormatNumber(Int(10 * Rnd))

RetrieveWord = astrWordList(intRandomNo)

End Function

Function InitialDisplayString()

‘Create a loop that processes each letter of the word

For intLetterCounter = 1 to Len(strGameWord)

‘Use underscore characters to display string representing each letter InitialDisplayString = InitialDisplayString & “_ “

Next

End Function

‘Validate the player’s input

Function FirstLevelValidation()

Trang 4

‘See if the player clicked on Cancel or failed to enter any input

If strChoice = “” Then

FirstLevelValidation = “ExitFunction”

Exit Function

End If

‘Make sure the player only typed 1 letter

If Len(strChoice) > 1 Then

MsgBox “Invalid: You must only enter 1 letter at a time!”

FirstLevelValidation = “SkipRest”

Else

‘Make sure the player did not type a number by accident

If IsNumeric(strChoice) = “True” Then

MsgBox “Invalid: Only letters can be accepted as valid input!”

FirstLevelValidation = “SkipRest”

Else

FirstLevelValidation = “Continue”

End If

End If

End Function

Function SecondLevelValidation()

‘Check to see if this letter is already on the incorrectly guessed list

If Instr(1, strWrongGuesses, UCase(strChoice), 1) <> 0 Then

SecondLevelValidation = “DuplicateWrongAnswer”

Else

‘Check to see if this letter is already on the correctly guessed list

If Instr(1, strRightGuesses, UCase(strChoice), 1) <> 0 Then

SecondLevelValidation = “DuplicateRightAnswer”

End If

End If

End Function

Trang 5

Function TestLetterGuess()

If Instr(1, UCase(strGameWord), UCase(strChoice), 1) = 0 Then

‘Add the letter to the list of incorrectly guessed letters

strWrongGuesses = strWrongGuesses & “ “ & UCase(strChoice)

‘Increment the number of guesses that the player has made by 1

intNoMisses = intNoMisses + 1

‘If the player has missed 6 guesses then he has used up all chances

If intNoMisses = 6 Then

blnWordGuessed = “False”

End If

TestLetterGuess = “IncorrectGuess”

Else

TestLetterGuess = “CorrectGuess”

End If

End Function

Function NonGuessedString()

‘Loop through the temporary string

For intLetterCounter = 1 to Len(strTempStringOne)

‘Examine each letter in the word one at a time

strWordLetter = Mid(strTempStringOne, intLetterCounter, 1)

‘Otherwise add an underscore character indicating a nonmatching guess

If UCase(strWordLetter) <> UCase(strChoice) Then

strTempStringTwo = strTempStringTwo & strWordLetter

Else

‘The letter matches player’s guess Add it to the temporary string

intNoRight = intNoRight + 1

strRightGuesses = strRightGuesses & “ “ & UCase(strChoice)

strTempStringTwo = strTempStringTwo & “_”

End If

Next

End Function

Trang 6

Function CheckIfGameWon()

‘Check and see if the player has guessed all the letters that make up

‘the word If so, set indicator variable and exit the Do Until loop

If intNoRight = Len(strGameWord) Then

CheckIfGameWon = “True”

End If

End Function

Function FlipString()

‘Spin through and reverse the letters in the strTempStringTwo variable

‘In order to switch letters to underscore characters and underscore

‘characters to the appropriate letters

For intFlipCounter = 1 to Len(strTempStringTwo)

‘Examine each letter in the word one at a time

strWordLetter = Mid(strTempStringTwo, intFlipCounter, 1)

‘Replace each letter with the underscore character

If strWordLetter <> “_” Then

strDisplayString = strDisplayString & “_ “

Else

‘Replace each underscore with its appropriate letter

strDisplayString = strDisplayString & _

Right(Left(strGameWord,intFlipCounter),1) & “ “

End If

Next

End Function

‘Determine if the player won or lost and display game results

Function DisplayGameResults()

‘Select message based on whether or not the player figured out the word

If blnWordGuessed = “True” Then

strMsgText = “Congratulations, You Win!”

Trang 7

strMsgText = “Sorry, You Lose.”

End If

‘Display the results of the game

intPlayAgain = MsgBox(vbCrLf & “The word was: “ & _

UCase(strGameWord) & vbCrLf & vbCrLf & vbCrLf & strMsgText & _

vbCrLf & vbCrLf & vbCrLf & _

“Would you like to play again?” , 4, cTitleBarMsg)

‘Find out if the player wants to play another game

If intPlayAgain = 6 Then

‘If the answer is yes reset the following variables & start a new game

strDisplayString = “”

strTempStringTwo = “”

PlayTheGame()

End If

End Function

‘This function displays the game splash screen

Function SplashScreen()

MsgBox “Thank you for playing VBScript Hangman © Jerry Ford 2002.” & _

vbCrLf & vbCrLf & “Please play again soon!”, , cTitlebarMsg

End Function

Although the script, as shown here, should work just fine, there is always the chance that you’ll miss something or make a typo when creating it After all, this is a rather large script and with size generally comes complexity, which only increases the probability that some-thing will go wrong

After you’ve thoroughly tested the script, give it to somebody else to test Ask your tester to play the game according to the rules, and then ask him to play it by not following the rules Ask your tester to keep track of any problems that he experiences and to record any error mes-sages that might appear If an error does appear, get the player to write down exactly what steps he took, so that you can go back and generate the error yourself and begin debugging it.

Trang 8

In this chapter, you learned how to add programming logic to your scripts to help deal with errors This included everything from rewriting error messages to making them more user friendly to ignoring errors or creating error-handling routines that allow your scripts to recover from certain types of errors I also provided advice that can help you prevent errors from occurring in the first place, or at least minimize their number Finally, I reviewed the different ways of reporting errors that cannot otherwise be handled On top of all this, you learned how to create the Hangman game and how to test it at various stages of development.

C H A L L E N G E S

1 Make the Hangman game more fun and interesting by expanding the pool of game words

2 Improve the Hangman program by keeping track of the number of games played during a session and displaying a summary of the overall number of times the player won and lost.

3 Add logic to the Hangman game that allows you to track its use For example, prompt the player for his or her name, and then write a message to either a log file or the Windows application event log each time the player plays the game.

Trang 10

Using the Windows Registry to Configure

Script Settings

10

S o far, all the scripts you’ve worked with in this book collected

configura-tion informaconfigura-tion and input from three places: from the user, from within the script itself, or from INI files In this chapter, I’ll show you another option for externalizing script settings by storing and retrieving configuration data using the Windows Registry As a bonus, in the chapter’s game project, I’ll also demonstrate how to retrieve input data from files Specifically, you will

• Review the overall organization and design of the Windows Registry

• Learn how to programmatically create, modify, and delete Registry keys and values

• Learn how to read data stored in external files and process it as input

Project Preview: Part 2 of the Hangman Game

In this chapter, you will enhance the Hangman game that you began developing

in Chapter 9, “Handling Script Errors.” You’ll begin by creating a new setup script that uses the Windows Registry to store the location of the folder where new external word files are stored You’ll then modify the Hangman script by remov-ing the array that stores game words within the script and tweakremov-ing the script so that it retrieves words from the external word files You will also modify the game to allow the player to select the category of words to play in For example, you might want to create different word files for categories such as Foods or Places

Ngày đăng: 03/07/2014, 18:20