Änderungen an HighscoreManager

Highscore
Simona-Ioana Purdila 2025-01-05 17:36:17 +01:00
parent dcf9c95a99
commit 1a697e3a9c
2 changed files with 20 additions and 20 deletions

View File

@ -2,6 +2,7 @@ package PR2.HitoriSpiel.GUI;
import PR2.HitoriSpiel.Domain.HitoriBoard;
import PR2.HitoriSpiel.Domain.HitoriSolutionLoader;
import PR2.HitoriSpiel.Utils.HighscoreManager;
import PR2.HitoriSpiel.Utils.Setup;
import javax.swing.*;
@ -108,6 +109,7 @@ public class StartMenu extends JPanel {
private void highscorelist() {
HighscoreManager highscoreManager = new HighscoreManager(); // Manager instanziieren
new HighscoreDialog((JFrame) SwingUtilities.getWindowAncestor(this)).setVisible(true);
}

View File

@ -19,24 +19,9 @@ public class HighscoreManager {
public synchronized void addHighscore(String playerName, int time, String boardName) {
fileLock.lock();
try {
// Prüfe, ob es bereits Highscores für das Spielfeld gibt
boolean isShorterTime = highscoreList.stream()
.filter(highscore -> highscore.getBoardName().equals(boardName))
.allMatch(highscore -> time < highscore.getScore());
if (isShorterTime) {
// Entferne alle Highscores für das Spielfeld, die länger oder gleich sind
highscoreList.removeIf(highscore -> highscore.getBoardName().equals(boardName));
// Füge den neuen Highscore hinzu
highscoreList.add(new Highscore(playerName, time, boardName));
// Sortiere die Liste
highscoreList.sort(Comparator.comparingInt(Highscore::getScore)); // Kürzeste Zeit zuerst
saveHighscores();
} else {
System.out.println("Neuer Highscore ist nicht kürzer als die bestehenden Einträge.");
}
highscoreList.add(new Highscore(playerName, time, boardName));
highscoreList.sort(Comparator.comparingInt(Highscore::getScore)); // Kürzeste Zeit zuerst
saveHighscores();
} finally {
fileLock.unlock();
}
@ -57,6 +42,21 @@ public class HighscoreManager {
return true;
}
// Benutzername abfragen und Highscore hinzufügen
public void addHighscoreAfterGame(int elapsedTime, String boardName) {
Scanner scanner = new Scanner(System.in);
System.out.print("Bitte geben Sie Ihren Namen ein: ");
String playerName = scanner.nextLine().trim();
if (playerName.isEmpty()) {
System.out.println("Ungültiger Name. Highscore wird nicht gespeichert.");
return;
}
addHighscore(playerName, elapsedTime, boardName);
System.out.println("Highscore erfolgreich gespeichert!");
}
// Highscores laden
private void loadHighscores() {
fileLock.lock();
@ -86,7 +86,6 @@ public class HighscoreManager {
}
}
// Highscores speichern
private void saveHighscores() {
fileLock.lock();
@ -173,4 +172,3 @@ public class HighscoreManager {
}
}