How to create a high score database in Java ME
Article Metadata
Code Example
Article
Every game should have a high score system to allow gamers to challenge other gamers or themselves.
Java ME use Record Management System (RMS) to allow developers to save and restore data over different application sessions. So, for creating a quick high-score system saving name of the user and points, you can use the following code.
Contents |
Source code
Adding a new high-score
// We open the recordstore
RecordStore highscore = RecordStore.openRecordStore("High Score", true);
// To add a new HiScore we use a quick string comma-separated
String str = new String("John,3400");
byte [] strb = str.getBytes();
int id = highscore.addRecord(strb, 0, strb.length);
highscore.closeRecordStore();
Retrieving top high-scores
RecordStore highscore = RecordStore.openRecordStore("High Score", true);
// To read all hiscores saved on the RMS
RecordEnumeration enum = highscore.enumerateRecords(null, null, false);
int id;
byte[] record;
String str;
while (enum.hasNextElement( )) {
id = enum.nextRecordId( );
record = highscore.getRecord(id);
str = new String(record);
// Operate with str to extract name and points
}
highscore.closeRecordStore();
How to retrieve correctly sorted scores
To sort the scores from the highest to the lowest one, an option is to implement the RecordComparator interface, and to use it when calling the RecordStore enumerateRecords() method.
A basic implementation of a RecordComparator that sorts the scores stored is the following one:
class ScoresComparator implements RecordComparator
{
public int compare(byte[] arg0, byte[] arg1)
{
int score0 = 0, score1 = 0;
for(int i = 0; i < arg0.length; i++)
{
if(arg0[i] == ',')
{
score0 = Integer.parseInt(new String(arg0, i + 1, arg0.length - i - 1));
}
}
for(int i = 0; i < arg1.length; i++)
{
if(arg1[i] == ',')
{
score1 = Integer.parseInt(new String(arg1, i + 1, arg1.length - i - 1));
}
}
if(score0 < score1)
return RecordComparator.FOLLOWS;
else if(score0 == score1)
return RecordComparator.EQUIVALENT;
else
return RecordComparator.PRECEDES;
}
}
So, the previous call to enumerateRecords() will accordingly change:
RecordEnumeration enumerator = highscore.enumerateRecords(null, new ScoresComparator(), false);
Download
You can download a sample MIDlet showing the code presented in this article here: Media:HighScoreDatabaseMIDlet.zip
Implementation notes
- You should use try-catch sentences to catch exceptions on the I/O operation or make a throws.
- When writing and reading data to and from RMS, you should use separate threads to avoid blocking the MIDlet main thread.


20 Sep
2009
This article is intended for Game Developers who want to create a very basic functionality that exist in every type of game "High Score". The code given in this article is about storing the player name and its corresponding score in database object RecordStore.After that there is a very great thing is to display the already stored scores and then sort it out.
Really good coding practice have been followed by writer of this article. Article can become the starting point of every game developers.This Example is tested thoroughly.So any one can try it in their games without any risk of errors.
21 Sep
2009
This article shows how to implement a high score database in Java ME. Such functionality is often built into games to keep track of the highest scores achieved so far. The code example shows how to achieve this using the Record Management System (RMS). The article shows how to add a new score to the database and also how to retrieve the highest scores. If scores are to be sorted (as is usually the case – highest scores are normally sorted in descending order), the article provides an example of the implementation of the RecordComparator interface. An implementation of this interface is passed as a parameter to the enumerateRecords method of the RecordStore class, which ensures that the returned RecordEnumeration is sorted using the provided comparator.
For devices which support JSR-75 (the FileConnection API), this functionality could also be implemented using File I/O. In the case of high-scores stored in a file, however, some sort of basic encryption should probably be maintained to prevent users cheating by directly editing the high-scores file.