Another Highscore Technique
Author |
Message |
GregLP
|
Posted: Thu Jan 19, 2006 11:15 pm Post subject: Another Highscore Technique |
|
|
While I was making my first game with actual highscore, I discovered a very quick way to record the numbers.
It starts off with a procedure to get the scores out of the file:
The array which has the highscores from the file should be one greater in number of elements than the total number of highscore places you would like. (so, have 1..11 if you want 10 spaces)
code: |
procedure ReadHighScores (var Scores : HighScoreType)
var Counter := 1
var HighScoreFile : int
open : HighScoreFile, "High_Score.txt", get
loop
exit when eof (HighScoreFile)
get : HighScoreFile, Scores (Counter)
Counter += 1
end loop
close : HighScoreFile
end ReadHighScores
|
This procedure accesses the file High_Score and gets all the values in it into the array (In this tutorial I used 10 values in the file)
Then, the 11th element gets the time/score that the user just got:
code: |
Scores (11) := TotalTimeElapsed
|
Now, use a sort procedure to rearrange the array into lowest to highest:
code: |
procedure Bubble (var x : HighScoreType)
var SortedAlready : boolean
var Temp : int
var End := 11 %The size of the array
loop
SortedAlready := true
for i : 1 .. End - 1
if x (i) > x (i + 1) then
SortedAlready := false
Temp := x (i)
x (i) := x (i + 1)
x (i + 1) := Temp
end if
end for
End -= 1
exit when SortedAlready or End = 1
end loop
end Bubble
|
This procedure looks complicated but all it does is sort the array.
Now, put to file only the first 10 elements. (This is why the End variable is set to 10)
code: |
procedure PutToFile (Scores : HighScoreType)
var HighScoreFile : int
var End:= 10
open : HighScoreFile, "High_Score.txt", put
for i : 1 .. 10
put : HighScoreFile, Scores (i)
end for
close : HighScoreFile
end PutToFile
|
This of course only puts the first 10 elements and drops the 11th. No fuss.
I have found this technique to be rock solid and have had no problems with it. If you have any questions or can think of a much better way to do the same thing then speak up! |
|
|
|
|
|
Sponsor Sponsor
|
|
|
NikG
|
Posted: Thu Mar 16, 2006 5:13 pm Post subject: (No subject) |
|
|
Pretty good!
It seems so logical now that you've shown it.
Edit: +5 bits |
|
|
|
|
|
Cervantes
|
Posted: Thu Mar 16, 2006 7:27 pm Post subject: Re: Another Highscore Technique |
|
|
Thanks for the concise tutorial.
GregLP wrote:
code: |
procedure Bubble (var x : HighScoreType)
var SortedAlready : boolean
var Temp : int
var End := 11 %The size of the array
loop
SortedAlready := true
for i : 1 .. End - 1
if x (i) > x (i + 1) then
SortedAlready := false
Temp := x (i)
x (i) := x (i + 1)
x (i + 1) := Temp
end if
end for
End -= 1
exit when SortedAlready or End = 1
end loop
end Bubble
|
A few things, here.
- A bubble sort can be used to sort just about anything, not just the 11 elements in your high score list. Don't hardcode the value 11 into the variable 'End'.
- Every naming convention I've seen uses lower case letters at the beginning of variables. Things beginning with an upper case letter are usually constants or classes or modules.
- Why the loop and the boolean?
zylum wrote:
code: |
procedure swap (var list : array 1 .. * of int, i, j : int)
const temp := list (i)
list (i) := list (j)
list (j) := temp
end swap
...
procedure bubbleSort (var list : array 1 .. * of int)
for j : 1 .. upper(list) - 1
const last := upper(list) - j + 1
for k : 1 .. last - 1
if list (k) > list (k + 1) then
swap (list, k, k + 1)
end if
end for
end for
end bubbleSort
|
Edit: Can't spell. |
|
|
|
|
|
[Gandalf]
|
Posted: Sun Mar 19, 2006 1:51 am Post subject: (No subject) |
|
|
This is actually the thread that prompted me to post my High Scores Class.
Note that you do not need a sorting algorithm for high scores, as long as the scores are sorted when they are first created. You only need to check if the new score was higher than a previous score, if so replace it and move all the other scores down one slot.
Yes, shameless plug. =) |
|
|
|
|
|
|
|