Code mit Highscoreliste erweitert
parent
3742dac038
commit
4cb6b12814
|
@ -9,24 +9,21 @@ import java.util.List;
|
|||
public class HitoriBoard {
|
||||
private final HitoriCell[][] board;
|
||||
private final List<String> solutionCoordinates;
|
||||
private int[][] originalBoardData;
|
||||
private final String boardName; // Name des Spielfelds
|
||||
|
||||
public HitoriBoard(int[][] numbers, List<String> solutionCoordinates) {
|
||||
this.originalBoardData = numbers; // Oginaldaten speichern
|
||||
|
||||
public HitoriBoard(int[][] numbers, List<String> solutionCoordinates, String boardName) {
|
||||
this.board = new HitoriCell[numbers.length][numbers[0].length];
|
||||
this.solutionCoordinates = solutionCoordinates;
|
||||
this.boardName = boardName;
|
||||
initializeBoard(numbers);
|
||||
}
|
||||
|
||||
public List<String> getSolutionCoordinates() {
|
||||
return solutionCoordinates;
|
||||
}
|
||||
|
||||
public HitoriCell[][] getBoard() {
|
||||
return board;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Initialisiert das Spielfeld mit den übergebenen Zahlen.
|
||||
*
|
||||
* @param numbers 2D-Array mit den Zahlen des Spielfelds.
|
||||
*/
|
||||
private void initializeBoard(int[][] numbers) {
|
||||
for (int i = 0; i < numbers.length; i++) {
|
||||
for (int j = 0; j < numbers[i].length; j++) {
|
||||
|
@ -35,32 +32,60 @@ public class HitoriBoard {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt den Namen des Spielfelds zurück.
|
||||
*
|
||||
* @return Name des Spielfelds.
|
||||
*/
|
||||
public String getBoardName() {
|
||||
return boardName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt die Lösungskonfiguration des Spielfelds zurück.
|
||||
*
|
||||
* @return Liste der Koordinaten, die zur Lösung gehören.
|
||||
*/
|
||||
public List<String> getSolutionCoordinates() {
|
||||
return solutionCoordinates;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt die HitoriCell an einer bestimmten Position zurück.
|
||||
*
|
||||
* @param row Zeile der Zelle.
|
||||
* @param col Spalte der Zelle.
|
||||
* @return Die HitoriCell an der angegebenen Position.
|
||||
*/
|
||||
public HitoriCell getCell(int row, int col) {
|
||||
return board[row][col];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt die Größe des Spielfelds zurück.
|
||||
*
|
||||
* @return Anzahl der Zeilen im Spielfeld.
|
||||
*/
|
||||
public int getSize() {
|
||||
return board.length;
|
||||
}
|
||||
|
||||
//Board zurücksetzen zu dem Anfangszustand
|
||||
/**
|
||||
* Setzt das Spielfeld zurück, indem alle Zellen in den Standardzustand gesetzt werden.
|
||||
*/
|
||||
public void resetBoard() {
|
||||
//initializeBoard(originalBoardData);
|
||||
for (int i = 0; i < board.length; i++) {
|
||||
for (int j = 0; j < board[i].length; j++) {
|
||||
board[i][j].setState(HitoriCell.CellState.GRAY); // Zurücksetzen
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setNumbers(int[][] numbers) {
|
||||
for (int i = 0; i < board.length; i++) {
|
||||
for (int j = 0; j < board[i].length; j++) {
|
||||
board[i][j].setNumber(numbers[i][j]);
|
||||
board[i][j].setState(HitoriCell.CellState.GRAY); // Standardzustand
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt das gesamte Spielfeld als 2D-Array zurück.
|
||||
*
|
||||
* @return 2D-Array mit den Zahlen des Spielfelds.
|
||||
*/
|
||||
public int[][] getNumbers() {
|
||||
int[][] numbers = new int[board.length][board[0].length];
|
||||
for (int i = 0; i < board.length; i++) {
|
||||
|
@ -71,4 +96,17 @@ public class HitoriBoard {
|
|||
return numbers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setzt die Zahlen des Spielfelds basierend auf einem 2D-Array.
|
||||
*
|
||||
* @param numbers 2D-Array mit den neuen Zahlen.
|
||||
*/
|
||||
public void setNumbers(int[][] numbers) {
|
||||
for (int i = 0; i < board.length; i++) {
|
||||
for (int j = 0; j < board[i].length; j++) {
|
||||
board[i][j].setNumber(numbers[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -9,30 +9,42 @@ import java.util.*;
|
|||
public class HitoriSolutionLoader {
|
||||
|
||||
public static List<String> loadSolution(String resourcePath) throws IOException {
|
||||
InputStream inputStream = HitoriSolutionLoader.class.getResourceAsStream(resourcePath);
|
||||
List<String> solutionCoordinates = new ArrayList<>();
|
||||
boolean solutionSectionFound = false;
|
||||
|
||||
try (InputStream inputStream = HitoriSolutionLoader.class.getResourceAsStream(resourcePath);
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
|
||||
|
||||
if (inputStream == null) {
|
||||
throw new IOException("Ressourcendatei nicht gefunden: " + resourcePath);
|
||||
}
|
||||
|
||||
List<String> solutionCoordinates = new ArrayList<>();
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
|
||||
String line;
|
||||
boolean isSolutionSection = false;
|
||||
|
||||
while ((line = reader.readLine()) != null) {
|
||||
// Erkenne den Abschnitt für die Lösung
|
||||
line = line.trim();
|
||||
|
||||
// Überprüfen, ob wir im Lösungsteil sind
|
||||
if (line.startsWith("//Lösung")) {
|
||||
isSolutionSection = true;
|
||||
continue; // Überspringe die Kommentarzeile
|
||||
solutionSectionFound = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isSolutionSection) {
|
||||
solutionCoordinates.add(line.trim());
|
||||
if (solutionSectionFound) {
|
||||
if (line.isEmpty()) {
|
||||
continue; // Ignoriere leere Zeilen
|
||||
}
|
||||
solutionCoordinates.add(line);
|
||||
}
|
||||
}
|
||||
|
||||
if (!solutionSectionFound) {
|
||||
System.err.println("Warnung: Lösungsteil wurde in der Datei " + resourcePath + " nicht gefunden.");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.err.println("Fehler beim Laden der Lösung: " + e.getMessage());
|
||||
throw e;
|
||||
}
|
||||
|
||||
return solutionCoordinates;
|
||||
|
||||
}
|
||||
}
|
|
@ -27,6 +27,7 @@ public class GameBoard extends JPanel {
|
|||
// Timer-Label oben hinzufügen
|
||||
timerLabel = new JLabel("Zeit: 0 Sekunden");
|
||||
timerLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
Setup.styleLabel(timerLabel);
|
||||
add(timerLabel, BorderLayout.NORTH);
|
||||
|
||||
// Timer starten
|
||||
|
@ -234,13 +235,12 @@ public class GameBoard extends JPanel {
|
|||
}
|
||||
}
|
||||
|
||||
private void saveHighscore() {
|
||||
private void addHighscore() {
|
||||
String playerName = JOptionPane.showInputDialog(this,
|
||||
"Bitte geben Sie Ihren Namen ein:",
|
||||
"Highscore speichern",
|
||||
JOptionPane.PLAIN_MESSAGE);
|
||||
|
||||
// Überprüfen, ob ein Name eingegeben wurde
|
||||
if (playerName == null || playerName.trim().isEmpty()) {
|
||||
JOptionPane.showMessageDialog(this,
|
||||
"Highscore wurde nicht gespeichert. Kein Name eingegeben.",
|
||||
|
@ -249,21 +249,26 @@ public class GameBoard extends JPanel {
|
|||
return;
|
||||
}
|
||||
|
||||
// Berechne die verstrichene Zeit
|
||||
int elapsedTime = (int) ((System.currentTimeMillis() - startTime) / 1000); // Zeit in Sekunden
|
||||
int elapsedTime = (int) ((System.currentTimeMillis() - startTime) / 1000);
|
||||
|
||||
// Spielfeldname (oder eine Beschreibung)
|
||||
String boardName = "Aktuelles Spielfeld"; // Falls ein spezifischer Name verfügbar ist, ersetzen
|
||||
String boardName = board.getBoardName();
|
||||
|
||||
// Speichere den Highscore mit dem HighscoreManager
|
||||
HighscoreManager.saveHighscore(playerName, elapsedTime, boardName);
|
||||
try {
|
||||
HighscoreManager manager = new HighscoreManager();
|
||||
manager.addHighscore(playerName, elapsedTime, boardName);
|
||||
|
||||
// Zeige eine Bestätigung an
|
||||
JOptionPane.showMessageDialog(this,
|
||||
"Highscore erfolgreich gespeichert!",
|
||||
"Erfolg",
|
||||
JOptionPane.INFORMATION_MESSAGE);
|
||||
} catch (Exception e) {
|
||||
JOptionPane.showMessageDialog(this,
|
||||
"Fehler beim Speichern des Highscores: " + e.getMessage(),
|
||||
"Fehler",
|
||||
JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void returnToMainMenu() {
|
||||
/// Eltern-Frame abrufen
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package PR2.HitoriSpiel.GUI;
|
||||
|
||||
import PR2.HitoriSpiel.Utils.HighscoreManager;
|
||||
import PR2.HitoriSpiel.Utils.Setup;
|
||||
|
||||
import javax.swing.table.DefaultTableModel;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
@ -9,36 +11,59 @@ import java.util.List;
|
|||
// aktueller Stand
|
||||
public class HighscoreDialog extends JDialog {
|
||||
|
||||
public HighscoreDialog(JFrame parent) {
|
||||
super(parent, "Highscoreliste", true);
|
||||
private final HighscoreManager highscoreManager;
|
||||
private final DefaultTableModel tableModel;
|
||||
|
||||
public HighscoreDialog(JFrame parentFrame) {
|
||||
super(parentFrame, "Highscoreliste", true);
|
||||
this.highscoreManager = new HighscoreManager();
|
||||
|
||||
setLayout(new BorderLayout());
|
||||
setSize(500, 400);
|
||||
setLocationRelativeTo(parent);
|
||||
setLocationRelativeTo(parentFrame);
|
||||
Setup.stylePanel((JPanel) getContentPane()); // Hintergrundfarbe setzen
|
||||
|
||||
// Tabelle erstellen
|
||||
String[] columnNames = {"Name", "Zeit (Sek.)", "Spielfeld"};
|
||||
DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);
|
||||
JLabel titleLabel = new JLabel("Highscores", SwingConstants.CENTER);
|
||||
titleLabel.setFont(new Font("Arial", Font.BOLD, 20));
|
||||
Setup.styleLabel(titleLabel); // Schriftstil setzen
|
||||
add(titleLabel, BorderLayout.NORTH);
|
||||
|
||||
// Highscores laden
|
||||
List<HighscoreManager.Highscore> highscores = HighscoreManager.loadHighscores();
|
||||
for (HighscoreManager.Highscore highscore : highscores) {
|
||||
tableModel.addRow(new Object[]{highscore.getName(), highscore.getTime(), highscore.getBoard()});
|
||||
}
|
||||
String[] columnNames = {"Name", "Punkte", "Spielfeld"};
|
||||
tableModel = new DefaultTableModel(columnNames, 0);
|
||||
JTable highscoreTable = new JTable(tableModel);
|
||||
highscoreTable.setFillsViewportHeight(true);
|
||||
highscoreTable.setEnabled(false); // Tabelle nur zur Anzeige
|
||||
JScrollPane scrollPane = new JScrollPane(highscoreTable);
|
||||
add(scrollPane, BorderLayout.CENTER);
|
||||
|
||||
JTable table = new JTable(tableModel);
|
||||
table.setEnabled(false); // Tabelle nur lesbar
|
||||
table.setFillsViewportHeight(true);
|
||||
|
||||
JScrollPane scrollPane = new JScrollPane(table);
|
||||
|
||||
// Button zum Schließen des Dialogs
|
||||
JButton closeButton = new JButton("Schließen");
|
||||
// Schließen-Button
|
||||
JButton closeButton = Setup.createButton("Schließen", 120, 40);
|
||||
closeButton.setFont(new Font(closeButton.getFont().getName(), closeButton.getFont().getStyle(), 14)); // Schriftgröße ändern
|
||||
closeButton.addActionListener(e -> dispose());
|
||||
|
||||
JPanel buttonPanel = new JPanel();
|
||||
Setup.stylePanel(buttonPanel); // Hintergrundstil setzen
|
||||
buttonPanel.add(closeButton);
|
||||
|
||||
setLayout(new BorderLayout());
|
||||
add(scrollPane, BorderLayout.CENTER);
|
||||
add(buttonPanel, BorderLayout.SOUTH);
|
||||
|
||||
// Highscores laden
|
||||
loadHighscores();
|
||||
}
|
||||
|
||||
private void loadHighscores() {
|
||||
tableModel.setRowCount(0); // Tabelle zurücksetzen
|
||||
List<HighscoreManager.Highscore> highscores = highscoreManager.getHighscores();
|
||||
|
||||
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()
|
||||
}));
|
||||
}
|
||||
|
||||
public void refreshHighscores() {
|
||||
loadHighscores();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -129,9 +129,8 @@ public class StartMenu extends JPanel {
|
|||
int[][] boardData = loadBoard(resourcePath);
|
||||
List<String> solutionCoordinates = HitoriSolutionLoader.loadSolution(resourcePath);
|
||||
|
||||
HitoriBoard hitoriBoard = new HitoriBoard(boardData, solutionCoordinates); // Stelle sicher, dass die Lösung korrekt geladen wird
|
||||
HitoriBoard hitoriBoard = new HitoriBoard(boardData, solutionCoordinates, selectedFile); // Stelle sicher, dass die Lösung korrekt geladen wird
|
||||
GameBoard gameBoard = new GameBoard(hitoriBoard);
|
||||
//GameBoard gameBoard = new GameBoard(new HitoriBoard(boardData, solutionCoordinates));
|
||||
loadGameBoard(gameBoard, solutionCoordinates);
|
||||
|
||||
System.out.println("Ausgewählte Datei: " + selectedFile);
|
||||
|
|
|
@ -2,72 +2,112 @@ 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.concurrent.locks.ReentrantLock;
|
||||
|
||||
// aktueller Stand
|
||||
public class HighscoreManager {
|
||||
|
||||
private static final String HIGHSCORE_FILE = "highscores.txt";
|
||||
private static final ReentrantLock fileLock = new ReentrantLock();
|
||||
// Highscore-Datenstruktur
|
||||
private final List<Highscore> highscoreList = new ArrayList<>();
|
||||
|
||||
public static class Highscore {
|
||||
private final String name;
|
||||
private final int time;
|
||||
private final String board;
|
||||
|
||||
public Highscore(String name, int time, String board) {
|
||||
this.name = name;
|
||||
this.time = time;
|
||||
this.board = board;
|
||||
public HighscoreManager() {
|
||||
loadHighscores();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public String getBoard() {
|
||||
return board;
|
||||
// Highscore hinzufügen
|
||||
public synchronized void addHighscore(String playerName, int score, String boardName) {
|
||||
fileLock.lock();
|
||||
try {
|
||||
highscoreList.add(new Highscore(playerName, score, boardName));
|
||||
highscoreList.sort(Comparator.comparingInt(Highscore::getScore).reversed()); // Sortierung absteigend
|
||||
saveHighscores();
|
||||
} finally {
|
||||
fileLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public static List<Highscore> loadHighscores() {
|
||||
List<Highscore> highscores = new ArrayList<>();
|
||||
File file = new File(HIGHSCORE_FILE);
|
||||
|
||||
if (!file.exists()) {
|
||||
return highscores;
|
||||
}
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
||||
// 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) {
|
||||
String name = parts[0];
|
||||
int time = Integer.parseInt(parts[1]);
|
||||
String board = parts[2];
|
||||
highscores.add(new Highscore(name, time, board));
|
||||
if (parts.length == 3) { // Name, Punkte, Spielfeld
|
||||
highscoreList.add(new Highscore(parts[0], Integer.parseInt(parts[1]), parts[2]));
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} 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 {
|
||||
fileLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
// Highscores nach Zeit sortieren (kürzeste Zeit zuerst)
|
||||
highscores.sort(Comparator.comparingInt(Highscore::getTime));
|
||||
return highscores;
|
||||
}
|
||||
|
||||
public static void saveHighscore(String name, int time, String board) {
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(HIGHSCORE_FILE, true))) {
|
||||
writer.write(name + ", " + time + ", " + board);
|
||||
// Highscores speichern
|
||||
private void saveHighscores() {
|
||||
fileLock.lock();
|
||||
try (BufferedWriter writer = new BufferedWriter(new FileWriter(HIGHSCORE_FILE))) {
|
||||
for (Highscore highscore : highscoreList) {
|
||||
writer.write(highscore.getPlayerName() + "," + highscore.getScore() + "," + highscore.getBoardName());
|
||||
writer.newLine();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
System.err.println("Fehler beim Speichern der Highscores: " + e.getMessage());
|
||||
} finally {
|
||||
fileLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
// Highscores abrufen
|
||||
public List<Highscore> getHighscores() {
|
||||
return Collections.unmodifiableList(highscoreList);
|
||||
}
|
||||
|
||||
// Alte Highscores bereinigen
|
||||
public void cleanOldHighscores(int maxEntries) {
|
||||
fileLock.lock();
|
||||
try {
|
||||
if (highscoreList.size() > maxEntries) {
|
||||
highscoreList.subList(maxEntries, highscoreList.size()).clear();
|
||||
saveHighscores();
|
||||
}
|
||||
} finally {
|
||||
fileLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
// Innere Highscore-Klasse
|
||||
public static class Highscore {
|
||||
private final String playerName;
|
||||
private final int score;
|
||||
private final String boardName;
|
||||
|
||||
public Highscore(String playerName, int score, String boardName) {
|
||||
this.playerName = playerName;
|
||||
this.score = score;
|
||||
this.boardName = boardName;
|
||||
}
|
||||
|
||||
public String getPlayerName() {
|
||||
return playerName;
|
||||
}
|
||||
|
||||
public int getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
public String getBoardName() {
|
||||
return boardName;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue