Highscore Liste verbessert, Durchschnittszeit des Spielfelds in die Highscore Liste hinzugefügt
parent
f62d6965e4
commit
0b9afd426f
|
@ -3,24 +3,24 @@ package PR2.HitoriSpiel.GUI;
|
|||
import PR2.HitoriSpiel.Utils.HighscoreManager;
|
||||
import PR2.HitoriSpiel.Utils.Setup;
|
||||
|
||||
import javax.swing.table.DefaultTableCellRenderer;
|
||||
import javax.swing.table.DefaultTableModel;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
// aktueller Stand
|
||||
public class HighscoreDialog extends JDialog {
|
||||
|
||||
private final HighscoreManager highscoreManager;
|
||||
private final DefaultTableModel tableModel;
|
||||
|
||||
public HighscoreDialog(JFrame parentFrame) {
|
||||
public HighscoreDialog(JFrame parentFrame, HighscoreManager highscoreManager) {
|
||||
super(parentFrame, "Highscoreliste", true);
|
||||
this.highscoreManager = new HighscoreManager();
|
||||
this.highscoreManager = highscoreManager;
|
||||
|
||||
setLayout(new BorderLayout());
|
||||
setSize(500, 400);
|
||||
setSize(700, 500); // Breite erhöht für mehr Platz
|
||||
setLocationRelativeTo(parentFrame);
|
||||
Setup.stylePanel((JPanel) getContentPane()); // Hintergrundfarbe setzen
|
||||
|
||||
|
@ -29,11 +29,28 @@ public class HighscoreDialog extends JDialog {
|
|||
Setup.styleLabel(titleLabel); // Schriftstil setzen
|
||||
add(titleLabel, BorderLayout.NORTH);
|
||||
|
||||
String[] columnNames = {"Name", "Punkte", "Spielfeld"};
|
||||
String[] columnNames = {"Platz", "Name", "Punkte", "Spielfeld", "Durchschnittszeit des Spielfelds"};
|
||||
tableModel = new DefaultTableModel(columnNames, 0);
|
||||
JTable highscoreTable = new JTable(tableModel);
|
||||
highscoreTable.setFillsViewportHeight(true);
|
||||
highscoreTable.setEnabled(false); // Tabelle nur zur Anzeige
|
||||
|
||||
// Renderer, um Text in der Mitte der Zellen zu platzieren
|
||||
DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
|
||||
centerRenderer.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
|
||||
for (int i = 0; i < highscoreTable.getColumnCount(); i++) {
|
||||
highscoreTable.getColumnModel().getColumn(i).setCellRenderer(centerRenderer);
|
||||
}
|
||||
|
||||
// Automatische Spaltenbreitenanpassung und individuelle Breiten setzen
|
||||
highscoreTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
|
||||
highscoreTable.getColumnModel().getColumn(0).setPreferredWidth(50); // Platz
|
||||
highscoreTable.getColumnModel().getColumn(1).setPreferredWidth(150); // Name
|
||||
highscoreTable.getColumnModel().getColumn(2).setPreferredWidth(100); // Punkte
|
||||
highscoreTable.getColumnModel().getColumn(3).setPreferredWidth(150); // Spielfeld
|
||||
highscoreTable.getColumnModel().getColumn(4).setPreferredWidth(230); // Durchschnittszeit
|
||||
|
||||
JScrollPane scrollPane = new JScrollPane(highscoreTable);
|
||||
add(scrollPane, BorderLayout.CENTER);
|
||||
|
||||
|
@ -48,23 +65,28 @@ public class HighscoreDialog extends JDialog {
|
|||
add(buttonPanel, BorderLayout.SOUTH);
|
||||
|
||||
// Highscores laden
|
||||
loadHighscores();
|
||||
loadHighscoresWithAverages();
|
||||
}
|
||||
|
||||
private void loadHighscores() {
|
||||
private void loadHighscoresWithAverages() {
|
||||
tableModel.setRowCount(0); // Tabelle zurücksetzen
|
||||
List<HighscoreManager.Highscore> highscores = highscoreManager.getSortedHighscores();
|
||||
Map<String, Double> averageTimes = highscoreManager.getAverageTimesByBoard();
|
||||
|
||||
highscores.stream()
|
||||
.sorted(Comparator.comparingInt(HighscoreManager.Highscore::getScore)) // Kürzeste Zeit zuerst
|
||||
.forEach(highscore -> tableModel.addRow(new Object[]{
|
||||
highscore.getPlayerName(),
|
||||
highscore.getScore(),
|
||||
highscore.getBoardName()
|
||||
}));
|
||||
int rank = 1;
|
||||
for (HighscoreManager.Highscore highscore : highscores) {
|
||||
double averageTime = averageTimes.getOrDefault(highscore.getBoardName(), 0.0);
|
||||
tableModel.addRow(new Object[]{
|
||||
rank++, // Platzierung
|
||||
highscore.getPlayerName(), // Name
|
||||
highscore.getScore(), // Punkte
|
||||
highscore.getBoardName(), // Spielfeld
|
||||
String.format("%.2f", averageTime) // Durchschnittszeit
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public void refreshHighscores() {
|
||||
loadHighscores();
|
||||
loadHighscoresWithAverages();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,7 +109,8 @@ public class StartMenu extends JPanel {
|
|||
|
||||
|
||||
private void highscorelist() {
|
||||
new HighscoreDialog((JFrame) SwingUtilities.getWindowAncestor(this)).setVisible(true);
|
||||
HighscoreManager highscoreManager = new HighscoreManager(); // Manager instanziieren
|
||||
new HighscoreDialog((JFrame) SwingUtilities.getWindowAncestor(this), highscoreManager).setVisible(true);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,10 +1,7 @@
|
|||
package PR2.HitoriSpiel.Utils;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
public class HighscoreManager {
|
||||
|
@ -62,7 +59,6 @@ public class HighscoreManager {
|
|||
}
|
||||
|
||||
|
||||
|
||||
// Highscores speichern
|
||||
private void saveHighscores() {
|
||||
fileLock.lock();
|
||||
|
@ -89,6 +85,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();
|
||||
}
|
||||
}
|
||||
|
||||
// Alte Highscores bereinigen
|
||||
public void cleanOldHighscores(int maxEntries) {
|
||||
fileLock.lock();
|
||||
|
@ -102,27 +121,7 @@ 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
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
NameTest,123,Hitori4x4
|
||||
Test,456,Hitori4x4
|
||||
Ioana,234,Hitori8x8
|
Loading…
Reference in New Issue