BreezyHamilton
BreezyHamilton

Reputation: 11

VBScript for rock, paper, scissors outcome

Let me first start by saying thank you in advance for any help.

I am working a script for school using VBScript. I have never used this particular script in the past but it does not seem too terrible to use. The assignment I am working on is to add an outcome display to the results of the game. Below is what I have so far. If I use all "IF" statements the script will run but it will display all of the game outcomes. Changing the "IF" statement to and "ELSEIF" statement returns a syntax error beginning with the "THEN" statement in the second line of the outcome script. Can someone point me in the right direction as to what I have missed or what I have done incorrectly. Thanks again for the time and consideration.

'Formally declare variables 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")

'Input for player name
Reply1 = InputBox("Hello. What is your name?")
'Display greeting and player name
MsgBox "Hello " & Reply1 & "! Welcome to Rock, Paper Scissors!"

'Display the rules of the game
WshShl.Popup "Welcome to Rock, Paper 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 Paper, Rock, or Scissors.", _
  "Let's play a game!")

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

'Assign a value to the randomly selected number
If GetRandomNumber = 3 then CardImage = "rock"
If GetRandomNumber = 2 then CardImage = "scissor"
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

If Answer = "Rock" & Computer <> "Paper" Then MsgBox "Paper Covers Rock: Computer Wins"
ElseIfAnswer = "Paper" & Computer <> "Scissors" Then MsgBox "Scissors Cuts Paper: Computer Wins"
ElseIfAnswer = "Scissors" & Computer <> "Rock" Then MsgBox "Rock Breaks Scissors: Computer Wins"
ElseIfComputer = "Rock" & Answer <> "Paper" Then MsgBox "Paper Covers Rock: You Win "
ElseIfComputer = "Paper" & Answer <> "Scissors" Then MsgBox "Scissors Cuts Paper: You Win "
ElseIfComputer = "Scissors" & Answer <> "Rock" Then MsgBox "Rock Breaks Scissors: You Win "
ElseIfComputer = "Rock" & Answer <> "Rock" Then MsgBox "TIE "
ElseIfComputer = "Paper" & Answer <> "Paper" Then MsgBox "TIE "
Else Computer = "Scissor" & Answer <> "Scissor" Then MsgBox "TIE "

End If

Upvotes: 1

Views: 1272

Answers (3)

omegastripes
omegastripes

Reputation: 12612

Take a look at this example:

Option Explicit

Dim Reply, Answer, RandomNumber, Computer, Result, UserChoice

' Init the random number generator
Randomize

' Input for player name
Reply = InputBox("Hello. What is your name?")

' Display greeting and player name
MsgBox "Hello " & Reply & "! Welcome to Rock Paper Scissors!"

' Loop begin - start the turn
Do
    ' Display the rules of the game and prompt the user to select a choice
    Do
        Answer = InputBox(_
            "Here are the rules of the game:" & vbCrLf & _
            "1. Guess the same thing as the computer to tie." & vbCrLf & _
            "2. Paper covers Rock and wins." & vbCrLf & _
            "3. Rock breaks Scissors and wins." & vbCrLf & _
            "4. Scissors cut Paper and wins." & vbCrLf & vbCrLf & _
            "Type Paper, Rock, or Scissors.", "Let's play a game!")
    Loop Until Answer = "Paper" Or Answer = "Rock" Or Answer = "Scissors" ' Repeat until proper user input

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

    ' Assign a value to the randomly selected number
    Select Case RandomNumber
        Case 1 Computer = "Paper"
        Case 2 Computer = "Rock"
        Case Else Computer = "Scissors"
    End Select

    ' Check the game's results
    If Answer = Computer Then
        Result = "Tie"
    Else
        Select Case Answer & Computer
            Case "PaperRock" Result = "Paper Covers Rock: You Win"
            Case "RockScissors" Result = "Rock Breaks Scissors: You Win"
            Case "ScissorsPaper" Result = "Scissors Cut Paper: You Win"
            Case "RockPaper" Result = "Paper Covers Rock: Computer Win"
            Case "ScissorsRock" Result = "Rock Breaks Scissors: Computer Win"
            Case "PaperScissors" Result = "Scissors Cut Paper: Computer Win"
        End Select
    End If

    ' Display the game's results so that the user can see if he or she won
    UserChoice = MsgBox(_
        "You picked: " & Answer & vbCrLf & _
        "Computer picked: " & Computer & vbCrLf & _
        Result, vbRetryCancel, "Result")

Loop While UserChoice = vbRetry ' Check user choice to continue or exit

' Say goodbye
MsgBox "Bye " & Reply & "!"

Upvotes: 0

Casey
Casey

Reputation: 3353

You need spaces between your keywords and variables. Else If is two words. The logical operator is And, not &. Your last condition doesn't make sense because Else without If doesn't permit a condition afterwards (the whole idea is the Else statement runs if none of the previous conditions match). I think you should probably spent a little time reviewing basic syntax.

Upvotes: 0

Robert Columbia
Robert Columbia

Reputation: 6418

You need to put a space after ElseIf.

ElseIf Answer = "Paper" & Computer <> "Scissors" Then MsgBox "Scissors Cuts Paper: Computer Wins"
ElseIf Answer = "Scissors" & Computer <> "Rock" Then MsgBox "Rock Breaks Scissors: Computer Wins"
ElseIf Computer = "Rock" & Answer <> "Paper" Then MsgBox "Paper Covers Rock: You Win "
ElseIf Computer = "Paper" & Answer <> "Scissors" Then MsgBox "Scissors Cuts Paper: You Win "
ElseIf Computer = "Scissors" & Answer <> "Rock" Then MsgBox "Rock Breaks Scissors: You Win "
ElseIf Computer = "Rock" & Answer <> "Rock" Then MsgBox "TIE "
ElseIf Computer = "Paper" & Answer <> "Paper" Then MsgBox "TIE "

Upvotes: 1

Related Questions