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

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

10 197 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 96,23 KB

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

Nội dung

Creating the NowGoPlay FunctionThe NowGoPlayfunction is called when the player clicks on the Yesbutton on the script’s initial pop-up dialog, indicating that he or she wants to play the

Trang 1

function DoYouWantToPlay()

strCardImage = “ ===============” & Space(15) & “Rules and “ & _

“Instructions” & vbCrLf & _

“| * “ & Space(32) & “|” & Space(15) & “================” & _

“========” & vbCrLf & _

“| * * “ & Space(31) & “|” & Space(15) & “1 Try to get to “ & _

“21 without going over.” & vbCrLf & _

“| *** “ & Space(31) & “|” & Space(15) & “2 Aces count as “ & _

“11s (not 1s).” & vbCrLf & _

“| * * “ & Space(31) & “|” & Space(15) & “3 The dealer “ & _

“must stop at 17 or later.” & vbCrLf & _

“|” & Space(15) & “****” & Space(16) & “|” & vbCrLf & _

“|” & Space(17) & “**” & Space(18) & “|” & vbCrLf & _

“|” & Space(14) & “* ** *” & Space(15) &”|” & vbCrLf & _

“|” & Space(11) & “********” & Space(12) &”|” & vbCrLf & _

“|” & Space(9) & “**********” & Space(10) & “|” & vbCrLf & _

“|” & Space(9) & “**********” & Space(10) & “|” & vbCrLf & _

“|” & Space(11) & “********” & Space(12) & “|” & vbCrLf & _

“|” & Space(13) & “******” & Space(14) & “|” & vbCrLf & _

“|” & Space(15) & “****” & Space(16) & “|” & vbCrLf & _

“|” & Space(17) & “ *” & Space(19) & “|” & vbCrLf & _

“|” & Space(32) & “* * |” & vbCrLf & _

“|” & Space(32) & “*** |” & vbCrLf & _

“|” & Space(32) & “* * |” & vbCrLf & _

“|” & Space(33) & “ * |” & vbCrLf & _

“ ===============” & vbCrLf & vbCrLf & vbCrLf & vbCrLf & _

“Would you like to play a game of Blackjack Lite? “

DoYouWantToPlay = MsgBox(strCardImage, 36, “BlackJack Lite”)

End Function

If the player clicks on the Yesbutton, the value of DoYouWantToPlayis set to 6 This value will later be tested in the script’s main processing section to determine whether the game should continue.

Trang 2

Creating the NowGoPlay() Function

The NowGoPlay()function is called when the player clicks on the Yesbutton on the script’s initial pop-up dialog, indicating that he or she wants to play the game.

function NowGoPlay()

DealFirstHand()

PlayTheGame()

If strUserBusted = “False” Then

ComputerPlay()

End If

DetermineWinner()

End Function

This function controls the actual play of the game It is made up of calls to several other functions First it calls the DealFirstHand()function, which deals both the user and the com-puter their initial cards Next it calls the PlayTheGame()function, which allows the user to continue to take hits or hold, and determines whether the player busted The NowGoPlay() function then checks the value of the strUserBusted variable to see whether the game should continue If the user decides to hold, then the ComputerPlay()function is called so the computer’s (or dealer’s) hand can finish being dealt Regardless of whether the user busts or the ComputerPlay()function is called, eventually control returns to the NowGoPlay() function and the DetermineWinner()function is called This function determines the winner

of the hand and gives the player an opportunity to play another hand.

Creating the DealFirstHand() Function

The DealFirstHand() function makes two calls to the GetRandomNumber() function to deal both the player’s and the computer’s initial cards.

function DealFirstHand()

intUserCard = GetRandomNumber()

intComputerCard = GetRandomNumber()

End Function

Trang 3

Creating the PlayTheGame() Function

The PlayTheGame()function, shown next, sets up a Do Untilloop that executes until either the player busts or decides to hold The first statement in the loop prompts the player to decide whether he or she wants another card If the player clicks Yes, the DealAnotherCard()function

is called The PlayTheGame()function then checks to see whether the player has busted, setting the value of the strUserBusted and strUserDonevariables if appropriate (True) If the player decides to hold and clicks the Nobutton, the value of strUserDoneis also set to True.

function PlayTheGame()

Do Until strUserDone = “True”

intAnotherCard = MsgBox(“User: “ & Space(8) & intUserCard & vbCrLf & _

“Computer: “ & intComputerCard & vbCrLf & vbCrLf & _

“[Click on YES for another card]” & vbCrLf & _

“[Click on NO to stick]”, 4, “Initial Deal”)

Select Case intAnotherCard

Case 6 ‘User clicked on Yes

‘MsgBox “You decided to take a hit.”

DealAnotherCard()

Case 7 ‘User clicked on No

strUserDone = “True”

End Select

If intUserCard > 21 then

strUserBusted = “True”

strUserDone = “True”

End If

Loop

End Function

Creating the DealAnotherCard() Function

The DealAnotherCard()function is called from the PlayTheGame()function when the player elects to take a hit (asks for another card) This function consists of two statements The first

Trang 4

statement assigns the card number returned to it from the GetRandomNumber()function to a variable named intUserNextCard The second statement tallies the player’s hand by adding the value of the new card to the cards already in the player’s hand.

function DealAnotherCard()

intUserNextCard = GetRandomNumber()

tinUserCard = intUserCard + intUserNextCard

End Function

Creating the ComputerPlay() Function

The ComputerPlay()function, shown here, is responsible for dealing the computer’s hand It uses a Do Whileloop to continue dealing the computer’s hand until either the computer’s hand exceeds a total of 17 but remains under 21, or it busts.

function ComputerPlay()

Do While intComputerCard < 17

intNewComputerCard = GetRandomNumber()

intComputerCard = intComputerCard + intNewComputerCard

Loop

End Function

Inside the Do Whileloop are two statements The first statement deals the computer a new card by calling the GetRandomNumber() function The second statement uses the value returned by the GetRandomNumber()function to update the computer’s hand (that is, its total).

Creating the DetermineWinner() Function

The DetermineWinner()function, shown here, checks to see whether the value of strUserBusted

is set to True It also checks to see whether the computer has busted, by checking to see whether the value of intComputerCardis greater than 21 If either of these conditions is true, the script assigns an appropriate text message to the strTextMsg variable This variable is used as input in an InputBox()pop-up dialog that shows the player the results of the game.

If neither of these conditions is true, the DetermineWinner()function performs three tests to determine whether the player won, the computer won, or whether there was a tie; the func-tion then sets the value of strTextMsgaccordingly.

Trang 5

function DetermineWinner()

If strUserBusted = “True” Then

strTextMsg = “The user has busted!”

Else

If intComputerCard > 21 then

strTextMsg = “The Computer has busted!”

Else

If intUserCard > intComputerCard Then strTextMsg = “The user wins!”

If intUserCard = intComputerCard Then strTextMsg = “Push (e.g Tie)!”

If intUserCard < intComputerCard Then strTextMsg = “The “ & _

“Computer wins!”

End If

End If

intPlayAgain = MsgBox(strTextMsg & vbCrLf & vbCrLf & “User: “ & _

Space(8) & intUserCard & vbCrLf & “Computer: “ & intComputerCard & _

vbCrLf & vbCrLf & vbCrLf & _

“Would you like to play another game?”, 4, “Initial Deal”)

If intPlayAgain = 6 Then

strUserBusted = “False”

strUserDone = “False”

NowGoPlay()

End If

DisplaySplashScreen()

End Function

Finally, the game’s results are displayed by showing the value of strTextMsgand the value of the player’s and the computer’s final hands The pop-up dialog also asks the player whether

he or she would like to play again If the player clicks the Yes button, then the NowGoPlay() function is called and the game starts over again Otherwise, the DisplaySplashScreen() function is called and the game ends.

Trang 6

Creating the DisplaySplashScreen() Function

This final function displays the game’s splash screen, providing a little information about the game and its creator, as well as offering an invitation to the player to return and play the game again another time.

function DisplaySplashScreen()

MsgBox “Thank you for playing BlackJack Lite © Jerry Ford 2002.” & _

vbCrLf & vbCrLf & “Please play again soon!”, 4144, “BlackJack Lite”

WScript.Quit()

End Function

After displaying the splash screen in a pop-up dialog, the DisplaySplashScreen() function ends the game by executing the WScript Quit()method.

The Final Result

That’s it You are all done Your fully assembled script should look as follows.

‘*************************************************************************

‘Script Name: BlackJack.vbs

‘Author: Jerry Ford

‘Created: 11/28/02

‘Description: This script creates a scaled down version of the casino

‘version of the BlackJack card game

‘*************************************************************************

‘Initialization Section

Option Explicit

Dim intPlayGame, strCardImage, intUserCard, intComputerCard, intAnotherCard

Dim intUserNextCard, strUserDone, intNewComputerCard, intPlayAgain

Dim strUserBusted, strTextMsg

strUserDone = “False”

strUserBusted = “False”

Trang 7

‘Main Processing Section

‘Ask the user if he or she wants to play

intPlayGame = DoYouWantToPlay()

Select Case intPlayGame

Case 6 ‘User clicked on Yes

NowGoPlay()

Case 7 ‘User clicked on No

DisplaySplashScreen()

End Select

‘Procedure Section

function DoYouWantToPlay()

strCardImage = “ ===============” & Space(15) & “Rules and “ & _

“Instructions” & vbCrLf & _

“| * “ & Space(32) & “|” & Space(15) & “================” & _

“========” & vbCrLf & _

“| * * “ & Space(31) & “|” & Space(15) & “1 Try to get to “ & _

“21 without going over.” & vbCrLf & _

“| *** “ & Space(31) & “|” & Space(15) & “2 Aces count as “ & _

“11s (not 1s).” & vbCrLf & _

“| * * “ & Space(31) & “|” & Space(15) & “3 The dealer “ & _

“must stop at 17 or later.” & vbCrLf & _

“|” & Space(15) & “****” & Space(16) & “|” & vbCrLf & _

“|” & Space(17) & “**” & Space(18) & “|” & vbCrLf & _

“|” & Space(14) & “* ** *” & Space(15) &”|” & vbCrLf & _

“|” & Space(11) & “********” & Space(12) &”|” & vbCrLf & _

“|” & Space(9) & “**********” & Space(10) & “|” & vbCrLf & _

“|” & Space(9) & “**********” & Space(10) & “|” & vbCrLf & _

“|” & Space(11) & “********” & Space(12) & “|” & vbCrLf & _

“|” & Space(13) & “******” & Space(14) & “|” & vbCrLf & _

“|” & Space(15) & “****” & Space(16) & “|” & vbCrLf & _

“|” & Space(17) & “ *” & Space(19) & “|” & vbCrLf & _

“|” & Space(32) & “* * |” & vbCrLf & _

“|” & Space(32) & “*** |” & vbCrLf & _

“|” & Space(32) & “* * |” & vbCrLf & _

Trang 8

“|” & Space(33) & “ * |” & vbCrLf & _

“ ===============” & vbCrLf & vbCrLf & vbCrLf & vbCrLf & _

“Would you like to play a game of Blackjack Lite? “

DoYouWantToPlay = MsgBox(strCardImage, 36, “BlackJack Lite”)

End Function

function NowGoPlay()

DealFirstHand()

PlayTheGame()

If strUserBusted = “False” Then

ComputerPlay()

End If

DetermineWinner()

End Function

function DealFirstHand()

intUserCard = GetRandomNumber()

intComputerCard = GetRandomNumber()

End Function

function PlayTheGame()

Do Until strUserDone = “True”

intAnotherCard = MsgBox(“User: “ & Space(8) & intUserCard & vbCrLf & _

“Computer: “ & intComputerCard & vbCrLf & vbCrLf & _

“[Click on YES for another card]” & vbCrLf & _

“[Click on NO to stick]”, 4, “Initial Deal”)

Select Case intAnotherCard

Case 6 ‘User clicked on Yes

‘MsgBox “You decided to take a hit.”

Trang 9

Case 7 ‘User clicked on No

strUserDone = “True”

End Select

If intUserCard > 21 then

strUserBusted = “True”

strUserDone = “True”

End If

Loop

End Function

function GetRandomNumber()

Randomize

GetRandomNumber = Round(FormatNumber(Int((13 * Rnd) + 1)))

If GetRandomNumber = 1 then GetRandomNumber = 11

If GetRandomNumber > 10 then GetRandomNumber = 10

End Function

function DealAnotherCard()

intUserNextCard = GetRandomNumber()

tinUserCard = intUserCard + intUserNextCard

End Function

function ComputerPlay()

Do While intComputerCard < 17

intNewComputerCard = GetRandomNumber()

intComputerCard = intComputerCard + intNewComputerCard

Loop

End Function

Trang 10

function DetermineWinner()

If strUserBusted = “True” Then

strTextMsg = “The user has busted!”

Else

If intComputerCard > 21 then

strTextMsg = “The Computer has busted!”

Else

If intUserCard > intComputerCard Then strTextMsg = “The user wins!”

If intUserCard = intComputerCard Then strTextMsg = “Push (e.g Tie)!”

If intUserCard < intComputerCard Then strTextMsg = “The “ & _

“Computer wins!”

End If

End If

intPlayAgain = MsgBox(strTextMsg & vbCrLf & vbCrLf & “User: “ & _

Space(8) & intUserCard & vbCrLf & “Computer: “ & intComputerCard & _

vbCrLf & vbCrLf & vbCrLf & _

“Would you like to play another game?”, 4, “Initial Deal”)

If intPlayAgain = 6 Then

strUserBusted = “False”

strUserDone = “False”

NowGoPlay()

End If

DisplaySplashScreen()

End Function

function DisplaySplashScreen()

MsgBox “Thank you for playing BlackJack Lite © Jerry Ford 2002.” & _

vbCrLf & vbCrLf & “Please play again soon!”, 4144, “BlackJack Lite”

WScript.Quit()

End Function

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

TỪ KHÓA LIÊN QUAN