Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Need my accumulator to work outside form iterations

teahouteahou Posts: 2Member
[size=3]Hi there. I am learning VB6 and am trying to make a 'rock, paper' scissors' game. I have the game working fine, but I wanted to add a score tracker to it, using an accumulator. The accumulator works for one game, and the score is displayed in the label.text control, but when a new game is played the score does not accumulate. Also, if there is a tie, the scores both reset to 0. The homework assignment only covers making the game, which is done. i want the score tracker for extra credit. Any hints would be greatly appreciated.

Here is the code:


Public Class frmRockPaperScissors
'Class level variables
Dim intSelection As Integer 'Computers selection
Dim strArray() = {"Rock", "Paper", "Scissors"} 'Array


Private Sub btnFight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFight.Click
Dim strWinner As String = ""
Dim intCpuScore As Integer
Dim intUserScore As Integer
Dim rand As New Random

'Select random index
intSelection = rand.Next(3)

'Clear the game board
picRock.Visible = False
picPaper.Visible = False
picScissors.Visible = False


'Display computers selection
Select Case intSelection
Case 0
picRock.Visible = True
Case 1
picPaper.Visible = True
Case 2
picScissors.Visible = True
End Select


'Choose a winner
If intSelection = 0 And UserChoice() = 1 Then
strWinner = "user"
ElseIf intSelection = 0 And UserChoice() = 2 Then
strWinner = "cpu"
ElseIf intSelection = 1 And UserChoice() = 0 Then
strWinner = "cpu"
ElseIf intSelection = 1 And UserChoice() = 2 Then
strWinner = "user"
ElseIf intSelection = 2 And UserChoice() = 0 Then
strWinner = "user"
ElseIf intSelection = 2 And UserChoice() = 1 Then
strWinner = "cpu"
Else
strWinner = ""

End If

'Keep score
If strWinner = "cpu" Then
intCpuScore += 1
MessageBox.Show("You have been defeated by the computers " &
strArray(intSelection))

ElseIf strWinner = "user" Then
intUserScore += 1
MessageBox.Show("You have defeated the computers " &
strArray(intSelection))
Else
intUserScore += 0
MessageBox.Show("Its a tie")
End If

'Display the score
lblCpuWins.Text = intCpuScore.ToString()
lblUserWins.Text = intUserScore.ToString()

End Sub


Private Function UserChoice() As Integer
Dim intUserChoice As Integer = 0

'Get users selection
If radRock.Checked = True Then
intUserChoice = 0
ElseIf radPaper.Checked = True Then
intUserChoice = 1
ElseIf radScissors.Checked = True Then
intUserChoice = 2
Else
MessageBox.Show("Make a selection.")
End If
Return intUserChoice
End Function

End Class
[/size]
Sign In or Register to comment.