Tải bản đầy đủ (.pdf) (10 trang)

Microsoft WSH and VBScript Programming for the Absolute Beginner Part 42 docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (265.82 KB, 10 trang )

390
If strPlayerInput = “” Then ‘Player must type something
strNotificationMsg = “Missing entry. Please try again.”
blnValidCell = “False”
End If
‘Check each cell to make sure that it has not already been selected
If strPlayerInput = “A1” Then
If A1 <> “ “ Then
blnValidCell = “False”
strNotificationMsg = “Invalid entry. Cell already selected. “ & _
“Please try again.”
End If
End If
If strPlayerInput = “A2” Then
If A2 <> “ “ Then
blnValidCell = “False”
strNotificationMsg = “Invalid entry. Cell already selected. “ & _
“Please try again.”
End If
End If
If strPlayerInput = “A3” Then
If A3 <> “ “ Then
blnValidCell = “False”
strNotificationMsg = “Invalid entry. Cell already selected. “ & _
“Please try again.”
End If
End If
If strPlayerInput = “B1” Then
If B1 <> “ “ Then
blnValidCell = “False”
strNotificationMsg = “Invalid entry. Cell already selected. “ & _


“Please try again.”
End If
End If
Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition
If strPlayerInput = “B2” Then
If B2 <> “ “ Then
blnValidCell = “False”
strNotificationMsg = “Invalid entry. Cell already selected. “ & _
“Please try again.”
End If
End If
If strPlayerInput = “B3” Then
If B3 <> “ “ Then
blnValidCell = “False”
strNotificationMsg = “Invalid entry. Cell already selected. “ & _
“Please try again.”
End If
End If
If strPlayerInput = “C1” Then
If C1 <> “ “ Then
blnValidCell = “False”
strNotificationMsg = “Invalid entry. Cell already selected. “ & _
“Please try again.”
End If
End If
If strPlayerInput = “C2” Then
If C2 <> “ “ Then
blnValidCell = “False”
strNotificationMsg = “Invalid entry. Cell already selected. “ & _
“Please try again.”

End If
End If
If strPlayerInput = “C3” Then
If C3 <> “ “ Then
blnValidCell = “False”
strNotificationMsg = “Invalid entry. Cell already selected. “ & _
“Please try again.”
End If
End If
End Function
391
Chapter 11 • Working with Built-In VBScript Objects
392
Function MarkPlayerSelection() ‘Mark an X or O in the appropriate cell
If strPlayerInput = “A1” Then
A1 = strPlayer
End If
If strPlayerInput = “A2” Then
A2 = strPlayer
End If
If strPlayerInput = “A3” Then
A3 = strPlayer
End If
If strPlayerInput = “B1” Then
B1 = strPlayer
End If
If strPlayerInput = “B2” Then
B2 = strPlayer
End If
If strPlayerInput = “B3” Then

B3 = strPlayer
End If
If strPlayerInput = “C1” Then
C1 = strPlayer
End If
If strPlayerInput = “C2” Then
C2 = strPlayer
End If
If strPlayerInput = “C3” Then
C3 = strPlayer
End If
End Function
Function SeeIfWon()
‘Check across the first row
If A1 = strPlayer Then
If A2 = strPlayer Then
Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition
If A3 = strPlayer Then
strWinner = strPlayer
strDirection = “- First row across!”
End If
End If
End If
‘Check across the second row
If B1 = strPlayer Then
If B2 = strPlayer Then
If B3 = strPlayer Then
strWinner = strPlayer
strDirection = “- Second row across!”
End If

End If
End If
‘Check across the third row
If C1 = strPlayer Then
If C2 = strPlayer Then
If C3 = strPlayer Then
strWinner = strPlayer
strDirection = “- Third row across!”
End If
End If
End If
‘Check the first column
If A1 = strPlayer Then
If B1 = strPlayer Then
If C1 = strPlayer Then
strWinner = strPlayer
strDirection = “- First column down!”
End If
End If
End If
393
Chapter 11 • Working with Built-In VBScript Objects
394
‘Check the second column
If A2 = strPlayer Then
If B2 = strPlayer Then
If C2 = strPlayer Then
strWinner = strPlayer
strDirection = “- Second column down!”
End If

End If
End If
‘Check the third column
If A3 = strPlayer Then
If B3 = strPlayer Then
If C3 = strPlayer Then
strWinner = strPlayer
strDirection = “- Third column down!”
End If
End If
End If
‘Check diagonally
If A1 = strPlayer Then
If B2 = strPlayer Then
If C3 = strPlayer Then
strWinner = strPlayer
strDirection = “- Diagonally A1 - C3!”
End If
End If
End If
‘Check diagonally
If A3 = strPlayer Then
If B2 = strPlayer Then
If C1 = strPlayer Then
strWinner = strPlayer
strDirection = “- Diagonally C1 - A3!”
End If
End If
End If
End Function

Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition
Function DisplaySplashScreen() ‘Display splash screen and terminate game
MsgBox “Thank you for playing Tic-Tac-Toe” & _
“© Jerry Ford 2004.” & vbCrLf & vbCrLf & “Please play again “ & _
“soon!”, 4144, cTitleBarMsg
WScript.Quit()
End Function
Okay, run through the Tic-Tac-Toe game a few times to be sure that you haven’t accidentally
made a few typos when keying in the script. After you have everything working just right,
go out and get a friend to play with and show off what you have learned.
Summary
In this chapter, you learned how to work with built-in VBScript objects. This included learn-
ing how to create custom objects with their own unique set of properties and methods. You
also learned how to trigger events associated with custom objects. On top of all this, you
learned how to perform complex parsing operations by working with the
RegExp object and
you created your first multi-player VBScript game.
395
Chapter 11 • Working with Built-In VBScript Objects
C HALLENGES
1. Enhance the Tic-Tac-Toe game by adding options that allow the players to get
help.
2. If you have a Web site, consider modifying the game’s closing splash screen to
display its address.
3. Try making a computerized version of this game where a single player goes head
to head against the computer.
4. Add logic that keeps track of the total number of games played and display this
information, along with the total number of games won by each player, at the
end of the final game.
This page intentionally left blank

Combining
Different Scripting
Languages
12
CHAPTER
W
elcome to the final chapter of this book. In this chapter, you will learn
how to develop a new type of script, called a Windows Script File, which
enables you to combine VBScript with one or more other WSH-supported
scripting languages to create a single executable script. Doing so enables you to
create scripts that can take advantage of the strengths of each individual scripting
language. Specifically, in this chapter I’ll demonstrate how to develop Windows
Script Files that combine VBScript and JScript. Along the way, you’ll be introduced
to the Extensible Markup Language, or XML, which allows different scripting lan-
guages to be combined into Windows Script Files. Specifically, you will
• Learn how to combine VBScript with another scripting language to create
Windows Script Files
• Learn how XML is used to format Windows Script Files
• Get a sneak peek at the JScript scripting language
• Learn how to execute Windows Script Files
Project Preview: The VBScript Game Console
In your final project, you will learn how to create Windows Script Files that com-
bine VBScript with a little bit of JScript to create a game console for all your
VBScript games. Once started, the game console displays a dynamically created
numbered list of your VBScript games and enables the user to choose which game
to play by either typing in the name of the game or typing its assigned number.
CHAPTER
398
When started, the game console appears in the upper-left corner of the display area. As
games are selected, they will appear in the middle of the screen. This keeps the game console

handy without making it intrusive. Figures 12.1 through 12.5 demonstrate the overall oper-
ation of the game console from beginning to end.
Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition
Figure 12.1
A JScript that
displays the game
console’s initial
splash screen is
executed.
Figure 12.2
The core logic for
the game console
is provided by a
VBScript, which is
responsible for
displaying and
controlling the
execution of your
VBScript games.
Figure 12.3
The game console
remains tucked
away in the
corner while the
player enjoys
playing your
VBScript game.
Figure 12.4
By selecting the
About option, the

user can get more
information about
the game console
and its author.
Introducing Windows Script Files
One of the strengths of the WSH is that it supports a number of different scripting lan-
guages, including VBScript, JScript, Perl, Python, and REXX. Microsoft automatically equips
the WSH with VBScript and JScript. Third-party software developers provide support for the
other scripting languages. Besides executing scripts written in any of these scripting lan-
guages, the WSH enables you to put any combination of these languages into a single script
file known as a Windows Script File.
XML provides the glue for combining different scripts
into a Windows Script File. In this chapter, I’ll cover
some of the more commonly used WSH-supported
XML statements. However, there simply is not enough
space available in this book to completely cover every
single XML element supported by the WSH. To learn
more about the WSH’s support for XML, visit
and read
through the Windows Script Host documentation
posted there.
Using XML, you specify the components that make up
Windows Script Files. For example, you use XML to
mark the locations within Windows Script Files where
individual scripts (written in scripting languages such
as VBScript and JScript) are embedded. Windows
Script Files are saved as plain text files with a .wsh file
extension and can be created using any plain text or
script editor.
XML is case-sensitive and imposes a strict set of rules on the format of Windows Script Files.

For example, within the context of the WSH, most XML tags occur in pairs with one opening
and one closing tag. Failure to include a matching closing tag will result in an error.
399
Chapter 12 • Combining Different Scripting Languages
Figure 12.5
When the game
console is finally
closed, another
JScript is run to
display a closing
splash screen.
Definition
A Windows Script File is a type of
script that allows multiple scripts,
written in any WSH-supported
scripting language, to be combined
to create a single script.
Definition
Extensible Markup Language, or
XML, is a language similar in design
and syntax to HTML. It is used
within the context of the WSH to
define the structure of Windows
Script Files.

×