-
Nov 7th, 2013, 08:53 PM
#1
Thread Starter
Junior Member
Rock Paper Scissors
Evening all,
I have coded a simple rock, paper, scissors game in Visual Studio 2010. It seems to work properly, however, I cannot get any text to display in the label if the user or computer wins three matches. Any ideas what I could be doing wrong? Thanks for any advice you can offer.
Code:
Private Sub goButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles goButton.Click
Randomize()
Dim num As Integer ' variable to hold random number (computer choice)
Const ROCK = 1
Const PAPER = 2
Const SCISSORS = 3
num = CInt((3 * Rnd())) ' generate random numbers 1-3
Dim winCount As Integer = 0
Dim matchCount As Integer = 0
Dim compCount As Integer = 0
If rockRadioButton.Checked And num = ROCK Then
resultLabel.Text = "Computer chose Rock too. Tie!"
ElseIf rockRadioButton.Checked And num = PAPER Then
resultLabel.Text = "Computer chose paper. You Lose!"
compCount += 1
ElseIf rockRadioButton.Checked And num = SCISSORS Then
resultLabel.Text = "Computer threw scissors. You Win!"
winCount += 1
ElseIf paperRadioButton.Checked And num = ROCK Then
resultLabel.Text = "Computer chose Rock. You Win!"
winCount += 1
ElseIf paperRadioButton.Checked And num = PAPER Then
resultLabel.Text = "Computer chose paper too. Tie!"
ElseIf paperRadioButton.Checked And num = SCISSORS Then
resultLabel.Text = "Computer threw scissors. You Lose!"
compCount += 1
ElseIf scissorsRadioButton.Checked And num = ROCK Then
resultLabel.Text = "Computer chose Rock. You Lose!"
compCount += 1
ElseIf scissorsRadioButton.Checked And num = PAPER Then
resultLabel.Text = "Computer chose paper. You Win!"
winCount += 1
ElseIf scissorsRadioButton.Checked And num = SCISSORS Then
resultLabel.Text = "Computer chose scissors too. Tie!"
End If
'condition to check if you have won three times
If compCount = 2 Then
winLabel.Text = "Game over. Computer Wins. Play Again."
ElseIf winCount = 2 Then
winLabel.Text = "You won the best of 3! Congrats!"
End If
End Sub
-
Nov 7th, 2013, 09:15 PM
#2
Re: Rock Paper Scissors
Have you tried stepping through the code?
Edit:
You don't have a conditional statement checking for either 3 matches or greater then 2 matches.
Last edited by Nightwalker83; Nov 7th, 2013 at 09:21 PM.
Reason: Adding more!
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Nov 7th, 2013, 09:21 PM
#3
Addicted Member
Re: Rock Paper Scissors
Hi, the variables are reset to zero every time you click the button. If you change the "scope" of the variables they will retain their values. This also means after each win the count values will have to be reset to zero in your code.
This excerpt is module scope and will retain the count
Code:
Public Class Form1
Dim winCount As Integer = 0
Dim matchCount As Integer = 0
Dim compCount As Integer = 0
Private Sub goButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles goButton.Click
This excerpt will reset the counter
Code:
'condition to check if you have won three times
If compCount = 2 Then
win.Text = "Game over. Computer Wins. Play Again."
compCount = 0
winCount = 0
ElseIf winCount = 2 Then
win.Text = "You won the best of 3! Congrats!"
compCount = 0
winCount = 0
End If
-
Nov 7th, 2013, 09:21 PM
#4
Thread Starter
Junior Member
Re: Rock Paper Scissors
I did step through the code, but couldn't find any problems. I did hover over the > = in the final if statement and it said this represents a boolean value. I am not sure if that is causing the problem or not.
-
Nov 8th, 2013, 08:23 AM
#5
Re: Rock Paper Scissors
Sorry, perhaps besides the topic but where is Lizard and Spock. It should be : Rock, Paper, Scissors, Lizard, Spock...
VB.NET MVP 2008 - Present
-
Nov 8th, 2013, 09:10 AM
#6
Addicted Member
Re: Rock Paper Scissors
Sorry, perhaps besides the topic but where is Lizard and Spock. It should be : Rock, Paper, Scissors, Lizard, Spock...
That's the second time I have heard that this year, some TV program??
Back to the OP, you may also want to look a little closer at the way your random number is generated, I believe it generates 4 values (0-3) when you intended 3.
You may also find this site interesting http://www.homeandlearn.co.uk/NET/vbNet.html
-
Nov 8th, 2013, 09:44 AM
#7
Re: Rock Paper Scissors
Mc_VB has it right in #3. What you should have noticed when you stepped through it any time other than the first time is that the counters were always either 0 or 1. Local variables don't retain their values between calls to the method. If you were to look at the IL (which I don't actually suggest), you would see why that would be. The local variables are created in the stack frame for the method, so as soon as the method returns, they are obliterated.
One other point is that you shouldn't be using Randomize and Rnd, which are holdovers from VB6 that may or may not cause you trouble. In the present code, it is unlikely that they will cause trouble, but they could, since you could press the button twice in one second, which will produce some interesting results: The random number won't be random.
Instead, create a Random object at form scope and call the .Next method for that. It's easier than Randomize and Rnd anyways.
My usual boring signature: Nothing
-
Nov 8th, 2013, 10:20 AM
#8
Thread Starter
Junior Member
Re: Rock Paper Scissors
Thanks guys for the help! That makes sense about the local variable. I didn't even think of that. Also thanks to Shaggy Hiker for the tip on using a Random object..
-
Nov 8th, 2013, 12:31 PM
#9
Thread Starter
Junior Member
Re: Rock Paper Scissors
Would anyone have a suggestion as to how to implement the random object and call random numbers 1-3?
-
Nov 8th, 2013, 12:34 PM
#10
Re: Rock Paper Scissors
Create one instance of the Random object. You don't want more than that, but having one on the form is generally ok:
Private myRand As New Random
Then use that object wherever you want a random number. To get a random number from 1-3 you would call .Next:
myRand.Next(1,4)
Note that the lower bound is included, but the upper bound is not, so 1-3 requires .Next(1,4).
My usual boring signature: Nothing
-
Nov 8th, 2013, 12:40 PM
#11
Fanatic Member
Re: Rock Paper Scissors
msdn LINK
just go down the page to the second example to find what u need, or read through it to learn it all
Yes!!!
Working from home is so much better than working in an office...
Nothing can beat the combined stress of getting your work done on time whilst
1. one toddler keeps pressing your AVR's power button
2. one baby keeps crying for milk
3. one child keeps running in and out of the house screaming and shouting
4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
5. working at 1 O'clock in the morning because nobody is awake at that time
6. being grossly underpaid for all your hard work
-
Nov 8th, 2013, 01:28 PM
#12
Re: Rock Paper Scissors
Originally Posted by Mc_VB
That's the second time I have heard that this year, some TV program??
The Big Bang Theory
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Nov 8th, 2013, 02:25 PM
#13
Thread Starter
Junior Member
Re: Rock Paper Scissors
Originally Posted by Shaggy Hiker
Create one instance of the Random object. You don't want more than that, but having one on the form is generally ok:
Private myRand As New Random
Then use that object wherever you want a random number. To get a random number from 1-3 you would call .Next:
myRand.Next(1,4)
Note that the lower bound is included, but the upper bound is not, so 1-3 requires .Next(1,4).
After I created the Random object and called .next, it is throwing errors with the variables. Do I need to change the operator? VB is saying "the operator = is not defined for types System.Random and Integer."
Code:
Dim num As Random
num.Next(1, 4)
Const ROCK = 1
Const PAPER = 2
Const SCISSORS = 3
If rockRadioButton.Checked And num = ROCK Then
resultLabel.Text = "Computer chose Rock too. Tie!"
ElseIf rockRadioButton.Checked And num = PAPER Then
resultLabel.Text = "Computer chose paper. You Lose!"
compCount += 1
ElseIf rockRadioButton.Checked And num = SCISSORS Then
resultLabel.Text = "Computer threw scissors. You Win!"
winCount += 1
ElseIf paperRadioButton.Checked And num = ROCK Then
resultLabel.Text = "Computer chose Rock. You Win!"
winCount += 1
ElseIf paperRadioButton.Checked And num = PAPER Then
resultLabel.Text = "Computer chose paper too. Tie!"
ElseIf paperRadioButton.Checked And num = SCISSORS Then
resultLabel.Text = "Computer threw scissors. You Lose!"
compCount += 1
ElseIf scissorsRadioButton.Checked And num = ROCK Then
resultLabel.Text = "Computer chose Rock. You Lose!"
compCount += 1
ElseIf scissorsRadioButton.Checked And num = PAPER Then
resultLabel.Text = "Computer chose paper. You Win!"
winCount += 1
ElseIf scissorsRadioButton.Checked And num = SCISSORS Then
resultLabel.Text = "Computer chose scissors too. Tie!"
End If
-
Nov 8th, 2013, 04:35 PM
#14
Re: Rock Paper Scissors
The .Next method RETURNS an integer in that range, it doesn't become an integer. Also, you REALLY don't want to make the Random object a local variable, as you have done. Random number generators are never truly random. They all start from a seed value. Randomize seeds the generator for Rnd, and creating the Random object seeds the generator behind the Random object. The seed is the current system time down to the second. If you create two Random objects within a second of each other they contain the same seed value and will produce the exact same sequence of numbers. Nothing random about that. Therefore, you want to be sure to only create one Random object.
My usual boring signature: Nothing
-
Nov 8th, 2013, 04:51 PM
#15
Re: Rock Paper Scissors
@Op, I have a console application version of Rock Paper Scissors written in vb.net in the Game Demos section. You can find it pretty easily though by click on Game Contribution in my signature. You can't just copy and paste it because it's a console application and not a windows form application, but the concept is there which is what you really need to take a look at.
-
Nov 8th, 2013, 06:53 PM
#16
Thread Starter
Junior Member
Re: Rock Paper Scissors
Originally Posted by Shaggy Hiker
The .Next method RETURNS an integer in that range, it doesn't become an integer. Also, you REALLY don't want to make the Random object a local variable, as you have done. Random number generators are never truly random. They all start from a seed value. Randomize seeds the generator for Rnd, and creating the Random object seeds the generator behind the Random object. The seed is the current system time down to the second. If you create two Random objects within a second of each other they contain the same seed value and will produce the exact same sequence of numbers. Nothing random about that. Therefore, you want to be sure to only create one Random object.
should I declare as decimal instead of integer?
-
Nov 8th, 2013, 07:09 PM
#17
Re: Rock Paper Scissors
Declare what? What you want to do is something like this:
At form scope:
Private myRand As New Random
Inside the method:
Dim num As Integer = myRand.Next(1,4)
My usual boring signature: Nothing
-
Nov 9th, 2013, 09:32 AM
#18
Thread Starter
Junior Member
Re: Rock Paper Scissors
Originally Posted by Shaggy Hiker
Declare what? What you want to do is something like this:
At form scope:
Private myRand As New Random
Inside the method:
Dim num As Integer = myRand.Next(1,4)
Thank you. I wasn't declaring it at form level and it wouldn't let me set it as private. Works good now. Thanks again!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|