📄 record.java
字号:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.rms.RecordStore;
/***
* 类名:Record
* 功能:在RMS中存储游戏中玩家的名字和得分
*/
public final class Record
{
public int MAXSIZE;
private RecordStore rs;
private String recordName;
public String name;
public long score;
public Record(String recordName, int maxsize)
{
this.recordName = recordName;
MAXSIZE = maxsize;
score = 0;
name = "AAAA";
}
/**
* 打开指定的RMS。
*/
public final void openRecordStore(boolean createIfNecessary)
throws Exception
{
rs = RecordStore.openRecordStore(recordName, createIfNecessary);
}
public final void closeRecordStore() throws Exception
{
rs.closeRecordStore();
}
/**
* 合成数据;
* */
private final byte[] encode() throws Exception
{
byte[] result;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeUTF(name);
dos.writeLong(score);
result = bos.toByteArray();
dos.close();
bos.close();
return result;
}
/**
* 分解数据;
* */
private final void decode(byte[] data) throws Exception
{
ByteArrayInputStream bis = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bis);
name = dis.readUTF();
score = dis.readLong();
dis.close();
bis.close();
}
/**
* 当前成绩和最后一条记录的成绩比较;默认排序方式为降序;
* */
private final boolean isSave(long score) throws Exception
{
byte[] temp = rs.getRecord(MAXSIZE);
decode(temp);
if (score > this.score)
{
return true;
}
return false;
}
/**
* 保存得分记录前,对以前的记录进行比较;若得分比以前得分高,才保存记录;
* */
public final void saveScore(String name, long score)
{
try
{
openRecordStore(false);
if (isSave(score))
{
this.name = name;
this.score = score;
byte[] temp;
temp = encode();
rs.setRecord(MAXSIZE + 1, temp, 0, temp.length);
insertRecord(score);
}
closeRecordStore();
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**插入记录*/
private final void insertRecord(long score) throws Exception
{
byte[] temp, inserdata, currentdata;
inserdata = rs.getRecord(MAXSIZE + 1);//需要插入的记录
long currentscore;
String currentname;
for (int i = 1; i <= MAXSIZE;)
{
currentdata = rs.getRecord(i);
ByteArrayInputStream bis = new ByteArrayInputStream(currentdata);
DataInputStream dis = new DataInputStream(bis);
currentname = dis.readUTF();
currentscore = dis.readLong();
if (score < currentscore)
{
i++;
}
else
{
temp = currentdata;
rs.setRecord(i, inserdata, 0, inserdata.length);
for (int j = i; j <= MAXSIZE; j++)
{
currentdata = rs.getRecord(j + 1);
rs.setRecord(j + 1, temp, 0, temp.length);
temp = currentdata;
}
return;
}
}
}
/**
* 显示记录;
* 进入排行榜界面时 openRecordStore();
* 返回菜单界面时 closeRecordStore();
* */
public final void loadScore(int recordID)
{
if (recordID <= MAXSIZE)
{
try
{
byte[] temp = rs.getRecord(recordID);
decode(temp);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
/**
* 添加MAXSIZE + 1条默认成绩的记录
* */
public final void addDefaultRecord()
{
try
{
openRecordStore(true);
if (rs.getNumRecords() == 0)
{
byte[] temp = encode();
for (int i = 1; i <= MAXSIZE + 1; i++)
{
rs.addRecord(temp, 0, temp.length);
}
}
closeRecordStore();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -