Dit is de code, als het spel is afgelopen zien we op een correcte manier de score, maar wanneer we het spel opnieuw opstarten en roepen het highscore menu op, dan blijft het leeg.
ScoreBeheren
Score
Vergelijken
ScoreBeheren
Code:
public class ScoreBeheren {
private ArrayList<Score> scores;
// hier worden de highscores opgeslagen
private static final String HIGHSCORE_FILE = "highscore.txt";
//Je moet gegevens kunnen invoegen en kunnen raadplegen
ObjectOutputStream outputStream = null;
ObjectInputStream inputStream = null;
public ScoreBeheren() {
scores = new ArrayList<Score>();
}
public ArrayList<Score> getScores() {
sort();
loadScoreFile();
return scores;
}
private void sort() {
ScoreVergelijken comparator = new ScoreVergelijken();
Collections.sort(scores, comparator);
}
public void addScore(String name, int score) {
scores.add(new Score(name, score));
updateScoreFile();
loadScoreFile();
}
public void loadScoreFile() {
try {
inputStream = new ObjectInputStream(new FileInputStream(HIGHSCORE_FILE));
scores = (ArrayList<Score>) inputStream.readObject();
} catch (FileNotFoundException e) {
System.out.println("[Laad] FNF Error: " + e.getMessage());
} catch (IOException e) {
System.out.println("[Laad] IO Error: " + e.getMessage());
} catch (ClassNotFoundException e) {
System.out.println("[Laad] CNF Error: " + e.getMessage());
} finally {
try {
if (outputStream != null) {
outputStream.flush();
outputStream.close();
}
} catch (IOException e) {
System.out.println("[Laad] Error: " + e.getMessage());
}
}
}
public void updateScoreFile() {
try {
outputStream = new ObjectOutputStream(new FileOutputStream(HIGHSCORE_FILE));
outputStream.writeObject(scores);
} catch (FileNotFoundException e) {
System.out.println("[Update] Error: " + e.getMessage() + ", het programma probeert een nieuw bestand te maken");
} catch (IOException e) {
System.out.println("[Update] Error: " + e.getMessage());
} finally {
try {
if (outputStream != null) {
outputStream.flush();
outputStream.close();
}
} catch (IOException e) {
System.out.println("[Update] Error: " + e.getMessage());
}
}
}
public String getHighscoreString() {
String highscoreString ="";
int max = 10;
ArrayList<Score> scores;
scores = getScores();
int i = 0;
int x = scores.size();
if (x > max) {
x = max;
}
while (i < x) {
highscoreString += (i + 1) + ".\t" + scores.get(i).getNaam() + "\t\t" + scores.get(i).getScore() + "\n";
i++;
}
return highscoreString;
}
}
Code:
import java.io.Serializable;
public class Score implements Serializable {
private int score;
private String naam;
public Score(String naam, int score) {
this.score = score;
this.naam = naam;
}
public int getScore() {
return score;
}
public String getNaam() {
return naam;
}
}
Vergelijken
Code:
import java.util.Comparator;
public class ScoreVergelijken implements Comparator<Score> {
public int compare(Score score1, Score score2) {
int sc1 = score1.getScore();
int sc2 = score2.getScore();
if (sc1 > sc2){
return -1;
}else if (sc1 < sc2){
return +1;
}else{
return 0;
}
}
}