1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
| package game;
import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.AlertType; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.TextField; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; import javax.microedition.rms.RecordComparator; import javax.microedition.rms.RecordEnumeration; import javax.microedition.rms.RecordStore;
public class HighScore extends MIDlet implements CommandListener { TextField name, score; Command add, show, exit, home; private String HS; private Form f;
protected void destroyApp(boolean arg0) throws MIDletStateChangeException { }
protected void pauseApp() { }
protected void startApp() throws MIDletStateChangeException { f = new Form("High scores");
f.append(name = new TextField("Name", "", 255, TextField.ANY)); f.append(score = new TextField("Score", "", 10, TextField.NUMERIC));
f.addCommand(add = new Command("Add new score", Command.OK, 1)); f.addCommand(show = new Command("Retrieve scores", Command.OK, 2)); f.addCommand(exit = new Command("Exit", Command.EXIT, 1));
f.setCommandListener(this);
Display.getDisplay(this).setCurrent(f); } public void commandAction(Command c, Displayable d) { if(c == home) { Display.getDisplay(this).setCurrent(f); } if(c == exit) { notifyDestroyed(); } else if(c == add) { try { addNewScore(name.getString(), score.getString());
showConfirm("Score correctly added"); } catch(Exception e) { showError(e.toString()); } } else if(c == show) { try { String[][] scores = retrieveHighScores();
System.out.println("HIGH SCORES");
for(int i = 0; i < scores.length; i++) { System.out.println("SCORE: " + scores[i][0] + " - " + scores[i][1]); HS = ("SCORE: " + scores[i][0] + " - " + scores[i][1]); } showConfirm("Scores correctly retrieved"); } catch(Exception e) { showError(e.toString()); } } } void showConfirm(String text) { Display.getDisplay(this).setCurrent(new Alert("Score Added", text, null, AlertType.CONFIRMATION)); Form highscore = new Form("Your Score"); highscore.append(HS);
highscore.addCommand(home = new Command("Home", Command.OK, 1)); highscore.addCommand(exit = new Command("Exit", Command.EXIT, 1));
highscore.setCommandListener(this); Display.getDisplay(this).setCurrent(highscore); } void showError(String error) { Display.getDisplay(this).setCurrent(new Alert("Error", error, null, AlertType.ERROR)); }
void addNewScore(String name, String score) throws Exception { RecordStore highscore = RecordStore.openRecordStore("High Score", true);
String str = new String(name + "," + score); byte [] strb = str.getBytes(); highscore.addRecord(strb, 0, strb.length);
highscore.closeRecordStore(); } String[][] retrieveHighScores() throws Exception { String[][] scores;
RecordStore highscore = RecordStore.openRecordStore("High Score", true);
RecordEnumeration enumerator = highscore.enumerateRecords(null, new ScoresComparator(), false); int id, index, separator; byte[] record; String str;
index = 0;
scores = new String[enumerator.numRecords()][2];
while (enumerator.hasNextElement( )) { id = enumerator.nextRecordId( ); record = highscore.getRecord(id); str = new String(record); separator = str.indexOf(',');
scores[index][0] = str.substring(0, separator); scores[index][1] = str.substring(separator + 1);
index++; } highscore.closeRecordStore();
return scores; }
class ScoresComparator implements RecordComparator { public int compare(byte[] arg0, byte[] arg1) { int score0 = 0, score1 = 0;
for(int i = 0; i < arg0.length; i++) { if(arg0[i] == ',') { score0 = Integer.parseInt(new String(arg0, i + 1, arg0.length - i - 1)); } } for(int i = 0; i < arg1.length; i++) { if(arg1[i] == ',') { score1 = Integer.parseInt(new String(arg1, i + 1, arg1.length - i - 1)); } }
if(score0 < score1) return 1; else if(score0 == score1) return 0; else return -1; } } } |