store and appear 5 top scores of a Game

  • Replies:5
Leonidas Savvides
  • Forum posts: 45

Jun 17, 2012, 3:06:34 PM via Website

Want: store and appear 5 top scores of a Game. Problem reading/writing to an array from Shared Preferences(Sh Pref).

scoreSaveReturn():
Get from Sh Pref store in array int[5], delete all Sh Pref, if score appropriate add it to array, populate Sh Pref and return top five scores as string.

THE 2ND block detected erroneous (if disabled - works with fixed value returned) WHERE THE PROBLEM? 1ST AFTER FOLLOW SECOND IN CODE SEQUENCE
1// this block detected NO ERROR
2
3savedHighScores = getSharedPreferences("highScores", MODE_PRIVATE);
4
5private String scoreSaveReturn(int totalPoints) {
6 SharedPreferences.Editor preferencesEditor = savedHighScores.edit();
7 String[] tags = savedHighScores.getAll().keySet().toArray(new String[0]);
8 int[] scores = new int[5];
9....................
same method(scoreSaveReturn) continues below
1...............
2 // this block detected erroneous WHERE THE PROBLEM? Tried to populate array int[5] from Shared Pref.
3for (int k = 0; k<5; k++) {
4 scores[k]=savedHighScores.getInt(tags[k],0);
5}
6 preferencesEditor.clear();
7 preferencesEditor.apply();
.............other code works OK.

Reply
Jeremiah
  • Forum posts: 775

Jun 19, 2012, 7:12:09 AM via Website

What is the error message you are getting?

Reply
Leonidas Savvides
  • Forum posts: 45

Jun 19, 2012, 10:53:26 AM via Website

is a run time(live run on emulator_after finish game_and this method called to get(or and add current score) 5 top scores) error.. normal that crashed...

basically any open source or ready made script of top5scores/addCurrentIfAppropriate?

Reply
Leonidas Savvides
  • Forum posts: 45

Jun 19, 2012, 5:05:20 PM via Website

scores[] needed be of type Integer or int?

error get on run-time always(end of game points generated): The application FlagQuizGame(process com.deitel.flagquizgame) has stopped unexpectedly. Please try again.

for debug purposes: where I can save temporary code, to try other code (in eclipse)?

— modified on Jun 19, 2012, 6:07:59 PM

Reply
Jeremiah
  • Forum posts: 775

Jun 20, 2012, 1:58:36 AM via Website

Can you see the logcat of the emulator? It should tell you specifically why it stopped (null pointer... etc) and on which line number caused it. If your running the emulator manually (not with eclipse) to see the logcat output from the command line type:

adb logcat

scores[] should be int I believe.

To make a copy of your code for debugging purposes, I would just open the folder for your project and copy the source .java files to another folder. The to revert it back copy them back to your projects folder.

You may want to try Scoreloop or OpenFeint for highscores... http://www.scoreloop.com/

Just a hunch but maybe your getting an array out of bounds exception if your String Array tags [] has less than 5 elements.

try:

for (int k = 0; k<5; k++) {
if ( java.lang.reflect.Array.getLength (tags)>k) scores[k]=savedHighScores.getInt(tags[k],0);
else scores[k]=0;
}

Reply
Leonidas Savvides
  • Forum posts: 45

Jul 14, 2012, 11:39:21 PM via Website

This code output for String ScoresTop5

0:0
1:0
2:0
3:56 // here (1)adds score rather create another entry 38+18... also (2)higher on top, well?
4:0

[code]
//------------------------------------------------------------------------------
@SuppressLint({ "NewApi", "NewApi" })
private String scoreSaveReturn(int totalPoints) {

SharedPreferences.Editor preferencesEditor = savedHighScores.edit();

String[] tags = savedHighScores.getAll().keySet().toArray(new String[0]);
int[] scores = new int[5];

for (int k = 0; k<5; k++) {
if ( java.lang.reflect.Array.getLength(tags)>k) scores[k]=savedHighScores.getInt(tags[k],0);
else scores[k]=0;
}


preferencesEditor.clear();
preferencesEditor.apply();

int pos;

int scores2[]= new int[5];

for (int i=0; i<5; i++) {
scores2[i]=scores[i];
}

for (int k = 0; k<5; k++) {
if (scores[k] <= totalPoints) {
pos=k;
scores[pos]=totalPoints;
for (int i=k; i<3; i++) {
scores[i+1]=scores2[i];
}
break;
}
}

for (int k = 0; k<5; k++) { // tags.length
preferencesEditor.putInt(String.format("%d",k), scores[k]);

}
preferencesEditor.apply();

// get top scores
String ScoresTop5 = "Top Scores:\n";

for (int k = 0; k<5; k++) {

ScoresTop5 = ScoresTop5 + k + ": " + savedHighScores.getInt(tags[k],0) +"\n";
}
return ScoresTop5;
}

//------------------------------------------------------------------------------
[/code]

— modified on Jul 14, 2012, 11:40:10 PM

Reply