Results 1 to 3 of 3

Thread: VB Game one highscore

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2016
    Posts
    11

    Question VB Game one highscore

    So I have my basic slider box game that tracks moves with every button click.
    Code:
    Public Class Form1
        Dim iSpace As Integer
        Dim iMoves As Integer = 0
        Sub CheckWin()
            If Button1.Text <> "1" Then
            ElseIf Button2.Text <> "2" Then
            ElseIf Button3.Text <> "3" Then
            ElseIf Button4.Text <> "4" Then
            ElseIf Button5.Text <> "5" Then
            ElseIf Button6.Text <> "6" Then
            ElseIf Button7.Text <> "7" Then
            ElseIf Button8.Text <> "8" Then
            ElseIf Button9.Text <> "9" Then
            ElseIf Button10.Text <> "10" Then
            ElseIf Button11.Text <> "11" Then
            ElseIf Button12.Text <> "12" Then
            ElseIf Button13.Text <> "13" Then
            ElseIf Button14.Text <> "14" Then
            ElseIf Button15.Text <> "15" Then
            Else : Me.Text = "Winner winner chicken dinner!"
            End If
        End Sub
        Sub Exchange(ByVal AButton As Button, ByVal BButton As Button, ByVal iPiece As Integer)
            AButton.Text = BButton.Text
            AButton.Visible = True
            BButton.Visible = False
            iSpace = iPiece
            CheckWin()
        End Sub
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            iSpace = 2
            Button2.Visible = False
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Select Case iSpace
                Case 2
                    Exchange(Button2, Button1, 1)
                Case 5
                    Exchange(Button5, Button1, 1)
                Case Else
                    Me.Text = "Nah fam!"
            End Select
            iMoves = iMoves + 1
            LblMoves.Text = iMoves
        End Sub
    
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            Select Case iSpace
                Case 1
                    Exchange(Button1, Button2, 2)
                Case 3
                    Exchange(Button3, Button2, 2)
                Case 6
                    Exchange(Button6, Button2, 2)
                Case Else
                    Me.Text = "Nah fam!"
            End Select
            iMoves = iMoves + 1
            LblMoves.Text = iMoves
        End Sub
    
        Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
            Select Case iSpace
                Case 2
                    Exchange(Button2, Button3, 3)
                Case 4
                    Exchange(Button4, Button3, 3)
                Case 7
                    Exchange(Button7, Button3, 3)
                Case Else
                    Me.Text = "Nah fam!"
            End Select
            iMoves = iMoves + 1
            LblMoves.Text = iMoves
        End Sub
    
        Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
            Select Case iSpace
                Case 3
                    Exchange(Button3, Button4, 4)
                Case 8
                    Exchange(Button8, Button4, 4)
                Case Else
                    Me.Text = "Nah fam!"
            End Select
            iMoves = iMoves + 1
            LblMoves.Text = iMoves
        End Sub
    
        Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
            Select Case iSpace
                Case 1
                    Exchange(Button1, Button5, 5)
                Case 6
                    Exchange(Button6, Button5, 5)
                Case 9
                    Exchange(Button9, Button5, 5)
                Case Else
                    Me.Text = "Nah fam!"
            End Select
            iMoves = iMoves + 1
            LblMoves.Text = iMoves
        End Sub
    
        Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click
            Select Case iSpace
                Case 2
                    Exchange(Button2, Button6, 6)
                Case 5
                    Exchange(Button5, Button6, 6)
                Case 7
                    Exchange(Button7, Button6, 6)
                Case 10
                    Exchange(Button10, Button6, 6)
                Case Else
                    Me.Text = "Nah fam!"
            End Select
            iMoves = iMoves + 1
            LblMoves.Text = iMoves
        End Sub
    
        Private Sub Button7_Click(sender As System.Object, e As System.EventArgs) Handles Button7.Click
            Select Case iSpace
                Case 3
                    Exchange(Button3, Button7, 7)
                Case 6
                    Exchange(Button6, Button7, 7)
                Case 8
                    Exchange(Button8, Button7, 7)
                Case 11
                    Exchange(Button11, Button7, 7)
                Case Else
                    Me.Text = "Nah fam!"
            End Select
            iMoves = iMoves + 1
            LblMoves.Text = iMoves
        End Sub
    
        Private Sub Button8_Click(sender As System.Object, e As System.EventArgs) Handles Button8.Click
            Select Case iSpace
                Case 4
                    Exchange(Button4, Button8, 8)
                Case 7
                    Exchange(Button7, Button8, 8)
                Case 12
                    Exchange(Button12, Button8, 8)
                Case Else
                    Me.Text = "Nah fam!"
            End Select
            iMoves = iMoves + 1
            LblMoves.Text = iMoves
        End Sub
    
        Private Sub Button9_Click(sender As System.Object, e As System.EventArgs) Handles Button9.Click
            Select Case iSpace
                Case 5
                    Exchange(Button5, Button9, 9)
                Case 10
                    Exchange(Button10, Button9, 9)
                Case 13
                    Exchange(Button13, Button9, 9)
                Case Else
                    Me.Text = "Nah fam!"
            End Select
            iMoves = iMoves + 1
            LblMoves.Text = iMoves
        End Sub
    
        Private Sub Button10_Click(sender As System.Object, e As System.EventArgs) Handles Button10.Click
            Select Case iSpace
                Case 6
                    Exchange(Button6, Button10, 10)
                Case 9
                    Exchange(Button9, Button10, 10)
                Case 11
                    Exchange(Button11, Button10, 10)
                Case 14
                    Exchange(Button14, Button10, 10)
                Case Else
                    Me.Text = "Nah fam!"
            End Select
            iMoves = iMoves + 1
            LblMoves.Text = iMoves
        End Sub
    
        Private Sub Button11_Click(sender As System.Object, e As System.EventArgs) Handles Button11.Click
            Select Case iSpace
                Case 7
                    Exchange(Button7, Button11, 11)
                Case 10
                    Exchange(Button10, Button11, 11)
                Case 12
                    Exchange(Button12, Button11, 11)
                Case 15
                    Exchange(Button15, Button11, 11)
                Case Else
                    Me.Text = "Nah fam!"
            End Select
            iMoves = iMoves + 1
            LblMoves.Text = iMoves
        End Sub
    
        Private Sub Button12_Click(sender As System.Object, e As System.EventArgs) Handles Button12.Click
            Select Case iSpace
                Case 8
                    Exchange(Button8, Button12, 12)
                Case 11
                    Exchange(Button11, Button12, 12)
                Case 16
                    Exchange(Button16, Button12, 12)
                Case Else
                    Me.Text = "Nah fam!"
            End Select
            iMoves = iMoves + 1
            LblMoves.Text = iMoves
        End Sub
    
        Private Sub Button13_Click(sender As System.Object, e As System.EventArgs) Handles Button13.Click
            Select Case iSpace
                Case 9
                    Exchange(Button9, Button13, 13)
                Case 14
                    Exchange(Button14, Button13, 13)
                Case Else
                    Me.Text = "Nah fam!"
            End Select
            iMoves = iMoves + 1
            LblMoves.Text = iMoves
        End Sub
    
        Private Sub Button14_Click(sender As System.Object, e As System.EventArgs) Handles Button14.Click
            Select Case iSpace
                Case 10
                    Exchange(Button10, Button14, 14)
                Case 13
                    Exchange(Button13, Button14, 14)
                Case 15
                    Exchange(Button15, Button14, 14)
                Case Else
                    Me.Text = "Nah fam!"
            End Select
            iMoves = iMoves + 1
            LblMoves.Text = iMoves
        End Sub
    
        Private Sub Button15_Click(sender As System.Object, e As System.EventArgs) Handles Button15.Click
            Select Case iSpace
                Case 11
                    Exchange(Button11, Button15, 15)
                Case 14
                    Exchange(Button14, Button15, 15)
                Case 16
                    Exchange(Button16, Button15, 15)
                Case Else
                    Me.Text = "Nah fam!"
            End Select
            iMoves = iMoves + 1
            LblMoves.Text = iMoves
        End Sub
    
        Private Sub Button16_Click(sender As System.Object, e As System.EventArgs) Handles Button16.Click
            Select Case iSpace
                Case 12
                    Exchange(Button12, Button16, 16)
                Case 15
                    Exchange(Button15, Button16, 16)
                Case Else
                    Me.Text = "Nah fam!"
            End Select
            iMoves = iMoves + 1
            LblMoves.Text = iMoves
        End Sub
        
    End Class
    Now, what I need to do is add a highscore feature that keeps track of the lowest number of moves used to win, I want the integer written to a Label (lblHigh). I know the easiest way is to use a StreamWriter and StreamReader but I'm having trouble on where to start. If anyone could figure out how to keep track of the lowest moves used to win as an integer in LblHigh(label), that'd be great. Thanks.

  2. #2
    Hyperactive Member Vexslasher's Avatar
    Join Date
    Feb 2010
    Posts
    429

    Re: VB Game one highscore

    You can save the integer with a setting. To add a setting click "Project" at the top then at bottom of that drop down click "Properties" in the page that comes up in left side click the one that says "Settings" you should see a setting come up rename it to HighScore and change the type to Integer, and keep scope as User.

    This is how you load a stored saved setting.
    Code:
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim CurrentHighScore As Integer = My.Settings.HighScore
        End Sub

    This is how you change/replace/save the setting with a new value. The if commands here make it only add the new score if it was the new best score.

    Code:
                    Dim CurrentHighScore As Integer = My.Settings.HighScore
            If iMoves > CurrentHighScore Then
                My.Settings.HighScore = iMoves
                My.Settings.Save()
            End If)

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: VB Game one highscore

    I would like to point out that you could simply your large If statement to this:
    Code:
    'Create a collection of all the buttons that you want to test
    Dim buttons() As Button = {Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8, Button9, Button10, Button11, Button12, Button13, Button14, Button15}
    
    'If you want all the buttons on the form, this can be further simplified to:
    'Dim buttons As IEnumerable(Of Button) = Me.Controls.OfType(Of Button)()
    
    'Check the number of buttons in the collection where it's name(minus the word Button) equals its respective Text
    If (From btn As Button In buttons Where btn.Name.Replace("Button", String.Empty) = btn.Text).Count = 0 Then
        'If none of the button's names(minus the word Button) equals its respective Text then set the Form's Text
        Me.Text = "Winner winner chicken dinner!"
    End If
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Tags for this Thread

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