# high score table problem

Page 1 of 1

## 4 Replies - 3008 Views - Last Post: 12 March 2011 - 12:57 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=220417&amp;s=24ae1478d27403a1d6668d467d030905&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 PandaFace

• New D.I.C Head

Reputation: 0
• Posts: 13
• Joined: 19-February 11

# high score table problem

Posted 05 March 2011 - 08:10 PM

hi all

i'm very new to vb, and am having problems with an fairly basic game where you have to guess a number between 1 and x

I've set it so when you win you have the option of saving your score [which is (50 ÷ guesses) x (300 ÷ time) x maximum number]

it then opens up a form for entering your name and the code on the "Submit" button is:
(label1-10 display your name, label11-20 display score, and label21-30 display the difficulty (max. number) you were playing at)
```
If score >= Dialog1.Label11.Text Then
'change label1, 11, and 21 (the top ones) to name, difficulty, and score
Else If score >= Dialog1.Label12.Text And la < Global.NGG_v4.Dialog1.Label11.Text Then
'change label2, 12, and 22 (the second ones) to name, difficulty, and score
Else If score >= Dialog1.Label13.Text Then
'as above
Else If score >= Dialog1.Label14.Text Then
'as above
Else If score >= Dialog1.Label15.Text Then
'as above
Else If score >= Dialog1.Label16.Text Then
'as above
Else If score >= Dialog1.Label17.Text Then
'as above
Else If score >= Dialog1.Label18.Text Then
'as above
Else If score >= Dialog1.Label19.Text Then
'as above
Else If score >= Dialog1.Label20.Text Then
'as above
End If
```

unfortunately whatever i do it always puts the score etc in the top 3 labels(1, 11, and 21) showing you always have the highest score even when you don't

what am i doing wrong?

Is This A Good Question/Topic? 0

## Replies To: high score table problem

### #2 Martyr2

• Programming Theoretician

Reputation: 5179
• Posts: 13,899
• Joined: 18-April 07

## Re: high score table problem

Posted 06 March 2011 - 02:24 PM

Always keep in mind the types of data you are comparing. Here you are compare a score (which is some type of number) to a string of text. Can you compare 3 to "hello"? No, makes no sense. What you need to do is make sure the text you get from the label is also converted to a number for comparing. Secondly, make sure that each of these score labels is given an actual value. I would suggest you zero them all out first.

Set the score labels to "0" and then when you go to compare the score, first convert the score label to a number using Int32.TryParse() (link to how this works below) and then compare it to score.

Int32.TryParse - MSDN

Another option would be instead of comparing the score directly to the label that you keep an array of scores behind the scenes and compare the score to the values in this array. If the current score is better than the top score in the array, then set the label to the score.

```Dim arScores(9) As Integer

' Initialize all values to zero here

if score >= arScores(0) then
' Change label 1 here
else if score >= arScores(1) then
' Change label 2 here
...

```

Now also make sure you move the scores down the list as needed. If someone gets top score, make sure that you move the scores down 1 spot and then put the new score at the top of the list. I will leave that up to you (think of a loop starting from the index of where you are going to put the new score).

This post has been edited by Martyr2: 06 March 2011 - 02:25 PM

### #3 PandaFace

• New D.I.C Head

Reputation: 0
• Posts: 13
• Joined: 19-February 11

## Re: high score table problem

Posted 06 March 2011 - 05:47 PM

Martyr2, on 06 March 2011 - 02:24 PM, said:

Always keep in mind the types of data you are comparing. Here you are compare a score (which is some type of number) to a string of text. Can you compare 3 to "hello"? No, makes no sense. What you need to do is make sure the text you get from the label is also converted to a number for comparing. Secondly, make sure that each of these score labels is given an actual value. I would suggest you zero them all out first.

Set the score labels to "0" and then when you go to compare the score, first convert the score label to a number using Int32.TryParse() (link to how this works below) and then compare it to score.

Int32.TryParse - MSDN

Another option would be instead of comparing the score directly to the label that you keep an array of scores behind the scenes and compare the score to the values in this array. If the current score is better than the top score in the array, then set the label to the score.

```Dim arScores(9) As Integer

' Initialize all values to zero here

if score >= arScores(0) then
' Change label 1 here
else if score >= arScores(1) then
' Change label 2 here
...

```

thanks a lot, but for some reason neither of these methods work for me...

I've tried going
```Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

scores(0) = Int32.Parse(Dialog1.Label11.Text)
scores(1) = Int32.Parse(Dialog1.Label12.Text)
...

```

and then
```if myscore >= scores(0) then
'change top labels
else if myscore >= scores(1) then
'change second labels
...
```

but it always changes only the top labels (and yes all the score displaying labels have text of "0" so the Parse() should be working)

is there something I've forgotten or overlooked?

### #4 demausdauth

Reputation: 190
• Posts: 692
• Joined: 03-February 10

## Re: high score table problem

Posted 07 March 2011 - 10:44 AM

The code is correct -- you will always change the top 3 labels if the score is greater than the previous score. What I am seeing is you don't have a process for allowing the a score that is highest to bump the current highest down on the list.

What you should do (imo) is develop a process to manage the high scores:

• Read a list of current high scores(probably saved to a text or xml file)
• Determine where the current score falls within the list (if it does)
• Save the list of high scores again
• Display the list of high scores

### #5 PandaFace

• New D.I.C Head

Reputation: 0
• Posts: 13
• Joined: 19-February 11

## Re: high score table problem

Posted 12 March 2011 - 12:57 AM

demausdauth, on 07 March 2011 - 10:44 AM, said:

The code is correct -- ...

well it's certainly not working for me???
(it still just changes the top score)

and thanks, but I've worked out how to save the scores and move them down the table