Results 1 to 13 of 13

Thread: [RESOLVED] I need help with a score problem in my vb 2008 game

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    11

    Resolved [RESOLVED] I need help with a score problem in my vb 2008 game

    Hi guys, i am making a guess the number game, i have all that down good, now i am adding a score system. I thought i had it good, but it takes away all the points i put in for the different difficulties.
    like, if you get the number on the easy level you get 1 point
    Points:
    easy = 1
    medium = 5
    hard = 10
    impossible = 100

    and if you get it wrong it takes away 1 point

    but when i get it wrong, it takes away 5 points, and if i get it right i get 116 points

    please help me.

    Code:
     Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim random As New Random
            ' Easy difficulty
            If RadioButton1.Checked = True Then
                Label1.Text = random.Next(1, 10)
            End If
            If TextBox1.Text = Label1.Text Then
                'add score
                score.Text += 1
                '
                Label2.Visible = True
                Label3.Visible = True
                Label3.Text = "You are CORRECT!"
                Button1.Enabled = False
                TextBox1.Enabled = False
            Else : Label2.Visible = True
                ' minus score
                score.Text -= 2
                '
                Label3.Visible = True
                Label3.Text = "You are WRONG!"
                Button1.Enabled = False
                TextBox1.Enabled = False
            End If
            ' Medium difficulty
            If RadioButton2.Checked = True Then
                Label1.Text = random.Next(1, 50)
            End If
            If TextBox1.Text = Label1.Text Then
                'add score
                score.Text += 5
                '
                Label2.Visible = True
                Label3.Visible = True
                Label3.Text = "You are CORRECT!"
                Button1.Enabled = False
                TextBox1.Enabled = False
            Else : Label2.Visible = True
                ' minus score
                score.Text -= 1
                '
                Label3.Visible = True
                Label3.Text = "You are WRONG!"
                Button1.Enabled = False
                TextBox1.Enabled = False
            End If
            ' Hard difficulty
            If RadioButton3.Checked = True Then
                Label1.Text = random.Next(1, 100)
            End If
            If TextBox1.Text = Label1.Text Then
                'add score
                score.Text += 10
                '
                Label2.Visible = True
                Label3.Visible = True
                Label3.Text = "You are CORRECT!"
                Button1.Enabled = False
                TextBox1.Enabled = False
            Else : Label2.Visible = True
                ' minus score
                score.Text -= 1
                '
                Label3.Visible = True
                Label3.Text = "You are Wrong!"
                Button1.Enabled = False
                TextBox1.Enabled = False
            End If
            ' Impossible difficulty
            If RadioButton4.Checked = True Then
                Label1.Text = random.Next(1, 10000)
                Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 36.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
            End If
            If TextBox1.Text = Label1.Text Then
                'add score
                score.Text += 100
                '
                Label2.Visible = True
                Label3.Visible = True
                Label3.Text = "You are CORRECT!"
                Button1.Enabled = False
                TextBox1.Enabled = False
            Else : Label2.Visible = True
                ' minus score
                score.Text -= 1
                '
                Label3.Visible = True
                Label3.Text = "You are WRONG!"
                Button1.Enabled = False
                TextBox1.Enabled = False
            End If
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Button1.Enabled = True
            TextBox1.Enabled = True
            Label2.Visible = False
            Label3.Visible = False
            Label1.Text = "0"
            TextBox1.Text = ""
            Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 72.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        End Sub
    
        Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
            Process.Start("http://cyked.ucoz.com")
        End Sub
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            AboutBox1.Show()
        End Sub
    End Class

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: I need help with a score problem in my vb 2008 game

    Its a bit difficult working out the logic of your code when all your controls have names like text1 and label3 - its a good idea to give them meaningful names, and adding a blank line here or there to visually split out parts of the code is a good idea too.

    The first thing I can see without even looking at your code is that the points you are getting awarded is the sum of the points available for the different levels, ie you seem to be getting easy + medium + hard + impossible, so thats the first thing I would look for : is your code running all four "correct" blocks somehow.

    Also note you are incrementing text values. This works but it is bad practice and if you had Option Strict set to On (which I'd recommend) it would be giving you errors. Things like "Score.text -=2" are logically incorrect.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    11

    Re: I need help with a score problem in my vb 2008 game

    Oh ok, i tried setting the score as an integer, but then i could not get it to display.
    I will name all the code, just give me a minute

  4. #4
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: I need help with a score problem in my vb 2008 game

    I think this is your actual problem - you have got your End If statement in the wrong place after checking the difficulty level.

    Code:
            ' Easy difficulty
            If RadioButton1.Checked = True Then
                Label1.Text = random.Next(1, 10)
            End If
    This End If should come at the end of the block that handles the points like this :

    Code:
            ' Easy difficulty
            If RadioButton1.Checked = True Then
                Label1.Text = random.Next(1, 10)
          
                If TextBox1.Text = Label1.Text Then
                    'add score
                    score.Text += 1
                    '
                    Label2.Visible = True
                    Label3.Visible = True
                    Label3.Text = "You are CORRECT!"
                    Button1.Enabled = False
                    TextBox1.Enabled = False
                Else : Label2.Visible = True
                    ' minus score
                    score.Text -= 2
                    '
                    Label3.Visible = True
                    Label3.Text = "You are WRONG!"
                    Button1.Enabled = False
                    TextBox1.Enabled = False
                End If
            End If
    Because your If..End If is only wrapped around the bit that generates the number to guess and not the points and doesn't wrap the point scoring, it means that the point scoring for each difficulty gets run each time, hence it is accumulating the correct or incorrect scores for each level every time.

    Obviously you need to correct this for all four blocks - I've just shown one example above.
    Last edited by keystone_paul; Jul 19th, 2009 at 03:53 AM.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    11

    Re: I need help with a score problem in my vb 2008 game

    Ok i fixed it,
    Code:
     Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GuessButton.Click
            Dim random As New Random
            ' Easy difficulty
            If RadioEasy.Checked = True Then
                randomnumber.Text = random.Next(1, 10)
            End If
            If PlayerGuess.Text = randomnumber.Text Then
                'add score
                score.Text += 1
                '
                numberwas.Visible = True
                WrongRight.Visible = True
                WrongRight.Text = "You are CORRECT!"
                GuessButton.Enabled = False
                PlayerGuess.Enabled = False
            Else : numberwas.Visible = True
                ' minus score
                score.Text -= 2
                '
                WrongRight.Visible = True
                WrongRight.Text = "You are WRONG!"
                GuessButton.Enabled = False
                PlayerGuess.Enabled = False
            End If
            ' Medium difficulty
            If RadioMedium.Checked = True Then
                randomnumber.Text = random.Next(1, 50)
            End If
            If PlayerGuess.Text = randomnumber.Text Then
                'add score
                score.Text += 5
                '
                numberwas.Visible = True
                WrongRight.Visible = True
                WrongRight.Text = "You are CORRECT!"
                GuessButton.Enabled = False
                PlayerGuess.Enabled = False
            Else : numberwas.Visible = True
                ' minus score
                score.Text -= 1
                '
                WrongRight.Visible = True
                WrongRight.Text = "You are WRONG!"
                GuessButton.Enabled = False
                PlayerGuess.Enabled = False
            End If
            ' Hard difficulty
            If RadioHard.Checked = True Then
                randomnumber.Text = random.Next(1, 100)
            End If
            If PlayerGuess.Text = randomnumber.Text Then
                'add score
                score.Text += 10
                '
                numberwas.Visible = True
                WrongRight.Visible = True
                WrongRight.Text = "You are CORRECT!"
                GuessButton.Enabled = False
                PlayerGuess.Enabled = False
            Else : numberwas.Visible = True
                ' minus score
                score.Text -= 1
                '
                WrongRight.Visible = True
                WrongRight.Text = "You are Wrong!"
                GuessButton.Enabled = False
                PlayerGuess.Enabled = False
            End If
            ' Impossible difficulty
            If RadioImpossible.Checked = True Then
                randomnumber.Text = random.Next(1, 10000)
                Me.randomnumber.Font = New System.Drawing.Font("Microsoft Sans Serif", 36.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
            End If
            If PlayerGuess.Text = randomnumber.Text Then
                'add score
                score.Text += 100
                '
                numberwas.Visible = True
                WrongRight.Visible = True
                WrongRight.Text = "You are CORRECT!"
                GuessButton.Enabled = False
                PlayerGuess.Enabled = False
            Else : numberwas.Visible = True
                ' minus score
                score.Text -= 1
                '
                WrongRight.Visible = True
                WrongRight.Text = "You are WRONG!"
                GuessButton.Enabled = False
                PlayerGuess.Enabled = False
            End If
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ResetButton.Click
            GuessButton.Enabled = True
            PlayerGuess.Enabled = True
            numberwas.Visible = False
            WrongRight.Visible = False
            randomnumber.Text = "0"
            PlayerGuess.Text = ""
            Me.randomnumber.Font = New System.Drawing.Font("Microsoft Sans Serif", 72.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        End Sub
    
        Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
            Process.Start("http://cyked.ucoz.com")
        End Sub
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click, HelpButton.Click
            AboutBox1.Show()
        End Sub
    End Class

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    11

    Re: I need help with a score problem in my vb 2008 game

    Oh thanks! I will test that now

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    11

    Re: I need help with a score problem in my vb 2008 game

    Thank you, it worked!

  8. #8
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: I need help with a score problem in my vb 2008 game

    In order to get around the problem of using text to hold the scores you want to declare a local-level integer variable to hold the score. You then increment/decrement this with the score each time and then set the Score.Text = the score variable.

    Incidentally it took me a while to get my head round your logic because for a number guessing game you would usually have the game and pick a number and then the user tries to guess it - in your code its more of a "predicting" game because the user picks a number and then the computer picks one and you win if you correctly predicted it!

  9. #9

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    11

    Re: I need help with a score problem in my vb 2008 game

    I will attempt that.
    Wait, how do i do that lol, im still new to vb, when i tried
    Code:
     Dim score as integer = 10
    it didnt work
    and when i tried
    Code:
     Dim score as new integer
    i didnt know how to add the score

  10. #10
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: I need help with a score problem in my vb 2008 game

    You need

    Code:
    Dim currentscore as integer
    And place that between the 1st line of code ("Public Class Form1") and the start of your button click event. Variables declared there will have local scope (ie will be visible and accessible in all procedures within the class).

    If you had "Dim currentscore as integer = 10" then the user would start the game with a score of 10.

    Then when it comes to your scoring code, you want something like :

    Code:
    ' Easy difficulty
        If RadioEasy.Checked = True Then
            
            randomnumber.Text = random.Next(1, 10)
           
            If PlayerGuess.Text = randomnumber.Text Then
                'add score
                currentscore += 1
                '
                numberwas.Visible = True
                WrongRight.Visible = True
                WrongRight.Text = "You are CORRECT!"
                GuessButton.Enabled = False
                PlayerGuess.Enabled = False
            Else : numberwas.Visible = True
                ' minus score
                currentscore -= 2
                '
                WrongRight.Visible = True
                WrongRight.Text = "You are WRONG!"
                GuessButton.Enabled = False
                PlayerGuess.Enabled = False
            End If
    
            score.text = currentscore
        End If
    Although another note on good practice, having a textbox called score and a variable called score is not a good idea (if it works at all), likewise having a variable called "random" of type "Random" is going to lead to problems (especially as VB.Net is case insensitive)

  11. #11

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    11

    Re: I need help with a score problem in my vb 2008 game

    Thank you for all your help, sorry for the delay in this last post, i was having dinner.

  12. #12
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: I need help with a score problem in my vb 2008 game

    No problem - but if your problem is fixed now please remember to mark the thread resolved using the thread tools menu at the top of the thread.

  13. #13

    Thread Starter
    New Member
    Join Date
    Jul 2009
    Posts
    11

    Re: [RESOLVED] I need help with a score problem in my vb 2008 game

    Done

Posting Permissions

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



Click Here to Expand Forum to Full Width