הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Friday, November 8, 2013 2:16 AM
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.
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
All replies (2)
Friday, November 8, 2013 2:21 AM ✅Answered | 1 vote
This declaration
Dim compCount As Integer = 0
will set the variable to zero each time the button click event is executed. You need to either
- move that declaration out of the method and into the form level, or
- use a static variable - http://msdn.microsoft.com/en-us/library/z2cty7t8(v=vs.110).aspx
The same problem applies to other variables that need to retain their values between button clicks.
Friday, November 8, 2013 3:15 PM
Thank yo so much! That makes perfect sense!