Posted By
|
Message
|
jamesh
Registered 28/02/2012 15:24:25
Points 381
|
16th May, 2012 at 16/05/2012 16:35:01 -
Hi
this is probably something pretty obvious, but I've been puzzling over it for a while
I want to make a self-sorting high scores list, not using the built in hi-score object.
In python it would be something like this:
scorelist=[ ] #a list, opened from a file, which contains the list in this format - '[0, 1, 2, 3, 4, 5, 6, etc]'
score= #a number, introduced from the game you just played?
scorelist.append(score)
scorelist.sort() #this I guess is the important bit
if len(scorelist)>10:
del scorelist[0] #remove the lowest number if the list is over a certain length
Then you can change the order so it's top first, write it back to file, whatever.
This is very simple but I guess only so because of the sort function, which takes any work out of it.
I'm guessing this would be quite simple to translate into Lua, though I have no experience with the Lua extension.
What would you guys recommend? Do it in Lua, or is there an easy way to do it with eg an array and a loop? I couldn't figure it out at all
James
n/a
|
Chris Burrows
Registered 14/09/2002
Points 2396
|
16th May, 2012 at 16/05/2012 17:55:27 -
Here are two examples. The first runs a loop which uses the bubble sort algorithm (http://en.wikipedia.org/wiki/Bubble_sort) to re-order the list from highest to lowest. My second example checks if the submitted score is higher than the lowest score on the list, if it is, then it finds at which slot to enter the score, and all lower scores are shifted down. One of them should prove helpful.
http://www.whenthereisnoroominhellthedeadwalktheearth.com/MMF/highscores2.mfa
http://www.create-games.com/download.asp?id=8684
n/a
|
jamesh
Registered 28/02/2012 15:24:25
Points 381
|
16th May, 2012 at 16/05/2012 18:59:23 -
I'll give these methods a go - thanks a lot for coming to my aid!
n/a
|
UrbanMonk BRING BACK MITCH
Registered 07/07/2008
Points 49667
|
17th May, 2012 at 17/05/2012 06:18:01 -
Sort the list when you add a new score to it.
Save the data however you want, but once you've found a method do this.
Check if the score is higher than the lowest score, if it is remove the lowest and put in the new score.
Next keep comparing the score with the one above it and swap if the new score is higher.
Keep doing this till the score above the new score is greater, then stop. You're done.
That's all there is to it, no need to sort the scores everytime you load them, just sort them when you store them and save CPU cycles.
n/a
|
Chris Burrows
Registered 14/09/2002
Points 2396
|
17th May, 2012 at 17/05/2012 06:44:46 -
That's how it's done in my arcade highscores example.
n/a
|
|
|