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

Microsoft WSH and VBScript Programming for the Absolute Beginner Part 41 ppsx

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 (85.32 KB, 10 trang )

380
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
Building the SeeIfWon() Function


The SeeIfWon() function, shown here, performs a series of eight tests to see whether the
game has been won by one of the players. These tests include checking all three cells in each
row and in each column to see whether the same player has selected them. The function
also checks diagonally to see whether there is a winner.
Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition
Function SeeIfWon()
‘Check across the first row
If A1 = strPlayer Then
If A2 = strPlayer Then
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!”
381
Chapter 11 • Working with Built-In VBScript Objects
382
End If
End If
End If
‘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 the diagonally
If A3 = strPlayer Then
If B2 = strPlayer Then
If C1 = strPlayer Then
Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition
strWinner = strPlayer
strDirection = “- Diagonally C1 - A3!”
End If
End If
End If
End Function
Building the DisplaySplashScreen() Function
The script’s final function, DisplaySplashScreen() is shown here. This function displays
information about the script and its author and then terminates the script’s execution by
using the
WScript object’s Quit() method.
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
The Final Result
That’s it. You have all the necessary pieces to assemble the game. Once you have keyed every-
thing, your fully assembled script should like the one that follows.
‘*************************************************************************
‘Script Name: TicTacToe.vbs
‘Author: Jerry Ford
‘Created: 11/15/04
‘Description: This script is a VBScript implementation of the
‘ Tic-Tac-Toe game
‘*************************************************************************
‘Initialization Section
Option Explicit
383
Chapter 11 • Working with Built-In VBScript Objects
384
Const cTitleBarMsg = “VBScript T I C T A C T O E”
Dim A1, A2, A3, B1, B2, B3, C1, C2, C3 ‘Variables representing sections
‘of the Tic Tac Toe game board
Dim blnGameOver ‘Boolean variable that determines when to end game
Dim blnPlayerTypedQuit ‘Variable used to track whether a player typed Quit
Dim blnStopGame ‘Variable used in Main Processing section to
‘determine when to stop the game
Dim blnValidCell ‘Boolean variable that determines whether a player
‘specified a valid cell
Dim intNoMoves ‘Variable used to keep track of the number of plays
Dim intPlayAgain ‘Variable holds player response when asked to play

‘again
Dim strNotificationMsg ‘Variable used to display messages to player
Dim strPlayer ‘Variable used to identify whose turn it is
Dim strWinner ‘Variable used to determine whether the game is won
Dim strPlayerInput ‘Variable used to hold the player’s cell selection
Dim strDirection ‘Variable identifies how the player won the game
blnStopGame = “False”
‘Main Processing Section ————————————————————————-
Do Until blnStopGame = “True” ‘Keep playing until players decide to stop
SetVariableDefaults()
ClearGameBoard()
ManageGamePlay()
If blnPlayerTypedQuit = “True” Then ‘One of the players typed Quit
blnStopGame = “True”
Else ‘The game is over. Ask the players if they’d like to play again
Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition
intPlayAgain = MsgBox(“Would you like to play another game of “ & _
“Tic Tac Toe?”, 4, cTitleBarMsg)
If intPlayAgain = 7 Then ‘A player clicked on No. B break out of loop
blnStopGame = “True”
End If
End If
Loop
DisplaySplashScreen()
‘Procedure Section ———————————————————————————-
Function SetVariableDefaults() ‘Establish default variable settings
blnGameOver = “False”
blnPlayerTypedQuit = “False”
blnValidCell = “False”
intNoMoves = 0

strNotificationMsg = “Welcome! To play Tic Tac Toe follow the “ & _
“instruction at the bottom of the screen. Type Quit to terminate “ & _
“the game at any time.”
strPlayer = “X”
strWinner = “None”
strDirection = “”
End Function
Function ClearGameBoard() ‘Reset the game board
385
Chapter 11 • Working with Built-In VBScript Objects
386
A1 = “ “
A2 = “ “
A3 = “ “
B1 = “ “
B2 = “ “
B3 = “ “
C1 = “ “
C2 = “ “
C3 = “ “
End Function
Function ManageGamePlay() ‘Manage the overall execution of the game
Do Until blnGameOver = “True”
‘Start by checking to see if the game has already been completed
If strWinner = “X” Then
strNotificationMsg = “Game over! Player X Wins “ & strDirection
DisplayGameResults()
blnGameOver = “True”
End If
If strWinner = “O” Then

strNotificationMsg = “Game over! Player O Wins “ & strDirection
DisplayGameResults()
blnGameOver = “True”
End If
If strWinner = “Nobody” Then
strNotificationMsg = “Game over. It’s a tie!”
DisplayGameResults()
blnGameOver = “True”
End If
If blnGameOver <> “True” Then ‘If game is not over display the board
DisplayBoard() ‘in order to collect next player’s input
ValidateInput() ‘Validate the input
Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition
If UCase(strPlayerInput) = “QUIT” Then ‘See if a player type Quit
blnPlayerTypedQuit = “True”
blnValidCell = “False”
blnGameOver = “True”
End If
End If
‘Count the number of valid cell selections
If blnValidCell = “True” Then
intNoMoves = intNoMoves + 1
MarkPlayerSelection()
End If
‘If all 9 cells have been filled in we have a tie
If intNoMoves = 9 Then
SeeIfWon()
If strWinner = “None” Then
strWinner = “Nobody”
End If

Else
SeeIfWon()
End If
‘Time to switch player turns
If blnValidCell = “True” Then
If strPlayer = “X” Then
strPlayer = “O”
Else
strPlayer = “X”
End If
End If
Loop
End Function
387
Chapter 11 • Working with Built-In VBScript Objects
388
Function DisplayBoard() ‘Display the game board
strPlayerInput = UCase(InputBox(vbCrLf & _
strNotificationMsg & _
vbCrLf & vbCrLf & vbCrLf & vbCrLf & _
vbTab & “1” & vbTab & vbTab & “2” & vbTab & vbTab & “3” & vbCrLf & _
vbCrLf & vbTab & vbTab & “|” & vbTab & vbTab & “|” & vbTab & _
vbCrLf & “A” & vbTab & A1 & vbTab & “|” & vbTab & A2 & vbTab & _
“|” & vbTab & A3 & vbCrLf & vbTab & vbTab & “|” & vbTab & vbTab & _
“|” & vbTab & vbCrLf & “ ————————————————-” & _
“———————————————————” & vbCrLf & vbTab & _
vbTab & “|” & vbTab & vbTab & “|” & vbTab & vbCrLf & “B” & vbTab & _
B1 & vbTab & “|” & vbTab & B2 & vbTab & “|” & vbTab & B3 & _
vbCrLf & vbTab & vbTab & “|” & vbTab & vbTab & “|” & vbTab & _
vbCrLf & “ ———————————————————————” & _

“————————————-” & vbCrLf & vbTab & vbTab & “|” & _
vbTab & vbTab & “|” & vbTab & vbCrLf & “C” & vbTab & C1 & vbTab & _
“|” & vbTab & C2 & vbTab & “|” & vbTab & C3 & vbCrLf & vbTab & _
vbTab & “|” & vbTab & vbTab & “|” & vbTab & vbCrLf & vbCrLf & _
vbCrLf & vbCrLf & “Player “ & strPlayer & _
“‘s turn. Type your move:”, cTitleBarMsg))
End Function
Function DisplayGameResults() ‘Game is over. Display the results.
MsgBox vbCrLf & _
strNotificationMsg & _
vbCrLf & vbCrLf & vbCrLf & vbCrLf & _
vbTab & “1” & vbTab & vbTab & “2” & vbTab & vbTab & “3” & vbCrLf & _
vbCrLf & vbTab & vbTab & “|” & vbTab & vbTab & “|” & vbTab & _
vbCrLf & “A” & vbTab & A1 & vbTab & “|” & vbTab & A2 & vbTab & _
“|” & vbTab & A3 & vbCrLf & vbTab & vbTab & “|” & vbTab & vbTab & _
“|” & vbTab & vbCrLf & _
“ ————————————————————————————” & _
“———————-” & vbCrLf & vbTab & vbTab & “|” & vbTab & vbTab & _
Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition
“|” & vbTab & vbCrLf & “B” & vbTab & B1 & vbTab & “|” & vbTab & _
B2 & vbTab & “|” & vbTab & B3 & vbCrLf & vbTab & vbTab & “|” & _
vbTab & vbTab & “|” & vbTab & vbCrLf & “ —————————” & _
“——————————————————————————-” & vbCrLf & _
vbTab & vbTab & “|” & vbTab & vbTab & “|” & vbTab & vbCrLf & _
“C” & vbTab & C1 & vbTab & “|” & vbTab & C2 & vbTab & “|” & vbTab & _
C3 & vbCrLf & vbTab & vbTab & “|” & vbTab & vbTab & “|” & vbTab & _
vbCrLf & vbCrLf & vbCrLf & vbCrLf, , cTitleBarMsg
End Function
Function ValidateInput() ‘Run several tests valid correct player input
Select Case strPlayerInput ‘Ensure a valid cell was specified

Case “A1”
blnValidCell = “True”
Case “A2”
blnValidCell = “True”
Case “A3”
blnValidCell = “True”
Case “B1”
blnValidCell = “True”
Case “B2”
blnValidCell = “True”
Case “B3”
blnValidCell = “True”
Case “C1”
blnValidCell = “True”
Case “C2”
blnValidCell = “True”
Case “C3”
blnValidCell = “True”
Case Else
blnValidCell = “False”
strNotificationMsg = “Invalid cell. Please try again.”
End Select
389
Chapter 11 • Working with Built-In VBScript Objects

×