Question Best way to save sorted data to an array (or display it)

mcapone888

Member
Joined
Jun 17, 2011
Messages
10
Programming Experience
Beginner
Hi everyone. I am a novice with VB.Net. I created a high score table for an educational game I created. The High Score table will show the top 5 highest test scores (as percentages). It includes the person's name, percent correct, and number of correct answers. The test is open ended so the person taking it can choose when they have answered enough questions.

OK. I am saving the three variables as arrays:

EasyName(4) as String
EasyPercentage(4) as Decimal
EasyNumberCorrect(4) as Integer

* "Easy" is in reference to the game level - not important.

The Arrays are saved having the EasyPercentage array contain the highest percentage in the 0 spot, and then the 1, 2, 3, and 4 spots have percentages going lower and lower (so the 0 spot is the best percentage).

When the test is done I compare their percent correct to the EasyPercentage array. I compare it against the first value (0). If it the current percent is
greater, I save it to this spot in the array and bump down all the values in the array (I also do this with the EasyName(4) and EasyNumberCorrect(4) array to keep them all in sync. If their current percent is not better than the value at array EasyPercentage(0), I move on to (1). So on and so on. I am using an If / End If tree for all of this. I know I should probably use a loop, but again, I am a novice (and loops scare me :crying:).

Anyway, this is all working great. I am also saving / loading the data from a text file.

My issue is many times (especially in Easy mode), all top five percentages are 100%. Once all five spots are 100%, no one else can make the top five as the EasyPercentage array has all 100's in all five spaces (meaning no one new percentage will ever be greater than 100%).

I decided I want to use the EasyNumberCorrect array as a tie breaker. Meaning if the top two percentages are 100, then whoever has the higher amount of correct answers (EasyNumberCorrect) gets listed in the top spot.

This is where i get confused. How should I do this? Should I make a change when I save the data to the arrays, or should I just make a change to the form which displays the high scores. Perhaps I should use a multi-dimensional arrays to store both the EasyPercentage and EasyNumberCorrect values?

Any suggestions would be appreciated. Thanks.

Here's how I am saving my data to the arrays after the game - this only runs if they have made a high score:


VB.NET:
TempHighScoreName = Trim(txtName.Text)
TempHighScorePercentage = (CorrectTotal / TotalTotal) * 100
TempHighScoreNumberCorrect = CorrectTotal

If GameLevel = "Easy" Then
 If TempHighScorePercentage > EasyPercentage(0) Then
  EasyName(4) = EasyName(3)
  EasyPercentage(4) = EasyPercentage(3)
  EasyNumberCorrect(4) = EasyNumberCorrect(3)
  EasyName(3) = EasyName(2)
  EasyPercentage(3) = EasyPercentage(2)
  EasyNumberCorrect(3) = EasyNumberCorrect(2)
  EasyName(2) = EasyName(1)
  EasyPercentage(2) = EasyPercentage(1)
  EasyNumberCorrect(2) = EasyNumberCorrect(1)
  EasyName(1) = EasyName(0)
  EasyPercentage(1) = EasyPercentage(0)
  EasyNumberCorrect(1) = EasyNumberCorrect(0)
  EasyName(0) = TempHighScoreName
  EasyPercentage(0) = TempHighScorePercentage
  EasyNumberCorrect(0) = TempHighScoreNumberCorrect
 ElseIf TempHighScorePercentage > EasyPercentage(1) Then
  EasyName(4) = EasyName(3)
  EasyPercentage(4) = EasyPercentage(3)
  EasyNumberCorrect(4) = EasyNumberCorrect(3)
  EasyName(3) = EasyName(2)
  EasyPercentage(3) = EasyPercentage(2)
  EasyNumberCorrect(3) = EasyNumberCorrect(2)
  EasyName(2) = EasyName(1)
  EasyPercentage(2) = EasyPercentage(1)
  EasyNumberCorrect(2) = EasyNumberCorrect(1)
  EasyName(1) = TempHighScoreName
  EasyPercentage(1) = TempHighScorePercentage
  EasyNumberCorrect(1) = TempHighScoreNumberCorrect
 ElseIf TempHighScorePercentage > EasyPercentage(2) Then
  EasyName(4) = EasyName(3)
  EasyPercentage(4) = EasyPercentage(3)
  EasyNumberCorrect(4) = EasyNumberCorrect(3)
  EasyName(3) = EasyName(2)
  EasyPercentage(3) = EasyPercentage(2)
  EasyNumberCorrect(3) = EasyNumberCorrect(2)
  EasyName(2) = TempHighScoreName
  EasyPercentage(2) = TempHighScorePercentage
  EasyNumberCorrect(2) = TempHighScoreNumberCorrect
 ElseIf TempHighScorePercentage > EasyPercentage(3) Then
  EasyName(4) = EasyName(3)
  EasyPercentage(4) = EasyPercentage(3)
  EasyNumberCorrect(4) = EasyNumberCorrect(3)
  EasyName(3) = TempHighScoreName
  EasyPercentage(3) = TempHighScorePercentage
  EasyNumberCorrect(3) = TempHighScoreNumberCorrect
 ElseIf TempHighScorePercentage > EasyPercentage(4) Then
  EasyName(4) = TempHighScoreName
  EasyPercentage(4) = TempHighScorePercentage
  EasyNumberCorrect(4) = TempHighScoreNumberCorrect
                End If
 End If
End If

This is how I am displaying the data on the High Score form:

VB.NET:
[SIZE=2] lblEasyName0.Text = EasyName(0)
lblEasyPercentage0.Text = FormatNumber(EasyPercentage(0), 2) & [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"%"
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]lblEasyNumberCorrect0.Text = EasyNumberCorrect(0)
lblEasyName1.Text = EasyName(1)
lblEasyPercentage1.Text = FormatNumber(EasyPercentage(1), 2) & [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"%"
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]lblEasyNumberCorrect1.Text = EasyNumberCorrect(1)
lblEasyName2.Text = EasyName(2)
lblEasyPercentage2.Text = FormatNumber(EasyPercentage(2), 2) & [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"%"
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]lblEasyNumberCorrect2.Text = EasyNumberCorrect(2)
lblEasyName3.Text = EasyName(3)
lblEasyPercentage3.Text = FormatNumber(EasyPercentage(3), 2) & [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"%"
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]lblEasyNumberCorrect3.Text = EasyNumberCorrect(3)
lblEasyName4.Text = EasyName(4)
lblEasyPercentage4.Text = FormatNumber(EasyPercentage(4), 2) & [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"%"
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]lblEasyNumberCorrect4.Text = EasyNumberCorrect(4)
[/SIZE]
 
Surely if the user has 100% then they have all the questions right, so how can you determine which person has done better than the other in this case. There is an answer of course but it would involve another array and a timer object.

Keep a track of how long it takes for the user to do the game, record the time in the new array and then even when two people have 100% you can determine the better player by which one is quicker.

Let me know if you need help with the timer object (Its quite simple if you have a quick read up of it)

Regards

PS: You realy should practice using loops, they aren't as scary as you think, they are very useful and quite often make your code a lot easier / better.
 
Last edited:
Thank you for the response. The tie breaker would be the EasyNumberCorrect() array. So if both players had 100% (specified in the EasyPercentage() array), then whoever had a higher amount of Correct Answers (found within the EasyNumberCorrect() array), would put that person's name on top of the list. Thanks for any feedback.
 
Thank you for the response. The tie breaker would be the EasyNumberCorrect() array. So if both players had 100% (specified in the EasyPercentage() array), then whoever had a higher amount of Correct Answers (found within the EasyNumberCorrect() array), would put that person's name on top of the list. Thanks for any feedback.

Sorry I don't think you get what I was saying (I understand about the arrays). What I want to understand is what you are basing the 100% on. Normaly in a quiz type game as you seem to have, to get 100% you need to get all the questions right. So how can you have two people with 100% and one have more questions correct than the other? This is why in my other post I reccomended timing them.

Regards
 
Thanks. Actually, the game would be considered a solo game, not multiplayer. The child is just playing against themselves. Another person can play the game, but they are not competing directly against each other at the same time.

The game basically is pretty simple. A player starts the game and is given two numbers and a text box. They times the two numbers together, enter their answer in the text box and hit a submit button. The game then tells them if they were correct or not. It keeps track of how many right answers, and how many total questions the person was given so far. At the end of each problem, another button is giving allowing the player to quit. That is when I check to see if their percentage correct made the high score. The player can choose to quit after 1 question, or 100 hundred; or whatever number they want in between.

I called it a game, but really it is just a single person app a child can use to practice multiplication. I added the high score table just to keep them playing (either to beat their own high score, or to beat someone elses who played previously).

This is how multiple people can wind up with 100%. I am just looking for a way to break that tie on the High Score table using the EasyNumberCorrect() array as the tie breaker. I just don't know how to do it.

Thanks for any advice.
 
Thanks. Actually, the game would be considered a solo game, not multiplayer. The child is just playing against themselves. Another person can play the game, but they are not competing directly against each other at the same time.

The game basically is pretty simple. A player starts the game and is given two numbers and a text box. They times the two numbers together, enter their answer in the text box and hit a submit button. The game then tells them if they were correct or not. It keeps track of how many right answers, and how many total questions the person was given so far. At the end of each problem, another button is giving allowing the player to quit. That is when I check to see if their percentage correct made the high score. The player can choose to quit after 1 question, or 100 hundred; or whatever number they want in between.

I called it a game, but really it is just a single person app a child can use to practice multiplication. I added the high score table just to keep them playing (either to beat their own high score, or to beat someone elses who played previously).

This is how multiple people can wind up with 100%. I am just looking for a way to break that tie on the High Score table using the EasyNumberCorrect() array as the tie breaker. I just don't know how to do it.

Thanks for any advice.

Ok I get you now. As to your question about a mulit dimensional array, I would steer clear of these until you have more experience. If it were me:

Firstly at the end of the game I would check to see if the player has tied with the highest player IE the one at the top, now you could check against the percentage or you could check against the percentage and the number of questions answered (this would be the most correct as if you retain 100% with more questions then you have done better anyway). Once done tell the player they have tied, won or lost (if necessary) and then sort the array's by percentage and number of questions. IE:

If (percentage1 >= percentage2) And (numquestions1 > numquestions2) Then
'The player with percentage1 & numquestions1 is the winner​
Else
'The player with percentage2 & numquestions2 is the winner​
End If

Hope this helps.
 
Thank you! I believe this will work. I will implement it as soon as I get home this evening. I will run this type of code against all five top scores (starting with the highest first). I will then loop this code five times. The reason being is if there are more than two top scorers with 100%. Thank you for your posts. I believe I know what must be done now. Thanks.
 
Back
Top