FileDocCategorySizeDatePackage
WormScore.javaAPI DocJ2ME MIDP 2.05686Thu Nov 07 12:02:20 GMT 2002example.wormgame

WormScore

public class WormScore extends Object
The WormScore keeps track of the high scores for each of the worm levels. All access to the scores occur through static methods. There can never be a WormScore object. We cache the scores and names for quick access during game play. The calling application must first call openHighScores() to open the score database. Finally, a closeHighScores() must be called to release system resources.

Fields Summary
private static short[]
highScore
Array of high scores for each game level.
private static String[]
highScoreName
Array of player names for each high score level.
private static javax.microedition.rms.RecordStore
myStore
Current score for this game.
private static boolean
highScoresHaveBeenInit
Is the internal score struct intialized?
Constructors Summary
private WormScore()
Default constructor can not be instantiated.

 /* = false; */

           
      
    
Methods Summary
static voidcloseHighScores()
Close the high score file

	if (myStore != null) {
	    try {
		myStore.closeRecordStore();
	    } catch (RecordStoreException frse) {
	    }
	    myStore = null;
	}
    
static shortgetHighScore(int level)
Return the high score for a given level.

param
level current level for high score check
return
numeric value for highest score at the requested level

	if (!highScoresHaveBeenInit) {
	    openHighScores();               // Force scores to be initialized
	    closeHighScores();
	}
	return highScore[level];
    
static java.lang.StringgetHighScoreName(int level)
Return the high score name for a given level.

param
level current level for high score check
return
name for highest score at the requested level

	if (!highScoresHaveBeenInit) {
	    openHighScores();               // Force scores to be initialized
	    closeHighScores();
	}
	return highScoreName[level];
    
private static voidinitializeScores()
Initialize all high scores to 0.

	/* Initialize the score store */
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	DataOutputStream dos = new DataOutputStream(baos);
	byte[] b;

	try {
	    try {
		dos.writeShort(0);
		dos.writeUTF("");
		b = baos.toByteArray();
		dos.close();
	    } catch (IOException ioe) {
		throw new RecordStoreException();
	    }
	    
	    
	    for (int i = 0; i < WormPit.MAX_LEVELS; i++) {
		myStore.addRecord(b, 0, b.length);
	    }
	} catch (RecordStoreException rse) {
	    /* Silently fail; exception to read high score is non-critical */
	    closeHighScores();
	}
    
static voidopenHighScores()
Open the high score storage file. If the file doesn't exist, initialize all high scores to 0.

	try {
	    myStore = RecordStore.openRecordStore("HighScores", true);

	    if (highScoresHaveBeenInit) return;

	    /* Intialize the internal score structures */
	    if (myStore.getNumRecords() == 0) {
		initializeScores();
	    } else {
		/* Read high score store */
		ByteArrayInputStream bais;
		DataInputStream      dis;
		byte                 data[];
		
		for (int i = 0; i < WormPit.MAX_LEVELS; i++) {
		    data = myStore.getRecord(i+1);
		    if (data != null) {
			try {
			    bais = new ByteArrayInputStream(data);
			    dis  = new DataInputStream(bais);
			    highScore[i]     = dis.readShort();
			    highScoreName[i] = dis.readUTF();
			    dis.close();
			} catch (IOException ioe) {
			}
		    }
		}
	    }
	    highScoresHaveBeenInit = true;
	    
	} catch (RecordStoreException rse) {
	    /* Silently fail; exception to read high score is non-critical */
	}
    
static voidsetHighScore(int level, int newScore, java.lang.String name)
Save high score for posterity.

param
level current game level
param
newScore current game score to be recorded
param
name current user name to be recorded

	ByteArrayOutputStream baos;
	DataOutputStream      das;
	byte[]                data;

	/* Only save score if it's higher */
	if (newScore <= highScore[level]) {
	    return;
	}

	try {
	    try {
		baos = new ByteArrayOutputStream();
		das = new DataOutputStream(baos);

		das.writeShort((short)newScore);
		das.writeUTF(name);
		data = baos.toByteArray();
		das.close();
	    } catch (IOException ioe) {
		throw new RecordStoreException();
	    }

            if (myStore == null) {
		openHighScores();
		myStore.setRecord(level + 1, data, 0, data.length);
		closeHighScores();
	    } else {
		myStore.setRecord(level + 1, data, 0, data.length);
	    }
	} catch (RecordStoreException rse) {
	    /* Silently fail; exception to save high score is non-critical */
	}

	highScore[level]     = (short)newScore;
	highScoreName[level] = name;