Help With High Score Leaderboard Code

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1408 Views - Last Post: 20 March 2015 - 10:01 AM Rate Topic: -----

#1 DRest608  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 19-March 15

Help With High Score Leaderboard Code

Posted 19 March 2015 - 01:15 PM

I am trying to take 8 players scores and sort them into highest at the top to lowest at the bottom. Any codes i have tried just is not working out at all and i have no idea what i am doing wrong. I have 8 separate Textboxes.
First I had this:

Private Sub RankScoresBtn_Click(sender As Object, e As EventArgs) Handles RankScoresBtn.Click
Dim score(7) As Integer
score(0) = CInt(P1Score.Text)
score(1) = CInt(P2Score.Text)
score(2) = CInt(P3Score.Text)
score(3) = CInt(P4Score.Text)
score(4) = CInt(P5Score.Text)
score(5) = CInt(P6Score.Text)
score(6) = CInt(P7Score.Text)
score(7) = CInt(P8Score.Text)
Array.Sort(score)
Array.Reverse(score)
End Sub



Then i tried this:
Private Sub RankScoresBtn_Click(sender As Object, e As EventArgs) Handles RankScoresBtn.Click
Dim highscore(7) As Integer
highscore(0) = P1Score.Text(1)
highscore(1) = P2Score.Text(2)
highscore(2) = P3Score.Text(3)
highscore(3) = P4Score.Text(4)
highscore(4) = P5Score.Text(5)
highscore(5) = P6Score.Text(6)
highscore(6) = P7Score.Text(7)
highscore(7) = P8Score.Text(8)

Array.Sort(highscore)
Array.Reverse(highscore)
End Sub



but with that one, all the Pscore.text had blue lines under it

then i tried:

Dim a(5) As Integer
Dim i As Integer
Dim j As Integer
a(0) = P1Score.Text
a(1) = P2Score.Text
a(2) = P3Score.Text
a(3) = P4Score.Text
a(4) = P5Score.Text
For i = 0 To 4
For j = i + 1 To 4
If a(i) > a(j) Then
' swap the number in those two locations
' For Eg: if x = 10 and y = 9 then
' x y
' 10 9
' Since 10 > 9 we swap x and y
' x = x + y => x = 10 + 9 = 19
' y = x - y => y = 19 - 9 = 10 => y = 10
' x = x - y => x = 19 - 10 = 9 => x = 9
a(i) = a(i) + a(j)
a(j) = a(i) - a(j)
a(i) = a(i) - a(j)
End If
Next j
Next i
'Now the array would hold the values in ascending order
'To sort in descending order just make the following change
'If a(i) < a(j) Then
and with this i got an error stating Conversion from string "" to type 'Integer' is not valid"



and last but not least i tried this:

#Region "variables"
    'Dim score As Integer
    Dim player1score As Integer = 0
    Dim player2score As Integer = 0
    Dim player3score As Integer = 0
    Dim player4score As Integer = 0
    Dim player5score As Integer = 0
    Dim player6score As Integer = 0
    Dim player7score As Integer = 0
    Dim player8score As Integer = 0

    Dim p1name As String
    Dim p2name As String
    Dim p3name As String
    Dim p4name As String
    Dim p5name As String
    Dim p6name As String
    Dim p7name As String
    Dim p8name As String

    'Private Sub scoreboard()
    'End Sub
#End Region

    Private Sub RankScoresBtn_Click(sender As Object, e As EventArgs) Handles RankScoresBtn.Click
        If player1score > player2score Then
            player8score = player7score
            player7score = player6score
            player6score = player5score
            player5score = player4score
            player4score = player3score
            player3score = player2score
            player2score = player1score
            p8name = p7name
            p7name = p6name
            p6name = p5name
            p5name = p4name
            p4name = p3name
            p3name = p2name
            p2name = p1name
            PlayerOneRank.Text = p1name
            PlayerTwoRank.Text = p2name
            PlayerThreeRank.Text = p3name
            PlayerFourRank.Text = p4name
            PlayerFiveRank.Text = p5name
            PlayerSixRank.Text = p6name
            PlayerSevenRank.Text = p7name
            PlayerEightRank.Text = p8name
            P1Score.Text = player1score
            P2Score.Text = player2score
            P3Score.Text = player3score
            P4Score.Text = player4score
            P5Score.Text = player5score
            P6Score.Text = player6score
            P7Score.Text = player7score
            P8Score.Text = player8score
            Exit Sub
        End If
        If player2score > player1score Then
            player8score = player7score
            player7score = player6score
            player6score = player5score
            player5score = player4score
            player4score = player3score
            player3score = player2score
            p8name = p7name
            p7name = p6name
            p6name = p5name
            p5name = p4name
            p4name = p3name
            p3name = p2name
            PlayerTwoRank.Text = p2name
            PlayerThreeRank.Text = p3name
            PlayerFourRank.Text = p4name
            PlayerFiveRank.Text = p5name
            PlayerSixRank.Text = p6name
            PlayerSevenRank.Text = p7name
            PlayerEightRank.Text = p8name
            P2Score.Text = player2score
            P3Score.Text = player3score
            P4Score.Text = player4score
            P5Score.Text = player5score
            P6Score.Text = player6score
            P7Score.Text = player7score
            P8Score.Text = player8score
            Exit Sub
        End If
        If player3score > player2score Then
            player8score = player7score
            player7score = player6score
            player6score = player5score
            player5score = player4score
            player4score = player3score
            p8name = p7name
            p7name = p6name
            p6name = p5name
            p5name = p4name
            p4name = p3name
            PlayerThreeRank.Text = p3name
            PlayerFourRank.Text = p4name
            PlayerFiveRank.Text = p5name
            PlayerSixRank.Text = p6name
            PlayerSevenRank.Text = p7name
            PlayerEightRank.Text = p8name
            P3Score.Text = player3score
            P4Score.Text = player4score
            P5Score.Text = player5score
            P6Score.Text = player6score
            P7Score.Text = player7score
            P8Score.Text = player8score
            Exit Sub
        End If
        If player4score > player3score Then
            player8score = player7score
            player7score = player6score
            player6score = player5score
            player5score = player4score
            p8name = p7name
            p7name = p6name
            p6name = p5name
            p5name = p4name
            PlayerFourRank.Text = p4name
            PlayerFiveRank.Text = p5name
            PlayerSixRank.Text = p6name
            PlayerSevenRank.Text = p7name
            PlayerEightRank.Text = p8name
            P4Score.Text = player4score
            P5Score.Text = player5score
            P6Score.Text = player6score
            P7Score.Text = player7score
            P8Score.Text = player8score
            Exit Sub
        End If
        If player5score > player4score Then
            player8score = player7score
            player7score = player6score
            player6score = player5score
            p8name = p7name
            p7name = p6name
            p6name = p5name
            PlayerFiveRank.Text = p5name
            PlayerSixRank.Text = p6name
            PlayerSevenRank.Text = p7name
            PlayerEightRank.Text = p8name
            P5Score.Text = player5score
            P6Score.Text = player6score
            P7Score.Text = player7score
            P8Score.Text = player8score
            Exit Sub
        End If
        If player6score > player5score Then
            player8score = player7score
            player7score = player6score
            p8name = p7name
            p7name = p6name
            PlayerSixRank.Text = p6name
            PlayerSevenRank.Text = p7name
            PlayerEightRank.Text = p8name
            P6Score.Text = player6score
            P7Score.Text = player7score
            P8Score.Text = player8score
            Exit Sub
        End If
        If player7score > player6score Then
            player8score = player7score
            p8name = p7name
            PlayerSevenRank.Text = p7name
            PlayerEightRank.Text = p8name
            P7Score.Text = player7score
            P8Score.Text = player8score
            Exit Sub
        End If
        If player8score > player7score Then
            player8score = player8score
            p8name = p8name
            PlayerEightRank.Text = p8name
            P8Score.Text = player8score
            Exit Sub
        End If
    End Sub



Any help would be GREATLY GREATLY appreciated....i'm ripping my hair out on this!


Here is a pic of my design for it.
Attached Image

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Help With High Score Leaderboard Code

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: Help With High Score Leaderboard Code

Posted 19 March 2015 - 01:20 PM

I see no problem with the first code block other than I don't see where you tried to display it. And as long as each element can indeed be case to integer.

take code 1 and extend it to now put the elements values back into the textbox

ie, p1Score.Text = score(0)
...
Was This Post Helpful? 0
  • +
  • -

#3 DRest608  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 19-March 15

Re: Help With High Score Leaderboard Code

Posted 19 March 2015 - 01:51 PM

View PostCharlieMay, on 19 March 2015 - 01:20 PM, said:

I see no problem with the first code block other than I don't see where you tried to display it. And as long as each element can indeed be case to integer.

take code 1 and extend it to now put the elements values back into the textbox

ie, p1Score.Text = score(0)
...


i did what you said and still nothing when i click the button to order them.

View PostDRest608, on 19 March 2015 - 01:48 PM, said:

View PostCharlieMay, on 19 March 2015 - 01:20 PM, said:

I see no problem with the first code block other than I don't see where you tried to display it. And as long as each element can indeed be case to integer.

take code 1 and extend it to now put the elements values back into the textbox

ie, p1Score.Text = score(0)
...


i did what you said and still nothing when i click the button to order them.


actually i shouldn't say nothing....for some reason when i click the button, the blank boxes turn to zeroes?
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: Help With High Score Leaderboard Code

Posted 19 March 2015 - 01:51 PM

You did, well can you show me where you did because what I told you works so how about you show what you just tried off my suggestion.

That first code example you gave takes the number from the textboxes into an array and sorts them, then reverses the array. That's it. You now have to go from there and place the array elements back into their appropriate textbox. So show what you just did that didn't work and we can help you along.
Was This Post Helpful? 0
  • +
  • -

#5 DRest608  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 19-March 15

Re: Help With High Score Leaderboard Code

Posted 19 March 2015 - 06:21 PM

View PostCharlieMay, on 19 March 2015 - 01:51 PM, said:

You did, well can you show me where you did because what I told you works so how about you show what you just tried off my suggestion.

That first code example you gave takes the number from the textboxes into an array and sorts them, then reverses the array. That's it. You now have to go from there and place the array elements back into their appropriate textbox. So show what you just did that didn't work and we can help you along.



well i did it this way now and it works...but i just need it the scores to take the player's names along with them. How would i go about doing this?

 Private Sub RankScoresBtn_Click(sender As Object, e As EventArgs) Handles RankScoresBtn.Click
        Dim boxes() As TextBox = {P1Score, P2Score, P3Score, P4Score, P5Score, P6Score, P7Score, P8Score}
        Dim score(boxes.Length - 1) As Integer

        For i As Integer = 0 To boxes.Length - 1
            Integer.TryParse(boxes(i).Text, score(i))
        Next

        Array.Sort(score)
        Array.Reverse(score)

        For i As Integer = 0 To boxes.Length - 1
            boxes(i).Text = score(i).ToString
        Next
    End Sub

Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: Help With High Score Leaderboard Code

Posted 19 March 2015 - 07:17 PM

You could create your own type with PlayerName and Score properties. Fill each instance with the appropriate values and add them to a List(Of T)

From this, you could use LINQ to sort the list descending and then bring the sorted result into your textboxes. This would keep the player and the score together as they are contained together in your custom type.
Was This Post Helpful? 0
  • +
  • -

#7 DRest608  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 19-March 15

Re: Help With High Score Leaderboard Code

Posted 19 March 2015 - 08:24 PM

View PostCharlieMay, on 19 March 2015 - 07:17 PM, said:

You could create your own type with PlayerName and Score properties. Fill each instance with the appropriate values and add them to a List(Of T)

From this, you could use LINQ to sort the list descending and then bring the sorted result into your textboxes. This would keep the player and the score together as they are contained together in your custom type.


I'm sorry...i'm new to coding so I'm not really sure how to make a type and not sure what this LINQ is and how i'd use it
Was This Post Helpful? 0
  • +
  • -

#8 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: Help With High Score Leaderboard Code

Posted 20 March 2015 - 05:30 AM

OK, since you've worked with arrays, perhaps before sorting the array, store the player and score in another mult-dimensional array.

Then after sort you could search the array find the score and from that left element get access to the player in the second dimension.

This is going to be more code but it's in line with how you're managing this data currently.
Was This Post Helpful? 0
  • +
  • -

#9 DRest608  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 19-March 15

Re: Help With High Score Leaderboard Code

Posted 20 March 2015 - 05:51 AM

View PostCharlieMay, on 20 March 2015 - 05:30 AM, said:

OK, since you've worked with arrays, perhaps before sorting the array, store the player and score in another mult-dimensional array.

Then after sort you could search the array find the score and from that left element get access to the player in the second dimension.

This is going to be more code but it's in line with how you're managing this data currently.


this is what i came up with but it still wont' work

Dim Players(,) As String
Dim i As Integer = 0
While P1Score.Text()
    Players(i,0) = P1Score.Text
    Players(i,1) = PLayerOneName.Text
    i = i + 1
End While


this doesn't even seem right
Was This Post Helpful? 0
  • +
  • -

#10 DRest608  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 19-March 15

Re: Help With High Score Leaderboard Code

Posted 20 March 2015 - 06:05 AM

I also tried this but Player1 has a blue line under it

Private Sub RankScoresBtn_Click(sender As Object, e As EventArgs) Handles RankScoresBtn.Click
        Dim PlayerOneName As String
        Dim PlayerOneScore As String
        Dim Player1 As String

        PlayerOneName = PlayerOneRank.Text
        PlayerOneScore = P1Score.Text
        Player1 = PlayerOneName & PlayerOneScore

        P1Score.Text = Player1
        PlayerOneRank.Text = Player1

        Dim boxes() As TextBox = {Player1}
        Dim score(boxes.Length - 1) As Integer

        For i As Integer = 0 To boxes.Length - 1
            Integer.TryParse(boxes(i).Text, score(i))
        Next

        Array.Sort(score)
        Array.Reverse(score)

        For i As Integer = 0 To boxes.Length - 1
            boxes(i).Text = score(i).ToString
        Next
    End Sub

Was This Post Helpful? 0
  • +
  • -

#11 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: Help With High Score Leaderboard Code

Posted 20 March 2015 - 06:13 AM

What does your error window show that blue squiggly line to mean?
Was This Post Helpful? 0
  • +
  • -

#12 DRest608  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 19-March 15

Re: Help With High Score Leaderboard Code

Posted 20 March 2015 - 06:26 AM

View PostCharlieMay, on 20 March 2015 - 06:13 AM, said:

What does your error window show that blue squiggly line to mean?


It says: Value of 'String' cannot be converted to 1-dimensional array of System.Windows.Forms.Textbox
Was This Post Helpful? 0
  • +
  • -

#13 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: Help With High Score Leaderboard Code

Posted 20 March 2015 - 07:05 AM

That's pretty straightforward

See, in line 4 you declared Player1 to be a String

In line 13 you declare an Array of Textbox and you try to initialize it with that string. It's looking for a textbox there.
Was This Post Helpful? 0
  • +
  • -

#14 DRest608  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 19-March 15

Re: Help With High Score Leaderboard Code

Posted 20 March 2015 - 07:19 AM

View PostCharlieMay, on 20 March 2015 - 07:05 AM, said:

That's pretty straightforward

See, in line 4 you declared Player1 to be a String

In line 13 you declare an Array of Textbox and you try to initialize it with that string. It's looking for a textbox there.


If i change it to String instead of Textbox, then all the "boxes" get blue underlines
Was This Post Helpful? 0
  • +
  • -

#15 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: Help With High Score Leaderboard Code

Posted 20 March 2015 - 07:23 AM

Also, please scroll down to the Fast Reply box to respond, reserve the Quote+Reply button only for occasions when you need to specifically quote from a post. There is no need to quote the reply right above you.

If by habit, you accidentally hit it just delete everything from [ quote ] to [ /quote ] and begin your response.

This post has been edited by CharlieMay: 20 March 2015 - 07:24 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2