Scorepublic class Score extends Object Keep track of the last level played.
For each level keep track of the number of moves.
The scores are kept in a RecordStore named PushPuzzleScores.
There are two types of records:
- Last level
- Level history
Name of level (either resource name "/..." or...) |
Fields Summary |
---|
private int | levelId | private byte[] | levelRec | private static final int | LEVEL_LEN | private static final byte | LEVEL_TAG | private int | scoreId | private byte[] | scoreRec | private static final int | SCORE_LEN | private static final byte | SCORE_TAG | private RecordStore | store |
Constructors Summary |
---|
Score() // Record store, null if not open
/*
* Construct a new Score handler.
*/
store = null;
levelId = 0;
levelRec = new byte[LEVEL_LEN];
levelRec[0] = LEVEL_TAG;
putInt(levelRec, 1, 0);
putInt(levelRec, 5, 0);
scoreId = 0;
scoreRec = new byte[SCORE_LEN];
scoreRec[0] = SCORE_TAG;
putInt(scoreRec, 1, 0);
|
Methods Summary |
---|
void | close()Close the store.
try {
if (store != null) {
store.closeRecordStore();
}
} catch (RecordStoreException ex) {
ex.printStackTrace();
}
| private int | getInt(byte[] buf, int offset)Get an integer from an array.
return (buf[offset+0] & 0xff) << 24 |
(buf[offset+1] & 0xff) << 16 |
(buf[offset+2] & 0xff) << 8 |
(buf[offset+3] & 0xff);
| int | getLevel()Retrieve the level from the level record. It should
have already been read from the store or created.
The first byte is a tag, the second byte the level.
return getInt(levelRec, 1);
| int | getMoves()Get the number of moved on the current level.
return getInt(scoreRec, 9);
| int | getPushes()Get the number of pushes on the current level.
return getInt(scoreRec, 5);
| int | getTheme()Get the current Theme number.
return getInt(levelRec, 5);
| boolean | open()Open the record store and locate
the record with the level number in it.
try {
store = RecordStore.openRecordStore("PushPuzzleScores", true);
} catch (RecordStoreException ex) {
}
if (store == null)
return false;
try {
/*
* Locate the record containing the level.
*/
levelId = 0;
RecordEnumeration enum = store.enumerateRecords(null, null, false);
while (enum.hasNextElement()) {
int ndx = enum.nextRecordId();
if (store.getRecordSize(ndx) == LEVEL_LEN) {
int l = store.getRecord(ndx, levelRec, 0);
if (l == LEVEL_LEN &&
levelRec[0] == LEVEL_TAG) {
levelId = ndx;
break;
}
}
}
} catch (RecordStoreException ex) {
ex.printStackTrace();
return false;
}
return true;
| private void | putInt(byte[] buf, int offset, int value)Put an integer to an array
buf[offset+0] = (byte)((value >> 24) & 0xff);
buf[offset+1] = (byte)((value >> 16) & 0xff);
buf[offset+2] = (byte)((value >> 8) & 0xff);
buf[offset+3] = (byte)((value >> 0) & 0xff);
| boolean | readScore(int level)Read the score for the current level.
Read through the records looking for the one for this level.
try {
scoreId = 0;
// Locate the matching record
RecordEnumeration enum = store.enumerateRecords(null, null, false);
while (enum.hasNextElement()) {
int ndx = enum.nextRecordId();
if (store.getRecordSize(ndx) == SCORE_LEN) {
int l = store.getRecord(ndx, scoreRec, 0);
if (l == SCORE_LEN &&
scoreRec[0] == SCORE_TAG &&
getInt(scoreRec, 1) == level) {
scoreId = ndx;
return true;
}
}
}
} catch (RecordStoreException ex) {
ex.printStackTrace();
return false;
}
// No record found, start fresh
scoreRec[0] = SCORE_TAG;
putInt(scoreRec, 1, level);
putInt(scoreRec, 5, 0);
putInt(scoreRec, 9, 0);
return true;
| boolean | setLevel(int level, int theme)Set the level and theme into the RecordStore.
putInt(levelRec, 1, level);
putInt(levelRec, 5, theme);
putInt(scoreRec, 1, level);
if (store == null)
return false;
try {
if (levelId == 0) {
levelId = store.addRecord(levelRec, 0, levelRec.length);
} else {
store.setRecord(levelId, levelRec, 0, levelRec.length);
}
} catch (RecordStoreException ex) {
System.out.println("RecordStoreException");
ex.printStackTrace();
return false;
}
readScore(level); // get the score for the level
return true;
| boolean | setLevelScore(int pushes, int moves)Set the updated score to the RecordStore.
// Update the scores in the buffer.
putInt(scoreRec, 5, pushes);
putInt(scoreRec, 9, moves);
try {
// Write/Add the record to the store
if (scoreId == 0) {
scoreId = store.addRecord(scoreRec, 0, scoreRec.length);
} else {
store.setRecord(scoreId, scoreRec, 0, scoreRec.length);
}
} catch (RecordStoreException ex) {
ex.printStackTrace();
return false;
}
return true;
|
|