Sort game score and user name records
in
Programming Questions
•
3 years ago
Hello world!
I am writing a game, based on a how much users gain score.
My problem is: get every user name, his score, and sort it that way so the biggest score is at the top of the list.
(Also I would like to save score to file on a disk, so I have best record even after i restart the sketch)
here is the idea:
ricardo: 245
julia: 67
andreas: 22
john: 3
etc...
here's the prototype sketch for sorting and listing these scores. here you have to type scores manually (in game it will appear dynamically
)
It works, however i believe it's the worst possible solution :)
my solution is to save score number into 3-digit string, then append name of a user, then sort it with
sort()
and then extract score and name again to human readable format.
so I really
appreciate any tips mo make this algorithm more simple:
Here you have to type 3-digit number and the name of the user as one string
example: 022andreas, 245ricardo, 003john etc.
- ArrayList names;
- String msg=""; // name of a person
- void setup()
- {
- size(400, 200);
- names=new ArrayList();
- }
- void draw()
- {
- background(100);
- String[] sorted=new String[names.size()]; // make temp string array which will be sorted
- for (int i=0;i<names.size();i++)
- {
- sorted[i]=(String)names.get(i);
- }
- sorted = sort(sorted); // sort temp array
- for (int i=0;i<sorted.length;i++) // make biggest score appear at the top of the list
- {
- println(sorted.length);
- String number="";
- String name=sorted[i].substring(3,sorted[i].length()); // get the name of a person
- if (sorted[i].substring(0,1).equals("0"))
- {
- if (sorted[i].substring(1,2).equals("0"))
- number=sorted[i].substring(2,3); // example score 005
- else
- number=sorted[i].substring(1,3); // example score 027
- }
- else number=sorted[i].substring(0,3); // example score 165
- text(name+": "+number,20,40+map(i,0,sorted.length,sorted.length,0)*10); // map makes listing of biggest score at the top
- }
- text("type name and score in \"###name\" manner: "+msg, 20,20);
- }
- void keyPressed()
- {
- if (key==ENTER)
- {
- names.add(msg);
- msg="";
- }
- else msg+=key;
- }
Thanks,
jimmi
1