2 Replies - 1176 Views - Last Post: 02 May 2013 - 08:49 PM Rate Topic: -----

#1 Zakku  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-April 13

Rock Paper Scissors Code problem

Posted 29 April 2013 - 09:31 PM

Hey guys I need some help with my code, I have to add Subs into it but every time I do it comes up with a compile error, also I need to add a score counter however I'm unsure of what code I should be using, this is my code so far
Option Explicit
 Dim ComputerThrow, Number, CardImage, Answer, Rock, Paper, Scissors, Count, NoRounds
NoRounds = 0
MsgBox ("Welcome to Rock, Paper and Scissors 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."), vbinformation, "Welcome" 

Number= Inputbox ("How many games do you wish to play?","Number of games", "1,2,3,4,5?")
FOR Count = 1 To Number
NoRounds = NoRounds + 1
Answer = InputBox ("Rock, Paper or Scissors?","Choose one","Rock, Paper, Scissors")
	
	Randomize()
        ComputerThrow = Int((3 * Rnd()) + 1)

If ComputerThrow = 1 then CardImage = "Rock"
If ComputerThrow = 2 then CardImage = "Scissors"
If ComputerThrow = 3 then CardImage = "Paper"

MsgBox "You Picked " & Answer & " Computer picked " & CardImage,vbquestion, "Results"

If Answer = "Rock" and CardImage = "Rock" Then    
			MsgBox "Both throw Rock. It's a Draw!",vbinformation, "Draw"
        ElseIf Answer = "Rock" and CardImage = "Paper" Then
            MsgBox "Computer throws Paper. Computer wins!",vbinformation, "Bad luck"
        ElseIf Answer = "Rock" and CardImage = "Scissors" Then
            MsgBox "Computer throws scissors. You win!",vbinformation, "Winner"
		ElseIf Answer = "Paper" and CardImage = "Rock" Then   
			MsgBox "Computer throws Rock. You win!",vbinformation, "Winner"
        ElseIf Answer = "Paper" and CardImage = "Paper" Then
            MsgBox "Both throw Paper. It's a Draw!",vbinformation, "Draw"
        ElseIf Answer = "Paper" and CardImage = "Scissors" Then
            MsgBox "Computer throws scissors. Computer wins!",vbinformation, "Bad luck" 
		ElseIf Answer = "Scissors" and CardImage = "Rock" Then   
			MsgBox "Computer throws Rock. Computer wins!",vbinformation, "Bad luck"
        ElseIf Answer = "Scissors" and CardImage = "Paper" Then
            MsgBox "Computer throws Paper. You win!",vbinformation, "Winner"
        ElseIf Answer = "Scissors" and CardImage = "Scissors" Then
            MsgBox "Both throw scissors. It's a Draw!",vbinformation, "Draw"
	End If
	
NEXT

This post has been edited by modi123_1: 29 April 2013 - 09:35 PM
Reason for edit:: use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Rock Paper Scissors Code problem

#2 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 303
  • View blog
  • Posts: 1,797
  • Joined: 26-March 09

Re: Rock Paper Scissors Code problem

Posted 30 April 2013 - 12:15 AM

It would be better if you posted your code where you have tried to implement subs, then people can advise on where you went wrong.
Was This Post Helpful? 0
  • +
  • -

#3 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Rock Paper Scissors Code problem

Posted 02 May 2013 - 08:49 PM

This sort of thing is scary:
If Answer = "Rock" and CardImage = "Rock" Then    
			MsgBox "Both throw Rock. It's a Draw!",vbinformation, "Draw"
        ElseIf Answer = "Rock" and CardImage = "Paper" Then
            MsgBox "Computer throws Paper. Computer wins!",vbinformation, "Bad luck"
        ElseIf Answer = "Rock" and CardImage = "Scissors" Then
            MsgBox "Computer throws scissors. You win!",vbinformation, "Winner"
		ElseIf Answer = "Paper" and CardImage = "Rock" Then   
			MsgBox "Computer throws Rock. You win!",vbinformation, "Winner"
        ElseIf Answer = "Paper" and CardImage = "Paper" Then
            MsgBox "Both throw Paper. It's a Draw!",vbinformation, "Draw"
        ElseIf Answer = "Paper" and CardImage = "Scissors" Then
            MsgBox "Computer throws scissors. Computer wins!",vbinformation, "Bad luck" 
		ElseIf Answer = "Scissors" and CardImage = "Rock" Then   
			MsgBox "Computer throws Rock. Computer wins!",vbinformation, "Bad luck"
        ElseIf Answer = "Scissors" and CardImage = "Paper" Then
            MsgBox "Computer throws Paper. You win!",vbinformation, "Winner"
        ElseIf Answer = "Scissors" and CardImage = "Scissors" Then
            MsgBox "Both throw scissors. It's a Draw!",vbinformation, "Draw"
	End If
	

especially when you could do the basic calculation a lot more simply. First, use 0 to 2 rather than 1 to 3. Then make Answer one of these numbers rather than the strings. Now just do this:
Select Case Answer - ComputerThrow 
    Case 0
        'draw
    Case -1, 2
        'win
    Case Else
        'loss
End Select

Make your CardImage into a zero-based array. Remove the +1 from your random number, so the number will be 0, 1, or 2. Let the user enter a 0, 1 or 2. (Rather than using an InputBox, try putting a frame with an array of 3 option buttons in it. Use the Index of the selected option button to get your user's entry.)

See how far you can get with that, and post back if you have problems.

This post has been edited by BobRodes: 02 May 2013 - 08:51 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1