Methods Summary |
---|
static void | closeHighScores()Close the high score file
if (myStore != null) {
try {
myStore.closeRecordStore();
} catch (RecordStoreException frse) {
}
myStore = null;
}
|
static short | getHighScore(int level)Return the high score for a given level.
if (!highScoresHaveBeenInit) {
openHighScores(); // Force scores to be initialized
closeHighScores();
}
return highScore[level];
|
static java.lang.String | getHighScoreName(int level)Return the high score name for a given level.
if (!highScoresHaveBeenInit) {
openHighScores(); // Force scores to be initialized
closeHighScores();
}
return highScoreName[level];
|
private static void | initializeScores()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 void | openHighScores()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 void | setHighScore(int level, int newScore, java.lang.String name)Save high score for posterity.
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;
|