# Thread: Rock Paper Scissors Problem

1. ## Rock Paper Scissors Problem

For some reason Rock ( button1 ) and paper ( button2 ) only display "You Win" and "Its a Draw" However scissors ( button3 ) Displays All 3 "You win", "You Lose" and "Its A Draw, I hope someone can help me find a solution ~David

Public Class Form1
Dim counter As Integer

Dim rock As Integer = 1
Dim paper As Integer = 2
Dim scissors As Integer = 3

Dim userValue As Integer

'rock beats scissors-paper beats rock-scissors beats paper

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
counter = 1

If counter = 1 Then
userValue = rock
End If

Randomize()

Dim computerValue As Integer = CInt(Int((3 * Rnd()) + 1))
If userValue = computerValue Then
Label1.Text = "Its a Draw"
ElseIf userValue = 1 And computerValue = 3 Then
Label1.Text = "You Win"
ElseIf computerValue = 3 And userValue = 1 Then
Label1.Text = "You lose"
End If

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
counter = 2

If counter = 2 Then
userValue = paper
End If

Randomize()

Dim computerValue As Integer = CInt(Int((3 * Rnd()) + 1))
If userValue = computerValue Then
Label1.Text = "Its a Draw"
ElseIf userValue = 2 And computerValue = 1 Then
Label1.Text = "You Win"
ElseIf computerValue = 2 And userValue = 1 Then
Label1.Text = "You lose"
End If

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
counter = 3

If counter = 3 Then
userValue = scissors
End If

Randomize()

Dim computerValue As Integer = CInt(Int((3 * Rnd()) + 1))
If userValue = computerValue Then
Label1.Text = "Its a Draw"
ElseIf userValue = 3 And computerValue = 2 Then
Label1.Text = "You Win"
ElseIf computerValue = 1 And userValue = 3 Then
Label1.Text = "You lose"
End If

End Sub
End Class

2. ## Re: Rock Paper Scissors Problem

You need to learn to debug your code. Rather than just reading it, watch it in action. Place a breakpoint at the top of the code and then step through it line by line, viewing that values of your variables and other relevant expressions. That way, you can see exactly where what actually happens differs from what you expect to happen. If you still can't work out how to fix it then post back but at least you can tell us EXACTLY where the issue is.

3. ## Re: Rock Paper Scissors Problem

In button 1 you have this:-
ElseIf userValue = 1 And computerValue = 3 Then
Label1.Text = "You Win"
ElseIf computerValue = 3 And userValue = 1 Then
Label1.Text = "You lose"
Both of those if are actually the same, you've just got the elements in a different order.

In button 2 you have this:-
ElseIf computerValue = 2 And userValue = 1 Then
...which will never be true because you've just set userValue to 2

As a general comment, you've got way more code than you need, too. You don't need the counter variable and you don't need the user variable. Each button should just do something like this (using pseudo code)

Code:
Call Randomize to reseed the random number generator
Set ComputerVal = a random number from 1 to 3
Select Case ComputerVal
case 1 : Return Win/Lose/Draw
case 2 : Return Win/Lose/Draw
case 3 : Return Win/Lose/Draw
End Case
You don't need to stire userVal in a variable, you know what user selection you're dealing with because you're writing the event handler for that selection.

4. ## Re: Rock Paper Scissors Problem

try this:

Code:
Public Class Form1
Dim rock As Integer = 1
Dim paper As Integer = 2
Dim scissors As Integer = 3

Dim userValue As Integer

Dim r As New Random

'rock beats scissors-paper beats rock-scissors beats paper

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
userValue = rock

Dim computerValue As Integer = r.Next(1, 4)
If userValue = computerValue Then
Label1.Text = "Its a Draw"
ElseIf computerValue = 2 Then
Label1.Text = "You Win"
ElseIf computerValue = 3 Then
Label1.Text = "You lose"
End If

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
userValue = paper

Dim computerValue As Integer = r.Next(1, 4)
If userValue = computerValue Then
Label1.Text = "Its a Draw"
ElseIf computerValue = 3 Then
Label1.Text = "You Win"
ElseIf computerValue = 1 Then
Label1.Text = "You lose"
End If

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
userValue = scissors

Dim computerValue As Integer = r.Next(1, 4)
If userValue = computerValue Then
Label1.Text = "Its a Draw"
ElseIf computerValue = 1 Then
Label1.Text = "You Win"
ElseIf computerValue = 2 Then
Label1.Text = "You lose"
End If

End Sub

End Class

5. ## Re: Rock Paper Scissors Problem

I've created a function for you @123Robo, which is similar to @.paul.'s code. It's even easier to use, and can be easily edited. I coded this as a practice, as I've yet not created a Rock, paper, scissor game. I thought I could share it with you and the rest of the community

Code:
Public Shared Function Rock_Paper_Scissors(ByVal [UserInput] As String) 'Call this function with either: "rock", "paper" or "scissor". Caps lock doesn't matter. Will result an error string if not one of these.

Dim Result As String = Nothing 'The result to be shown
Dim Possibilities() As String = {"rock".ToLower, "paper".ToLower, "scissor".ToLower} 'Holds the rock paper scissor variables
[UserInput] = [UserInput].ToLower

Dim WinMessage As String = "You win!" 'Displays selected text on win.
Dim LoseMessage As String = "You lose!" 'Displays selected text on lose.
Dim DrawMessage As String = "It's a draw!" 'Displays selected text on draw.
Dim ErrorMessage As String = "Invalid userinput" 'Displays selected text if the userinput is wrong

Dim newRandom As New Random
Dim ComputerMove As String = Possibilities(newRandom.Next(0, 3)) 'Creates a random integer which is either 0, 1 or 2.

If ComputerMove = [UserInput] Then 'Checks for possibilites
Result = DrawMessage 'Same as computer
Else
If [UserInput] = Possibilities(0) And ComputerMove = Possibilities(1) Then
Result = LoseMessage 'User: Rock, PC: Paper
ElseIf [UserInput] = Possibilities(0) And ComputerMove = Possibilities(2) Then
Result = WinMessage 'User: Rock, PC: Scissor
ElseIf [UserInput] = Possibilities(1) And ComputerMove = Possibilities(0) Then
Result = WinMessage 'User: Paper, PC: Rock
ElseIf [UserInput] = Possibilities(1) And ComputerMove = Possibilities(2) Then
Result = LoseMessage 'User: Paper, PC: Scissor
ElseIf [UserInput] = Possibilities(2) And ComputerMove = Possibilities(0) Then
Result = LoseMessage 'User: Scissor, PC: Rock
ElseIf [UserInput] = Possibilities(2) And ComputerMove = Possibilities(1) Then
Result = WinMessage 'User: Scissor, PC: Paper
Else
Result = ErrorMessage 'Userinput did not match any of the possibilities
End If
End If

Return Result 'Returns the value back to caller

'Code created by 'Frekvens1' for '123Robo' on VB Forums.

End Function
How do you run this code?

Code:
Label1.text = Rock_Paper_Scissors(YourVal) 'Replace 'YourVal' with either "rock", "paper" or "scissor". Upper letter works too. If another input is given, it will result in a error message. Which your label will display!
I've also commented what the different lines of codes do, so you can see how the function works. I hope this solves your problem!

- Frek

6. ## Re: Rock Paper Scissors Problem

You can just use pseudorandom number generator to reflect the real life. {Mersenne Twister}

8. ## Re: Rock Paper Scissors Problem

Good Catch.

123Robo, try not to start duplicate threads. It actually lessens the chance that you'll get a solution because the information gets muddled across two places. I've deleted your other thread because it contained nothing useful that hasn't been repeated in this thread.

9. ## Re: Rock Paper Scissors Problem

Originally Posted by FunkyDexter
Good Catch.
it contained nothing useful that hasn't been repeated in this thread.
I thought i had some valid bullet points

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Featured