highscore liste aus highscores.txt laden

currentStatusIoana
Simona-Ioana Purdila 2025-01-05 15:53:35 +01:00
parent 3d0f808daf
commit f62d6965e4
3 changed files with 59 additions and 18 deletions

View File

@ -6,6 +6,7 @@ import PR2.HitoriSpiel.Utils.Setup;
import javax.swing.table.DefaultTableModel;
import javax.swing.*;
import java.awt.*;
import java.util.Comparator;
import java.util.List;
// aktueller Stand
@ -52,10 +53,10 @@ public class HighscoreDialog extends JDialog {
private void loadHighscores() {
tableModel.setRowCount(0); // Tabelle zurücksetzen
List<HighscoreManager.Highscore> highscores = highscoreManager.getHighscores();
List<HighscoreManager.Highscore> highscores = highscoreManager.getSortedHighscores();
highscores.stream()
.sorted((a, b) -> Integer.compare(b.getScore(), a.getScore())) // Sortierung: Höchste Punkte zuerst
.sorted(Comparator.comparingInt(HighscoreManager.Highscore::getScore)) // Kürzeste Zeit zuerst
.forEach(highscore -> tableModel.addRow(new Object[]{
highscore.getPlayerName(),
highscore.getScore(),

View File

@ -9,11 +9,13 @@ import java.util.concurrent.locks.ReentrantLock;
public class HighscoreManager {
private static final String HIGHSCORE_FILE = "highscores.txt";
private static final String HIGHSCORE_FILE = "Files/highscores.txt";
private static final ReentrantLock fileLock = new ReentrantLock();
// Highscore-Datenstruktur
private final List<Highscore> highscoreList = new ArrayList<>();
public HighscoreManager() {
loadHighscores();
}
@ -33,16 +35,25 @@ public class HighscoreManager {
// Highscores laden
private void loadHighscores() {
fileLock.lock();
try (BufferedReader reader = new BufferedReader(new FileReader(HIGHSCORE_FILE))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 3) { // Name, Punkte, Spielfeld
highscoreList.add(new Highscore(parts[0], Integer.parseInt(parts[1]), parts[2]));
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(HIGHSCORE_FILE)) {
if (inputStream == null) {
System.out.println("Datei highscores.txt nicht im Classpath gefunden.");
return;
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
highscoreList.clear(); // Liste zurücksetzen
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 3) { // Name, Punkte, Spielfeld
String playerName = parts[0].trim();
int score = Integer.parseInt(parts[1].trim());
String boardName = parts[2].trim();
highscoreList.add(new Highscore(playerName, score, boardName));
}
}
}
} catch (FileNotFoundException e) {
System.out.println("Highscore-Datei nicht gefunden. Sie wird nach dem ersten Speichern erstellt.");
} catch (IOException | NumberFormatException e) {
System.err.println("Fehler beim Laden der Highscores: " + e.getMessage());
} finally {
@ -51,10 +62,11 @@ public class HighscoreManager {
}
// Highscores speichern
private void saveHighscores() {
fileLock.lock();
try (BufferedWriter writer = new BufferedWriter(new FileWriter(HIGHSCORE_FILE))) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter("highscores.txt"))) {
for (Highscore highscore : highscoreList) {
writer.write(highscore.getPlayerName() + "," + highscore.getScore() + "," + highscore.getBoardName());
writer.newLine();
@ -66,9 +78,15 @@ public class HighscoreManager {
}
}
// Highscores abrufen
public List<Highscore> getHighscores() {
return Collections.unmodifiableList(highscoreList);
// Highscores abrufen (sortiert nach kürzester Zeit)
public List<Highscore> getSortedHighscores() {
fileLock.lock();
try {
highscoreList.sort(Comparator.comparingInt(Highscore::getScore)); // Kürzeste Zeit zuerst
return new ArrayList<>(highscoreList); // Kopie der Liste zurückgeben
} finally {
fileLock.unlock();
}
}
// Alte Highscores bereinigen
@ -84,6 +102,29 @@ public class HighscoreManager {
}
}
// Highscores sortieren (kürzeste Zeit zuerst)
private void sortHighscores() {
highscoreList.sort(Comparator.comparingInt(Highscore::getScore));
}
// Highscores abrufen (sortiert mit Nummerierung)
public List<String> getFormattedHighscores() {
fileLock.lock();
try {
sortHighscores();
List<String> formattedHighscores = new ArrayList<>();
int rank = 1;
for (Highscore highscore : highscoreList) {
formattedHighscores.add(rank + ". " + highscore.getPlayerName() + " - " + highscore.getScore() + "s");
rank++;
}
return formattedHighscores;
} finally {
fileLock.unlock();
}
}
// Innere Highscore-Klasse
public static class Highscore {
private final String playerName;

View File

@ -1,3 +1,2 @@
Name, Zeit (Sekunden), Spielfeld
NameTest, 123 Sekunden, Hitori4x4_leicht.csv
NameTest,123,Hitori4x4
Ioana,234,Hitori8x8