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

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

10 101 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

Tiêu đề Microsoft WSH and VBScript Programming for the Absolute Beginner
Trường học Standard University
Chuyên ngành Computer Science
Thể loại Bài luận
Năm xuất bản 2023
Thành phố New York
Định dạng
Số trang 10
Dung lượng 96,61 KB

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

Nội dung

of Misses: “ & intNoMisses & “ “ & vbTab & _ “Incorrect:” & strWrongGuesses & vbCrLf & vbCrLf & vbCrLf & _ “Type a letter and click on OK.” , cTitleBarMsg ‘Determine if the player has qu

Trang 1

End If

SplashScreen()

WScript.Quit()

‘Procedure Section

Function DoYouWantToPlay()

‘Display the splash screen and ask the user if he or she wants to play

strSplashimage = Space(100) & “***********” & vbCrLf & _

“W E L C O M E T O “ & Space(68) & “*” & Space(18) & “*” & _

vbCrLf & Space(100) & “0” & Space(18) & “*” & vbCrLf & _

“V B S c r i p t H A N G M A N !” & Space(50) & “—||—” & _

Space(15) & “*” & vbCrLf & Space(99) & “/” & Space(1) & “\” & _

Space(17) & “*” & vbCrLf & Space(120) & “*” & vbCrLf & _

Space(120) & “*” & vbCrLf & space(113) & “ ******* “ & _ vbCrLf & “Would you like to play a game?” & vbCrLf & “ “

DoYouWantToPlay = MsgBox(strSplashimage, 36, cTitlebarMsg)

End Function

Function PlayTheGame()

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

intNoMisses = 0

intNoRight = 0

strWrongGuesses = “”

strRightGuesses = “”

‘Get the game a mystery word

strGameWord = RetrieveWord()

Trang 2

‘Call the function that formats the initial pop-up dialog 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

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

Trang 3

strCheckAnswer = TestLetterGuess()

If strCheckAnswer <> “IncorrectAnswer” Then

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

‘containing the interim stage of the word as currently

‘guessed by the player

strTempStringTwo = “”

NonGuessedString()

‘Check to see if the player has guessed the word

blnGameStatus = CheckIfGameWon()

If blnGameStatus = “yes” 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

‘Clear out the value of the strDisplayString variable

strDisplayString = “”

FlipString()

End If

End Select

End If

Loop

DisplayGameResults()

End Function

Function InitialDisplayString()

Trang 4

‘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

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

Function DisplayGameResults()

‘Set message depending on whether or not player figured out the word

If blnWordGuessed = “True” Then

strMsgText = “Congratulations, You Win!”

Else

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 answer is yes reset the following variables and start a new game

strDisplayString = “”

strTempStringTwo = “”

PlayTheGame()

End If

End Function

‘This function retrieves a randomly selected word from a word file

Function RetrieveWord()

‘Locate the folder where collections of game words are stored

strGameFolder = GetstrWordFileLocation()

Trang 5

‘Get the player to select a word category

strSelectCategory = SelectAWordCategory(strGameFolder)

‘Create the complete path and file name for the selected word file

strInputFile = strGameFolder & “\” & strSelectCategory

‘Open the file for reading

Set strWordFile = objFsoObject.OpenTextFile(strInputFile, 1)

‘Set this variable to zero It represents the No of words in the file

intNoWordsInFile = 0

‘Count the number of words in the file

Do while False = strWordFile.AtEndOfStream

‘Read a line

strWordFile.ReadLine()

‘Keep count of the number of words (or lines) read

intNoWordsInFile = intNoWordsInFile + 1

‘If the loop iterates more than 50 times something is wrong

If intNoWordsInFile > 50 Then

Exit Do

End If

Loop

‘Close the file when done counting the number of words (or lines)

strWordFile.Close

‘Pick a random number between 1 and the number of words in the file

Randomize

intRandomNo = FormatNumber(Int((intNoWordsInFile + 1) * Rnd),0)

‘Open the file for reading

Set strWordFile = objFsoObject.OpenTextFile(strInputFile, 1)

‘Skip the reading of all words prior to the randomly selected word

For intLinesInFile = 1 to intRandomNo - 1

‘Read the randomly selected word

strWordFile.SkipLine()

Trang 6

‘Return the randomly selected word to the calling statement

RetrieveWord = strWordFile.ReadLine()

‘Close the file when done

strWordFile.Close

End Function

‘This function retrieves location of folder where word files are stored

Function GetstrWordFileLocation()

‘Get the folder name and path from its assigned Registry value

GetstrWordFileLocation = _

objWshShl.RegRead(“HKCU\VBGames\Hangman\ListLocation”)

End Function

‘This function returns a word category

Function SelectAWordCategory(TargetFolder)

‘Specify the location of the folder that stores the word files

Set strGameFolder = objFsoObject.GetFolder(TargetFolder)

‘Get a list of files stored in the folder

Set objGameFiles = strGameFolder.Files

strSelection = “”

‘Loop through the list of word files

For Each strWordList In objGameFiles

‘Build a master string containing a list of all the word files

strFileString = strFileString & strWordList.Name

‘Remove the txt portion of each file’s file name

strCharactersToRemove = Len(strWordList.Name) - 4

Trang 7

‘Build a display string showing the category names of each word file

strSelection = strSelection & _

Left(strWordList.Name, strCharactersToRemove) & vbCrLf

Next

blnValidResponse = “False”

‘Loop until a valid category strSelection has been made

Do Until blnValidResponse = “True”

‘Prompt the player to select a word category

strChoice = InputBox(“Please specify the name of a word category “ & _

“from which game words will be selected.” & vbCrLf & vbCrLf & _

“Available Categories:” & vbCrLf & vbCrLf & _

strSelection, “Pick a Category” , “General”)

‘If input is not in master string the player must try again

If InStr(UCase(strFileString), UCase(strChoice)) = 0 Then

MsgBox “Sorry but this is not a valid category Please try again.” Else

blnValidResponse = “True”

End If

Loop

‘If the player typed nothing then specify a default word category

If Len(strChoice) = 0 Then

strChoice = “General”

End If

‘Add the txt portion of the file name back

SelectAWordCategory = strChoice & “.txt”

End Function

‘This function displays the game splash screen

Function SplashScreen()

Trang 8

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

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

End Function

‘Validate the player’s input

Function FirstLevelValidation()

‘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”

Trang 9

End If

End If

End Function

Function CheckIfGameWon()

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

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

If intNoRight = Len(strGameWord) Then

CheckIfGameWon = “yes”

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 a underscore character indicating a nonmatching guess

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

strTempStringTwo = strTempStringTwo & strWordLetter

Else

‘The letter matches the guess so add it to the temporary string

intNoRight = intNoRight + 1

strRightGuesses = strRightGuesses & “ “ & UCase(strChoice)

strTempStringTwo = strTempStringTwo & “_”

End If

Next

End Function

Function FlipString()

‘Spin through and reverse the letters in the strTempStringTwo variable

‘In order to switch letters to underscore characters and underscore

Trang 10

‘characters to the appropriate letters

For strFlipCounter = 1 to Len(strTempStringTwo)

‘Examine each letter in the word one at a time

strWordLetter = Mid(strTempStringTwo, strFlipCounter, 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,strFlipCounter),1) & “ “

End If

Next

End Function

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 player has missed 6 guesses then he has used up all his chances

If intNoMisses = 6 Then

blnWordGuessed = “False”

End If

TestLetterGuess = “IncorrectGuess”

Else

TestLetterGuess = “CorrectGuess”

End If

End Function

Summary

In this chapter, you learned how to write scripts that programmatically interact with the Windows Registry; this included reading, writing, and modifying Registry keys and values.

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

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN