Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RMS

 
Glenn Castro
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone explain to me the following code:
import javax.microedition.rms.*;
public class Score
{
private static RecordStore scoreRS;
private static final int MAX_REC_LEN=16;
private static StringBuffer recBuf=new StringBuffer(MAX_REC_LEN);
private static byte[] data;

public static void checkInitScore()
{
try
{
scoreRS=RecordStore.openRecordStore("topScore", false);
scoreRS.closeRecordStore();
}
catch(RecordStoreException e)
{
initScore();
}
}

public static void initScore()
{
try
{
scoreRS=RecordStore.openRecordStore("topScore", true);
clearBuf();
for(int i=0; i<5; i++)
{
data=stringToBufferToBytes("Boogs", 0);
scoreRS.addRecord(data, 0, data.length);
clearBuf();
}
scoreRS.closeRecordStore();
}
catch(RecordStoreException e)
{
}
}

public static void resetScore()
{
try
{
RecordStore.deleteRecordStore("topScore");
initScore();
}

catch(RecordStoreNotFoundException e)
{
initScore();
}

catch(RecordStoreException e)
{
}
}

private void saveScore(String name, int score) {
byte[] data = stringToBufferToBytes(name, score);
byte[] oldScore = new byte[50];
RecordStore scoreRS;
try {
scoreRS = RecordStore.openRecordStore("topScore", false);
for(int i=4; i>0; i--) {
scoreRS.getRecord(i, oldScore, 0);
if(score>Integer.parseInt(new String(oldScore, 7, 8).trim())) {
scoreRS.setRecord(i+1, oldScore, 0, oldScore.length);
if(i==1) {
scoreRS.setRecord(i, data, 0, data.length);
}
}
else {
scoreRS.setRecord(i+1, data, 0, data.length);
break;
}
}
data = null;
scoreRS.closeRecordStore();
}
catch(RecordStoreException e) {}
}

public static void clearBuf()
{
for(int i=0; i<MAX_REC_LEN; i++)
{
recBuf.insert(i,' ');
}
recBuf.setLength(MAX_REC_LEN);
}

public static byte[] stringToBufferToBytes(String name, int score)
{
clearBuf();
recBuf.insert(0, name);
recBuf.insert(7, Integer.toString(score));
return recBuf.toString().getBytes();
}
}
 
Jorma Ikonen
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should BTW clarify this code for you by yourself, not anyone else. Anyway, I'm going to use RecordStores in my midlet next time and therefore I can share the information:

public static void checkInitScore()
- this function tries to open RecordStore named as "topscore" and if it's not exist, calls the function of "public static void initScore()"
public static void initScore()
- Creates a new RecordStore named as "topscore" and adds 5 equal records into it.
- data in those each default records are: "boogs 0 ".
public static void resetScore()
- Deletes "topscore" store and calls initScore() to create a new one.
private void saveScore(String name, int score)
- opens a "topscore" and adds a new score to location as it's numerical order presents.
- I'm not sure does the code work, looks awful.
public static void clearBuf()
- a stupid way to use StringBuffers. Also this part of code have been generated due the lack of programming skils (read: unnecessary).
public static byte[] stringToBufferToBytes(String name, int score)
- looks awful. Uses a global variable named as "recBuf" even it could be declared in the function also. Benefit of this would be that use/having of "clearBuf()"-function is not needed.
- Anyway, it generates/returns a string form arguments "name, score" like: "Joe 548 ".

-Jorma-
 
David Price
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a not-quite-right attempt at keeping 5 high scores with names in a MIDP 'record store'.
Method checkInitScore tries to open the record store (the "false" means "throw an exception if it doesn't exist"), and if the record store doesn't exist it calls initScore to create it.
Method initScore opens the record store (the "true" means "create it if it doesn't exist"), then adds five records for player "Boogs" with score 0. These records will automatically get record ids 1 to 5. The two calls to method clearBuf are unnecessary, as stringBufferToBytes calls it at the start anyway.
Method resetScore tries to delete the record store and then create it again using initScore. If this fails because the record store doesn't exist, it just calls initScore; if this fails because of some other record store problem, it gives up.
Method saveScore tries to insert the new score into the record store, so that the record store will be ordered with highest scores in the records with lower ids. The logic of the "for" loop is wrong: it never looks at the score of record number 5, and it always inserts the new score somewhere, even if it's worse than all the saved scores. Try this code instead:
scoreRS.getRecord(5, oldScore, 0);
if(score>Integer.parseInt(new String(oldScore, 7, 8).trim())) {
int i;
for(i=4; i>0; i--) {
scoreRS.getRecord(i, oldScore, 0);
if(score>Integer.parseInt(new String(oldScore, 7, 8).trim())) {
scoreRS.setRecord(i+1, oldScore, 0, oldScore.length);
} else {
break;
}
}
scoreRS.setRecord(i+1, data, 0, data.length);
}
Note that the "new String(oldScore, 7, 8)" assumes that each character is one byte, although it's using the platform's default character encoding (if that's UTF-8 and there are e.g. Japanese characters in the player's name, they'll take up more than 1 byte). If you're only supporting one-byte encodings, you should probably use 'new String(oldScore, 7, 8, "ISO8859_1")' (ISO8859_1, also known as "Iso Latin 1", is the CLDC's default encoding and likely to be supported on all MIDP devices). And note the implicit maximum high score of 99999999.
(The line "data = null" is unnecessary, as the variable "data" will be going out of scope soon anyway).
Method clearBuf inserts MAX_REC_LEN space characters at the start of recBuf, each time pushing the existing contents forward. Then it cuts recBuf's length to MAX_REC_LEN, chopping off those existing contents anyway. This is horrible code, as lots of unnecessary copying is done during each "insert". Here's slightly better code:
recBuf.setLength(0);
for(int i=0; i<MAX_REC_LEN; i++)
{
recBuf.append(' ');
}
Method clearBuf should also be 'private', not 'public', as it should only be called from method stringToBufferToBytes anyway.
Method stringToBufferToBytes tries to create a byte array with the name and score in it, in the platform's default character encoding. It does this by inserting the name at the start, and the score at character position 7. (If the name is more than 7 characters long, this won't work properly of course). Since it's using "insert", the end result is going to be something like this: "Fred 12345 ", i.e. lots of spaces at the end. It then converts this to bytes (again, use 'getBytes("ISO8859_1")' if you're assuming one-byte characters). This method should also be 'private', not 'public', since you're probably not calling it from any other class.
Some final observations:
- The class has a field called "storeRS", which is used by checkInitScore and initScore but overridden by a local variable "storeRS" in saveScore
- It has a field called "data" which is never used, as it is overridden by a local variable "data" in saveScore
- I'd recommend using a format like "Fred:12345", and searching for the ':' - that way you could just use the platform's default character encoding to convert to and from byte arrays
- the class is missing a couple of features: there's no method to get a list of the high scores for display, and method saveScore should probably return a boolean saying whether the new score was a high score
I hope those are enough comments for now
 
Glenn Castro
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for your reply!
BTW, can tell me of some tutorial sites regarding rms and/or j2me for Nokia Series 40 and 60.
 
James Reilly
wrangler
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forum Nokia has a Games section (www.forum.nokia.com/games),
and a Java section (www.forum.nokia.com/java,
e.g. "Code and Examples" subsection).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic