JustARogueCoder
JustARogueCoder

Reputation: 27

VB.NET Score Not Adding

I'm making a rock paper scissors game in visual basic and the basics are the player choice is determined by a radio button of either rock paper or scissors and the computer's choice is based on a random number between 1 - 3 rock is 3 paper is 2 scissors is 1. When the play button is clicked it runs an if statement to check if the rock button has been checked and the random int is equal to rock in which case its a draw but if you choose rock and the random int Val is scissors then you win and it adds 1 to your score

The problem is the score doesn't go beyond 1 and I don't understand

Heres the code:

Public Class gameForm
    Private Sub btnPlay_Click(sender As Object, e As EventArgs) Handles btnPlay.Click
        Try
            Dim playerscore = 0
            Dim comscore = 0
            Dim rock As Integer = 3
            Dim paper As Integer = 2
            Dim scissors As Integer = 1
            Dim number As Integer = CInt(Int((3 * Rnd()) + 1))

            Randomize()

            If rbRock.Checked And number = rock Then
                lblPlayerChoice.Text = "You Chose Rock"
                lblPcChoice.Text = "Computer Chose Rock"
                lblGameOutcome.Text = "Draw"
            ElseIf rbRock.Checked And number = paper Then
                lblPlayerChoice.Text = "You Chose Rock"
                lblPcChoice.Text = "Computer Chose Paper"
                lblGameOutcome.Text = "Computer Wins"
                comscore = comscore + 1
                lblComScoreVal.Text = CStr(comscore)
            ElseIf rbRock.Checked And number = scissors Then
                lblPlayerChoice.Text = "You Chose Rock"
                lblPcChoice.Text = "Computer Chose Scissors"
                lblGameOutcome.Text = "You Win"
                playerscore = playerscore + 1
                lblPlayerScoreVal.Text = CStr(playerscore)
            End If

        Catch ex As Exception

            MessageBox.Show("Unknown Error Occurred", "Error", MessageBoxButtons.OK)

        End Try


    End Sub


End Class

Upvotes: 0

Views: 49

Answers (2)

Greg the Incredulous
Greg the Incredulous

Reputation: 1846

You're initialising a value for the player's score to zero when you click the button, rather than storing it somewhere else. Every time the button is clicked the score is set to zero and then the calculation is applied. Instead of setting to zero, you should set it to either the value of lblComScoreVal.text, or zero if that is not set to anything.

Upvotes: 2

Rich K.
Rich K.

Reputation: 74

You have to bring that playerscore and comscore out of your Sub btnPlay_Click ...

it should something like this

Public Class gameForm
    Private playerscore as Integer = 0
    Private comscore as Integer = 0

    Private Sub btnPlay_Click(sender As Object, e As EventArgs) Handles btnPlay.Click
        Try

Upvotes: 0

Related Questions