Durchschnitszeit-Funktion verbessert
parent
a4ce98a837
commit
d88e35b4e2
|
@ -1,9 +1,7 @@
|
|||
package PR2.HitoriSpiel.Fassade;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
public class HighscoreManager {
|
||||
|
@ -96,6 +94,29 @@ public class HighscoreManager {
|
|||
}
|
||||
}
|
||||
|
||||
// Durchschnittszeit für jedes Spielfeld berechnen
|
||||
public Map<String, Double> getAverageTimesByBoard() {
|
||||
fileLock.lock();
|
||||
try {
|
||||
Map<String, List<Integer>> boardScores = new HashMap<>();
|
||||
|
||||
for (Highscore highscore : highscoreList) {
|
||||
boardScores.computeIfAbsent(highscore.getBoardName(), k -> new ArrayList<>()).add(highscore.getScore());
|
||||
}
|
||||
|
||||
Map<String, Double> averageTimes = new HashMap<>();
|
||||
for (Map.Entry<String, List<Integer>> entry : boardScores.entrySet()) {
|
||||
List<Integer> scores = entry.getValue();
|
||||
double average = scores.stream().mapToInt(Integer::intValue).average().orElse(0.0);
|
||||
averageTimes.put(entry.getKey(), average);
|
||||
}
|
||||
|
||||
return averageTimes;
|
||||
} finally {
|
||||
fileLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
// Highscores abrufen
|
||||
public List<Highscore> getHighscores() {
|
||||
return new ArrayList<>(highscoreList); // Modifizierbare Kopie zurückgeben
|
||||
|
@ -142,6 +163,8 @@ public class HighscoreManager {
|
|||
return errors;
|
||||
}
|
||||
|
||||
public int getScore() {return time;}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return playerName + "," + time + "," + boardName + "," + errors;
|
||||
|
|
|
@ -7,6 +7,7 @@ import javax.swing.table.DefaultTableModel;
|
|||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
// aktueller Stand
|
||||
public class HighscoreDialog extends JDialog {
|
||||
|
@ -19,7 +20,7 @@ public class HighscoreDialog extends JDialog {
|
|||
super(parentFrame, "Highscoreliste", true);
|
||||
|
||||
this.highscoreManager = new HighscoreManager();
|
||||
this.tableModel = new DefaultTableModel(new String[]{"Platz", "Name", "Zeit (Sek.)", "Fehler", "Spielfeld"}, 0);
|
||||
this.tableModel = new DefaultTableModel(new String[]{"Platz", "Name", "Zeit (Sek.)", "Fehler", "Spielfeld", "Durchschnittszeit"}, 0);
|
||||
|
||||
setLayout(new BorderLayout());
|
||||
setSize(600, 400);
|
||||
|
@ -58,6 +59,7 @@ public class HighscoreDialog extends JDialog {
|
|||
private void loadHighscoresForBoard(String boardName) {
|
||||
tableModel.setRowCount(0); // Tabelle zurücksetzen
|
||||
List<HighscoreManager.Highscore> highscores = highscoreManager.getHighscoresForBoard(boardName);
|
||||
Map<String, Double> averageTimes = highscoreManager.getAverageTimesByBoard();
|
||||
|
||||
if (highscores.isEmpty()) {
|
||||
JOptionPane.showMessageDialog(this,
|
||||
|
@ -69,12 +71,14 @@ public class HighscoreDialog extends JDialog {
|
|||
|
||||
int rank = 1;
|
||||
for (HighscoreManager.Highscore highscore : highscores) {
|
||||
double averageTime = averageTimes.getOrDefault(highscore.getBoardName(), 0.0);
|
||||
tableModel.addRow(new Object[]{
|
||||
rank++, // Platznummer
|
||||
highscore.getPlayerName(), // Spielername
|
||||
highscore.getTime(), // Zeit in Sekunden
|
||||
highscore.getErrors(), // Anzahl der Fehler
|
||||
highscore.getBoardName() // Spielfeldname
|
||||
rank++, // Platzierung
|
||||
highscore.getPlayerName(), // Name
|
||||
highscore.getScore(), // Punkte
|
||||
highscore.getErrors(), //Errors
|
||||
highscore.getBoardName(), // Spielfeld
|
||||
String.format("%.2f", averageTime) // Durchschnittszeit
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue