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

Microsoft WSH and VBScript Programming for the Absolute Beginner Part 40 pot

10 137 0

Đ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 100,54 KB

Nội dung

370 ‘************************************************************************* ‘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 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” Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition Developing the Logic for the Main Processing Section The game’s main processing section is made up of a Do Until loop and a series of procedure calls. The loop is set up to execute until the players decide to stop playing the game as tracked using a Boolean variable named blnStopGame. 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 whether they’d like to play again 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() Building the SetVariableDefaults() Function The SetVariableDefaults() function, shown here, is straightforward and is responsible for setting default variable values. Function SetVariableDefaults() ‘Establish default variable settings blnGameOver = “False” blnPlayerTypedQuit = “False” blnValidCell = “False” 371 Chapter 11 • Working with Built-In VBScript Objects 372 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 Building the ClearGameBoard() Function The ClearGameBoard() function that follows is executed to clear out the contents of the game board to prepare it for a new game. As you can see, the game board is cleared by assigning a blank space to each cell of the board, thus removing any Xs or Os that may be present from a previous game. Function ClearGameBoard() ‘Reset the game board A1 = “ “ A2 = “ “ A3 = “ “ B1 = “ “ B2 = “ “ B3 = “ “ C1 = “ “ C2 = “ “ C3 = “ “ End Function Building the ManageGamePlay() Function The ManageGamePlay() function that follows is controlled by a Do Until loop that executes until the value assigned to a variable name blnGameOver is set equal to True. Within the loop, the function begins by performing a series of checks to determine whether the game has already been won. The first check looks to see whether player X has won. If this is the case, the value assigned to strNotificationMsg is set to Game over! Player X Wins and the value assigned to a variable named strDirection is used to identify how the game was won. The Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition display string assigned to strNotificationMsg is later used by the DisplayGameResults() func- tion. The next check looks to see whether player O won. The last check looks to see whether the game has ended in a tie. 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 If UCase(strPlayerInput) = “QUIT” Then ‘See if a player type Quit blnPlayerTypedQuit = “True” blnValidCell = “False” blnGameOver = “True” End If End If 373 Chapter 11 • Working with Built-In VBScript Objects 374 ‘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 If the game is not over yet (e.g., blnGameOver <> “True”), then the game board is displayed and a player is prompted to make a move. The player’s move is then validated. If the player typed Quit, then the script sets a number of controlling variables to indicate that the game is about to be terminated. Otherwise, the value assigned to intNoMoves is incremented by 1 to keep track of the game’s progress and the MarkPlayerSelection() function is called to associate the player’s move with a specific cell on the game board. Next the value assigned to intNoMoves is examined. If it is equal to 9, then the game is over and the script calls on SeeIfWon() to ascertain whether there was a winner. If nine moves were made and no winner Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition is identified, the game is declared a tie and the value assigned to strWinner is set equal to Nobody. If nine moves have not been made yet, the script still executes the SeeIfWon() func- tion to see whether player X or O has managed to line up three cells in a row. Finally, the last set of statements inside the loop controls player turns by switching the value assigned to strPlayer to either X or O. Building the DisplayBoard() Function The DisplayBoard() function consists of text designed to provide a graphic-like display. Embedded inside the game board displayed by this function are nine variables named A1 – A3, B1 – B3 , and C1 – C3—each of which represent a different cell on the game board. 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 Whenever this function is called it displays the game board, including the values assigned to each of its nine embedded variables (as either Xs or Os). This gives the game the capability to dynamically display each move made as the game progresses. 375 Chapter 11 • Working with Built-In VBScript Objects 376 Building the DisplayGameResults() Function The DisplayGameResults() function that follows is responsible for displaying the final results of the game. The dialog generated by this function is not much different than the dialog created by the DisplayBoard() function, except that this function uses the MsgBox() function in place of the InputBox() function. The MsgBox() function is more appropriate for this func- tion because it can be used to display a dialog with a single OK button, whereas using the InputBox() function would have resulted in the unnecessary display on a text input field at the bottom of the dialog. 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 & _ “|” & 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 Building the ValidateInput() Function The ValidateInput() function, shown here, uses a Select Case statement to process the input provided by players to determine whether valid input has been provided. Input is valid only if it is provided in the form of a valid cell range. Next, the function checks to be sure the player did not accidentally click on OK before entering a move. The last validation test performed by this function checks to make sure that the cell specified by the player has not Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition already been selected. This is done by checking to see whether the value assigned to the cell is anything other than a blank space. If it is, then regardless of whether a value of X or O has been assigned, the cell not available. 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 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” 377 Chapter 11 • Working with Built-In VBScript Objects 378 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 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 Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition 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 Building the MarkPlayerSelection() Function The MarkPlayerSelection() function that follows, is responsible for associating the player’s move with the appropriate cell on the game board. It does this by assigning the value stored in strPlayer to the specified cell. Remember, the value assigned to strPlayer is either an X or an O, depending on whose turn it is. 379 Chapter 11 • Working with Built-In VBScript Objects . to 9, then the game is over and the script calls on SeeIfWon() to ascertain whether there was a winner. If nine moves were made and no winner Microsoft WSH and VBScript Programming for the Absolute. that the cell specified by the player has not Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition already been selected. This is done by checking to see whether the. WSH and VBScript Programming for the Absolute Beginner, Second Edition Developing the Logic for the Main Processing Section The game’s main processing section is made up of a Do Until loop and

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