We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I want to store a high score with a name before, so an example would be "Schotsl 100" where Schotsl is my name and 100 my score. I though the FloatDict would be perfect since I can store one String and value in one set + I can just sort them based of the value which i exactly what i wanted!
Then it turned out I can only use the string once which is my where I run into problems since it means "Schotsl" can only have one high score, Is there another method I could use that would allow me to set create multiple strings with the same name but different high scores and then sort them based of the second value
Answers
float
type.float
.There are many ways to do this.
One is using an ArrayList of custom class objects:
Another is to use a Table with TableRows:
The custom class has slightly more concise code for adding and printing data. However the Table approach comes with saveTable already built-in if you want to save and load your scoreList file between sessions.
@GoTo I want to put them in like a top 10 and display them automatically, so it would be perfect to have 10 different object for all of my 10 high scores (which is why I choose FloatDict since I only want one value but 10 different FloatDict 'entries' ) and then each one contains my name and my score (as float like you said) and I want to sort them among all keys
https://Processing.org/reference/FloatList.html
@schotsl -- If you also want to sort a high score list, I'd go with Table, which already has Table.sort() built in.
Like this:
If you want the sort results reversed, you can loop through the scores backwards -- or there are two undocumented ways of reversing the sort itself:
Results:
Final suggestion: perhaps use
int
for your scores, notfloat
....Given the number of scores are fixed at 10, you can even change to a vanilla
float[]
array: *-:)float[] topScores = new float[10];
Then use Arrays::sort() method to sort that array: :)>-
http://Docs.Oracle.com/javase/8/docs/api/java/util/Arrays.html#sort-float:A-
Still I wonder why a
float
datatype for scores. Can't they be just anint
datatype instead? :>it's a name / value pair though, score AND scorer.
have you never played a video game?
Oops, I've ended up forgetting you still need to associate a name to each score. How forgetful I am! #-o
In this case, the
class
solution proposed at 1st by @jeremydouglass is the simplest.However, it's a good idea to make that
class implements
Comparable too:http://Docs.Oracle.com/javase/8/docs/api/java/lang/Comparable.html
Then having a List container to store each object of the ScoreEntry
class
.And finally, create a getTop10() helper function which calls Collections::sort() over that List container:
http://Docs.Oracle.com/javase/8/docs/api/java/util/Collections.html#sort-java.util.List-
And collect the 10 1st entries after that, returning everything as 1 String, ready to be println(). \m/
Thanks for all your help guys! Table seems like the simplest solution since it has everything build in :)