Итак, я делаю игру-лабиринт на Java, чтобы учиться во время написания кода.
Для моей игры-лабиринта игроку нужно добраться до выхода лабиринта как можно скорее. А время, которое у него есть, нужно сохранить в зашифрованном текстовом файле.
Итак, у меня есть пакет Highscores, объединяющий несколько классов. Код более-менее работает, выводит в консоль. Теперь мне нужно, чтобы этот вывод выводился на JPanel рядом с моим лабиринтом. Я добавил дополнительную информацию в код
Вот мой лучший класс:
Код: Выделить всё
public class Highscore {
// An arraylist of the type "score" we will use to work with the scores inside the class
private ArrayList scores;
// The name of the file where the highscores will be saved
private static final String highscorefile = "Resources/scores.dat";
//Initialising an in and outputStream for working with the file
ObjectOutputStream output = null;
ObjectInputStream input = null;
public Highscore() {
//initialising the scores-arraylist
scores = new ArrayList();
}
public ArrayList getScores() {
loadScoreFile();
sort();
return scores;
}
private void sort() {
ScoreVergelijken comparator = new ScoreVergelijken();
Collections.sort(scores, comparator);
}
public void addScore(String name, int score) {
loadScoreFile();
scores.add(new Score(name, score));
updateScoreFile();
}
public void loadScoreFile() {
try {
input = new ObjectInputStream(new FileInputStream(highscorefile));
scores = (ArrayList) input.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 (output != null) {
output.flush();
output.close();
}
} catch (IOException e) {
System.out.println("[Laad] IO Error: " + e.getMessage());
}
}
}
public void updateScoreFile() {
try {
output = new ObjectOutputStream(new FileOutputStream(highscorefile));
output.writeObject(scores);
} catch (FileNotFoundException e) {
System.out.println("[Update] FNF Error: " + e.getMessage() + ",the program will try and make a new file");
} catch (IOException e) {
System.out.println("[Update] IO Error: " + e.getMessage());
} finally {
try {
if (output != null) {
output.flush();
output.close();
}
} catch (IOException e) {
System.out.println("[Update] Error: " + e.getMessage());
}
}
}
public String getHighscoreString() {
String highscoreString = "";
int max = 10;
ArrayList 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;
}
Вот мой основной класс:
Код: Выделить всё
public class Main {
public static void main(String[] args) {
Highscore hm = new Highscore();
hm.addScore("Bart",240);
hm.addScore("Marge",300);
hm.addScore("Maggie",220);
hm.addScore("Homer",100);
hm.addScore("Lisa",270);
hm.addScore(LabyrinthProject.View.MainMenu.username,290);
System.out.print(hm.getHighscoreString());
} }
Код: Выделить всё
public class Score implements Serializable {
private int score;
private String naam;
public Score() {
}
public int getScore() {
return score;
}
public String getNaam() {
return naam;
}
public Score(String naam, int score) {
this.score = score;
this.naam = naam;
}
Класс ScoreVergelijken (что означает CompareScore)
Код: Выделить всё
public class ScoreVergelijken implements Comparator {
public int compare(Score score1, Score score2) {
int sc1 = score1.getScore();
int sc2 = score2.getScore();
if (sc1 > sc2){
return -1; // -1 means first score is bigger then second score
}else if (sc1 < sc2){
return +1; // +1 means that score is lower
}else{
return 0; // 0 means score is equal
}
} }
А также, как использовать эти рекорды и хранить их в зашифрованном виде в текстовом файле. Как мне этого добиться?
С уважением, начинающий студент, изучающий Java.
Подробнее здесь: https://stackoverflow.com/questions/223 ... core-in-en