📄 score.java
字号:
/* * @(#)Score.java 1.4 01/04/04 * Copyright (c) 1999-2001 Sun Microsystems, Inc. All Rights Reserved. */package examples.sokoban;import javax.microedition.rms.*;/** * Keep track of the last level played. * For each level keep track of the number of moves. * <p> * The scores are kept in a RecordStore named SokoScores. * There are two types of records: * - Last level record containing the last level played. * - Level history consisting of the number of moves and * pushes for that level */class Score { // Values from the current records describing the current level int level; // Current level int npushes; // Current number of pushes for level int nmoves; // Current number of moves for level private byte[] buffer; // Used for all reading and writing // Current level record = {byte LEVEL_TAG; int level;} private int levelId; // The record Id of the level record private static final int LEVEL_LEN = 5; private static final byte LEVEL_TAG = 1; /* * Score for level = * {byte SCORE_TAG; int level, int pushes; int moves;} */ private int scoreId; // The record Id of the current level private static final int SCORE_LEN = 13; private static final byte SCORE_TAG = 2; private RecordStore store; // Record store, null if not open /* * Construct a new Score handler. * Initial values are already set; null for store; * level = 0, levelId = 0, scoreId = 0, */ Score() { buffer = new byte[SCORE_LEN]; } /** * Open the record store and locate * the record with the level number in it. */ boolean open() { try { store = RecordStore.openRecordStore("SokobanScores", true); } catch (RecordStoreException ex) { return false; } try { /* * Locate the record containing the current level */ levelId = 0; RecordEnumeration enum = store.enumerateRecords(null, null, false); while (enum.hasNextElement()) { int ndx = enum.nextRecordId(); int len = store.getRecord(ndx, buffer, 0); if (len == LEVEL_LEN && buffer[0] == LEVEL_TAG) { levelId = ndx; level = getInt(buffer, 1); break; } } } catch (RecordStoreException ex) { return false; } return true; } /** * Set the level and update the level record in the RecordStore. */ boolean setLevel(int level) { this.level = level; buffer[0] = LEVEL_TAG; putInt(buffer, 1, level); if (store == null) return false; try { if (levelId == 0) { levelId = store.addRecord(buffer, 0, LEVEL_LEN); } else { store.setRecord(levelId, buffer, 0, LEVEL_LEN); } } catch (RecordStoreException ex) { return false; } return readScore(level); // get the score for the level } /** * Read the score for the current level. * Read through the records looking for the one for this level. * @param level the level of which to get the last scores */ boolean readScore(int level) { try { // Locate the score record for the requested level scoreId = 0; RecordEnumeration enum = store.enumerateRecords(null, null, false); while (enum.hasNextElement()) { int ndx = enum.nextRecordId(); int len = store.getRecord(ndx, buffer, 0); if (len == SCORE_LEN && buffer[0] == SCORE_TAG && getInt(buffer, 1) == level) { scoreId = ndx; npushes = getInt(buffer, 5); nmoves = getInt(buffer, 9); return true; } } } catch (RecordStoreException ex) { return false; } // No record found, start fresh npushes = 0; nmoves = 0; return true; } /** * Set the updated score to the RecordStore. */ boolean setLevelScore(int pushes, int moves) { npushes = pushes; nmoves = moves; // Update the scores in the buffer. buffer[0] = SCORE_TAG; putInt(buffer, 1, level); putInt(buffer, 5, npushes); putInt(buffer, 9, nmoves); try { // Write/Add the record to the store if (scoreId == 0) { scoreId = store.addRecord(buffer, 0, SCORE_LEN); } else { store.setRecord(scoreId, buffer, 0, SCORE_LEN); } } catch (RecordStoreException ex) { return false; } return true; } /** * Get an integer from an array. */ private int getInt(byte[] buf, int offset) { return (buf[offset+0] & 0xff) << 24 | (buf[offset+1] & 0xff) << 16 | (buf[offset+2] & 0xff) << 8 | (buf[offset+3] & 0xff); } /** * Put an integer to an array */ private void putInt(byte[] buf, int offset, int value) { buf[offset+0] = (byte)(value >> 24); buf[offset+1] = (byte)(value >> 16); buf[offset+2] = (byte)(value >> 8); buf[offset+3] = (byte)(value >> 0); } /** * Close the store. */ void close() { try { if (store != null) { store.closeRecordStore(); } } catch (RecordStoreException ex) { } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -