Compare commits
2 Commits
master
...
currentSta
Author | SHA1 | Date |
---|---|---|
|
0b9afd426f | |
|
f62d6965e4 |
|
@ -3,23 +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.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
|
||||
|
||||
|
@ -28,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);
|
||||
|
||||
|
@ -47,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.getHighscores();
|
||||
List<HighscoreManager.Highscore> highscores = highscoreManager.getSortedHighscores();
|
||||
Map<String, Double> averageTimes = highscoreManager.getAverageTimesByBoard();
|
||||
|
||||
highscores.stream()
|
||||
.sorted((a, b) -> Integer.compare(b.getScore(), a.getScore())) // Sortierung: Höchste Punkte 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,19 +1,18 @@
|
|||
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 {
|
||||
|
||||
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 +32,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 {
|
||||
|
@ -54,7 +62,7 @@ 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 +74,38 @@ 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();
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
|
@ -84,6 +121,9 @@ public class HighscoreManager {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Innere Highscore-Klasse
|
||||
public static class Highscore {
|
||||
private final String playerName;
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
Name, Zeit (Sekunden), Spielfeld
|
||||
|
||||
NameTest, 123 Sekunden, Hitori4x4_leicht.csv
|
||||
NameTest,123,Hitori4x4
|
||||
Test,456,Hitori4x4
|
||||
Ioana,234,Hitori8x8
|
Loading…
Reference in New Issue