Here is a TScores class that will keep track of name and score of the top
scorers for your games.
It works like this:
TScores is derived from TObjectList and maintains the specified
number of high scorers in a list as TScoreObj objects. Each TScoreObj
contains a name and score. The list is maintained in descending score
sequence. TScores has the following methods:
- Procedure LoadScores(scoreFilename:string); Initializes a TScores
instance with previously saved top score information.
If "scoreFilename" file does not exist, an
empty file is created. The file is saved is an "Init" file
format using TIniFile. Note that by default TIniFile
will look for and create files in the Windows folder unless the
filename includes path information. In this test program, I
create and maintain the file in the same foplder as the executable
program.
- Procedure SaveScores(ScoreFileName:string); Saves the
current scores in the specified file.
- Function SetScore(newscore:integer; newname:string):integer; Creates
a TScoreObj object and inserts it in the appropriate place in the
list (based on descending score information). If the number of entries
in the list after this insertion exceed the desired number of entries to
keep, the final entry in the list is deleted.
- Procedure GetScores(list:TStrings); Returns a displayable
version of the information in TScores.
- Function AddRec(rec:TScoreObj):integer; Function used by Loadscores
and SetScore to add objects to the list. Probably not required
for normal user use.
The test program simply lets you add scores to a displayed list, clear the
list, and change the number of of top scorers to remember.
I haven't really used the code much so, as usual, let me know if you
find a bug.
Download source
| Created: January 14, 2004 |
Modified: November 07, 2008
|