📄 hiscore.java
字号:
package com.centerscore.game;
import javax.microedition.rms.*;
import java.util.Vector;
import java.io.*;
class HiScore {
/* Start Modified 07-05-2003 for chinese support */
static final int NUM_HISCORES = 5;
/* End Modified 07-05-2003 */
private String[] m_Arr1Names = new String[NUM_HISCORES];
private int[] m_Arr1Scores = new int[NUM_HISCORES];
private static final String FILE_NAME = "QbertScores";
void writeFile() {
RecordStore rs = null;
try {
rs = RecordStore.openRecordStore(FILE_NAME, false);
myWriteFile(rs);
} catch (Exception e) {}
finally {
try {
if (rs != null)
rs.closeRecordStore();
} catch (Exception e) {}
}
}
private void myWriteFile(RecordStore rs) throws IOException, Exception {
// create a byte stream to output
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//(10 + 4) * NUM_HISCORES * NUM_CATEGORIES);
// wrap in something that understands our format
DataOutputStream dos = new DataOutputStream(baos);
// copy stuff over
for (int difficulty = 0; difficulty < 3; difficulty++) {
for (int scoreIndex = 0; scoreIndex < NUM_HISCORES; scoreIndex++) {
dos.writeUTF(m_Arr1Names[scoreIndex]);
dos.writeInt(m_Arr1Scores[scoreIndex]);
}
}
// done copy
// transfer over to record
byte[] buf = baos.toByteArray();
try {
rs.setRecord(1, buf, 0, buf.length); // records start at 1
}
catch (InvalidRecordIDException ire) {
// add record if it doesn't exist
rs.addRecord(buf, 0, buf.length);
}
}
// sorted default
void readFile(String[] arrDefaultNames, int[] arr1DefaultScores) {
RecordStore rs = null;
try {
rs = RecordStore.openRecordStore(FILE_NAME, true);
// 1) get the records
// 2) sort record
// 3) rewrite records
if (rs.getNumRecords() > 0) {
byte[] data = rs.getRecord(1);
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data));
for (int difficulty = 0; difficulty < 3; difficulty++) {
for (int scoreIndex = 0; scoreIndex < NUM_HISCORES; scoreIndex++) {
m_Arr1Names[scoreIndex] = dis.readUTF();
m_Arr1Scores[scoreIndex] = dis.readInt();
}
}
} else {
//default assignments
for (int difficulty = 0; difficulty < 3; difficulty++) {
for (int scoreIndex = 0; scoreIndex < NUM_HISCORES; scoreIndex++) {
m_Arr1Names[scoreIndex] = arrDefaultNames[scoreIndex];
m_Arr1Scores[scoreIndex] = arr1DefaultScores[scoreIndex];
}
}
}
} catch (Exception e) {}
finally {
if (rs != null) {
try {
rs.closeRecordStore();
} catch (Exception e) {} // ignore.. we're closing out
}
}
}
final int getIndex(int score) {
// score in this case is points.. so highest points on top!!
for (int i = NUM_HISCORES - 1; i >= 0; i--) {
if (score < m_Arr1Scores[i] || (score == m_Arr1Scores[i])) {
if (i + 1 >= NUM_HISCORES)
return -1;
return i + 1;
}
}
return 0;
}
String getName(int index) {
if (index < 0 || index >= NUM_HISCORES) {
return "";
}
return m_Arr1Names[index];
}
int getScore(int index) {
if (index < 0 || index >= NUM_HISCORES) {
return 0;
}
return m_Arr1Scores[index];
}
int addScore(String name, int points) {
int i = getIndex(points);
// if we're higher then the current (point), we want to shove under it
for (int j = NUM_HISCORES - 1; j > i; j--) {
m_Arr1Scores[j] = m_Arr1Scores[j - 1];
m_Arr1Names[j] = m_Arr1Names[j - 1];
}
/* Start Modified 07-05-2003 for chinese support */
/*
String tmp = "";
int len = name.length();
for (int k = 0; k < len; k++) {
char c = name.charAt(k);
if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
tmp += c;
} else {
tmp += " ";
}
}
*/
m_Arr1Names[i] = name;
/* End Modified 07-05-2003 */
m_Arr1Scores[i] = points;
return i;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -