Back to the Rock, Paper, and Scissors Game
Microsoft WSH and VBScript Programming for the Absolute Beginner
Authors: Ford J. L.
Published year: 2005
Pages: 24/137
Buy this book on amazon.com >>

Back to the Rock, Paper, and Scissors Game

Okay. Now it's time to go back to where this chapter started—talking about the Rock, Paper, and Scissors game. In this project, you will create a scripted version of this classic game. This version will be a bit limited, given that you've not had the chance yet to learn everything you'll need to create a more sophisticated version. However, you know enough to build the game's foundation and get a working model going. Later, in Chapter 5, you'll get the chance to return and spice things up a bit.

Designing the Game

The basic design of this game is quite simple. First, display the rules of the game, then ask the player to type either rock , paper , or scissors . Next , have the script randomly pick a choice of its own and display the results.

This project will be completed in six steps.

  1. Define the resources used by this script.

  2. Display the game's instructions.

  3. Provide a way for the user to select a choice.

  4. Devise a way for the script to generate a random number.

  5. Assign the computer's choice based on the script's randomly selected number.

  6. Display the final results of the game.

Defining the Resources Used by the Script

Begin by opening your editor and saving a blank file with a name of RockPaperScissors.vbs. Next add the first few lines of the script as follows :

'Formally declare each variable used by the script before trying to use them Dim WshShl, Answer, CardImage 'Create an instance of the WScript object in order to later use the Popup method Set WshShl = WScript.CreateObject("WScript.Shell")

The first line begins with a ' character. This character identifies a VBScript comment. Comments can be used to document the contents of scripts. Comments have no affect on the execution of a script. The next line begins with the VBScript keyword Dim . This statement defines three variables that will be used by the script. A variable is simply a portion of the computer memory where your scripts can store and retrieve data. I'll provide more information about variables and how they work in Chapter 4, "Constants, Variables, and Arrays." The third statement is another comment, and the fourth statement uses the WScript object's CreateObject() method to set up an instance of the WshShell object. This statement allows the script to access WshShell properties and methods .

Displaying the Rules of the Game

Next, let's take advantage of the WshShell object that you just defined by using its Popup() method to display a message in a graphical popup dialog.

'Display the rules of the game WshShl.Popup "Welcome to Rock, Pager and Scissors game. Here are the "& _ "rules of the game: 1. Guess the same thing as the computer "& _ "to tie. 2. Paper covers rock and wins. 3. Rock breaks "& _ "scissors and wins. 4. Scissors cut paper and wins."

What you have here is really just two lines of code, though it looks like five. The first line is a comment. However, the second line was so big that I chose to break it down into multiple pieces for easy display. To do so, I broke the message that I wanted to display into multiple segments of similar lengths, placing each segment within a pair of quotation marks. Then, to tie the different segments into one logical statement, I added the VBScript & character to the end of each line, followed by the _ character.

Collecting the Player's Selection

When the player clicks OK, the popup dialog displaying the game's rules disappears and is replaced with a new popup dialog that is generated by the following code:

'Prompt the user to select a choice Answer = InputBox("Type either Paper, Rock or Scissors.", "Let's play a game!")

The first statement is a comment and can be ignored. The second statement uses the VBScript InputBox() function to display a popup dialog into which the user can type either rock , paper , or scissors . The value typed by the user is then assigned to a variable called Answer .

Setting Up the Script's Random Selection

Now that the player has selected his or her choice, it's the script's turn to make a random selection on behalf of the computer. This can be done in two statements, as shown by the following statements:

'Time for the computer to randomly pick a choice Randomize GetRandomNumber = Int((3 * Rnd()) + 1)

The first line is a comment and can be ignored. The second line executes the Randomize statement, which ensures that the computer will generate a random number. If you left this line out and ran the script several times, you'd notice that after making an initial random choice, the script will always make the exact same choice time and time again. The Randomize statement prevents this behavior by ensuring that a random number is generated each time the script executes.

The next statement generates a random number between 1 and 3. I'll break down the activity that occurs in this statement. First, the Rnd() function generates a random number between 0 and 1. Next, the Int() function, which returns the integer portion of a number, executes, multiplying 3 times the randomly generated number and then adding 1 to it. The final result is a randomly generated number with a value between 1 and 3.

Assigning a Choice to the Script's Selection

Next, you'll need to assign a choice to each of the three possible numeric values randomly generated by the script.

'Assign a value to the randomly selected number If GetRandomNumber = 3 then CardImage = "rock" If GetRandomNumber = 2 then CardImage = "scissors" If GetRandomNumber = 1 then CardImage = "paper"

If the number 1 is generated, then a value of rock is assigned as the computer's selection. If the number 2 is generated, then a value of scissors is assigned as the computer's selection. Finally, if the number 3 is generated, then a value of paper is assigned as the computer's selection.

Displaying the Results of the Game

Once the script has come up with the computer's selection, it's time to display the results of the game so that user can see who won.

'Display the game's results so that the user can see if he or she won WshShl.Popup "You picked: "& Answer & Space(12) & "Computer picked: "& _ CardImage

The WshShell object's Popup() method is used to display the results of the game. Using the & concatenation character, I pieced together the various parts of the message. These parts included text phrases enclosed within quotation marks; the Answer variable; the CardImage variable, which represents the user's and computers' choices; the Space() method, which added 12 blank spaces to the text messages; and the _ character, which allowed me to spread the message out over two separate lines.

The Final Result

Now let's put all the pieces of the script together and then save and run the scripts.

'Formally declare each variable used by the script before trying to use them Dim WshShl, Answer, CardImage 'Create an instance of the WScript object in order to later use the Popup method Set WshShl = WScript.CreateObject("WScript.Shell") 'Display the rules of the game WshShl.Popup "Welcome to Rock, Pager and Scissors game. Here are the "& _ "rules of the game: 1. Guess the same thing as the computer "& _ "to tie. 2. Paper covers rock and wins. 3. Rock breaks "& _ "scissors and wins. 4. Scissors cut paper and wins." 'Prompt the user to select a choice Answer = InputBox("Type either Paper, Rock or Scissors.", "Let's play a game!") 'Time for the computer to randomly pick a choice Randomize GetRandomNumber = Int((3 * Rnd()) + 1) 'Assign a value to the randomly selected number If GetRandomNumber = 3 then CardImage = "rock" If GetRandomNumber = 2 then CardImage = "scissors" If GetRandomNumber = 1 then CardImage = "paper" 'Display the game's results so that the user can see if he or she won WshShl.Popup "You picked: "& Answer & Space(12) & "Computer picked: "& _ CardImage


Microsoft WSH and VBScript Programming for the Absolute Beginner
Authors: Ford J. L.
Published year: 2005
Pages: 24/137
Buy this book on amazon.com >>

Similar books on Amazon