分享
 
 
 

用RMS存储游戏积分

王朝other·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

import Java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.EOFException;

import java.io.IOException;

import javax.microedition.midlet.MIDlet;

import javax.microedition.midlet.MIDletStateChangeException;

import javax.microedition.rms.RecordComparator;

import javax.microedition.rms.RecordEnumeration;

import javax.microedition.rms.RecordFilter;

import javax.microedition.rms.RecordStore;

import javax.microedition.rms.RecordStoreException;

/**

* A class used for storing and showing game scores.

*/

public class RMSGameScores extends MIDlet implements RecordFilter,

RecordComparator {

/*

* The RecordStore used for storing the game scores.

*/

private RecordStore recordStore = null;

/*

* The player name to use when filtering.

*/

public static String playerNameFilter = null;

/**

* The constUCtor opens the underlying record store, creating it if

* necessary.

*/

public RMSGameScores() {

// Create a new record store for this example

try {

recordStore = RecordStore.openRecordStore("scores", true);

} catch (RecordStoreException rse) {

System.out.println("Record Store Exception in the ctor." + rse);

rse.printStackTrace();

}

}

/**

* startApp()

*/

public void startApp() throws MIDletStateChangeException {

RMSGameScores rmsgs = new RMSGameScores();

rmsgs.addScore(100, "Alice");

rmsgs.addScore(120, "Bill");

rmsgs.addScore(80, "Candice");

rmsgs.addScore(40, "Dean");

rmsgs.addScore(200, "Ethel");

rmsgs.addScore(110, "Farnsworth");

rmsgs.addScore(220, "Alice");

RMSGameScores.playerNameFilter = "Alice";

System.out

.println("Print all scores followed by Scores for Farnsworth");

rmsgs.printScores();

}

/*

* Part of the RecordFilter interface.

*/

public boolean matches(byte[] candidate) throws IllegalArgumentException {

// If no filter set, nothing can match it.

if (this.playerNameFilter == null) {

return false;

}

ByteArrayInputStream bais = new ByteArrayInputStream(candidate);

DataInputStream inputStream = new DataInputStream(bais);

String name = null;

try {

int score = inputStream.readInt();

name = inputStream.readUTF();

} catch (EOFException eofe) { System.out.println(eofe);

eofe.printStackTrace();

} catch (IOException eofe) {

System.out.println(eofe);

eofe.printStackTrace();

}

return (this.playerNameFilter.equals(name));

}

/*

* Part of the RecordComparator interface.

*/

public int compare(byte[] rec1, byte[] rec2) {

// Construct DataInputStreams for extracting the scores from

// the records.

ByteArrayInputStream bais1 = new ByteArrayInputStream(rec1);

DataInputStream inputStream1 = new DataInputStream(bais1);

ByteArrayInputStream bais2 = new ByteArrayInputStream(rec2);

DataInputStream inputStream2 = new DataInputStream(bais2);

int score1 = 0;

int score2 = 0;

try {

// Extract the scores.

score1 = inputStream1.readInt();

score2 = inputStream2.readInt();

} catch (EOFException eofe) {

System.out.println(eofe);

eofe.printStackTrace();

} catch (IOException eofe) {

System.out.println(eofe);

eofe.printStackTrace();

}

// Sort by score

if (score1 > score2) {

return RecordComparator.FOLLOWS;

} else if (score1 < score2) {

return RecordComparator.PRECEDES;

} else {

return RecordComparator.EQUIVALENT;

}

}

/**

* Add a new score to the storage.

*

* @param score

* the score to store.

* @param playerName

* the name of the play achieving this score.

*/

public void addScore(int score, String playerName) {

// Each score is stored in a separate record, formatted with

// the score, followed by the player name.

ByteArrayOutputStream baos = new ByteArrayOutputStream();

DataOutputStream outputStream = new DataOutputStream(baos);

try {

// Push the score into a byte array.

outputStream.writeInt(score);

// Then push the player name.

outputStream.writeUTF(playerName);

} catch (IOException ioe) {

System.out.println(ioe);

ioe.printStackTrace();

}

// Extract the byte array

byte[] b = baos.toByteArray();

try {

// Add it to the record store

recordStore.addRecord(b, 0, b.length);

} catch (RecordStoreException rse) {

System.out.println(rse);

rse.printStackTrace();

}

}

/**

* A helper method for the printScores methods.

*/

private void printScoresHelper(RecordEnumeration re) {

try {

while (re.hasNextElement()) {

int id = re.nextRecordId();

ByteArrayInputStream bais = new ByteArrayInputStream(

recordStore.getRecord(id));

DataInputStream inputStream = new DataInputStream(bais);

try {

int score = inputStream.readInt();

String playerName = inputStream.readUTF();

System.out.println(playerName + " = " + score);

} catch (EOFException eofe) {

System.out.println(eofe);

eofe.printStackTrace();

}

}

} catch (RecordStoreException rse) {

System.out.println(rse);

rse.printStackTrace();

} catch (IOException ioe) {

System.out.println(ioe);

ioe.printStackTrace();

}

}

/**

* This method prints all of the scores sorted by game score.

*/

public void printScores() {

try {

// Enumerate the records using the comparator implemented

// above to sort by game score.

// No RecordFilter here. All records in the RecordStore

RecordEnumeration re = recordStore.enumerateRecords(null, this,

true);

// Print all scores

System.out.println("Print all scores...");

printScoresHelper(re);

// Enumerate records respecting a RecordFilter

re = recordStore.enumerateRecords(this, this, true);

//Print scores for Farnsworth

System.out.println("Print scores for : " + this.playerNameFilter);

printScoresHelper(re);

} catch (RecordStoreException rse) {

System.out.println(rse);

rse.printStackTrace();

}}

/**

* pauseApp()

*/

public void pauseApp() {

System.out.println("pauseApp()");

}

/**

* destroyApp()

*

* This closes our open RecordStore when we are destroyed.

*

* @param cond

* true if this is an unconditional destroy false if it is not

* (ignored here and treated as unconditional)

*/

public void destroyApp(boolean cond) {

System.out.println("destroyApp( )");

try {

if (recordStore != null)

recordStore.closeRecordStore();

} catch (Exception ignore) {

// ignore this

}

}

}

(出处:http://www.knowsky.com)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有