Compare commits

..

No commits in common. "3d0f808daf245c3f1edcc251cf9d660cb2ccff6d" and "3742dac0382a7907cded32b82567849624b31786" have entirely different histories.

7 changed files with 139 additions and 252 deletions

View File

@ -9,22 +9,25 @@ import java.util.List;
public class HitoriBoard { public class HitoriBoard {
private final HitoriCell[][] board; private final HitoriCell[][] board;
private final List<String> solutionCoordinates; private final List<String> solutionCoordinates;
private final String boardName; // Name des Spielfelds private int[][] originalBoardData;
public HitoriBoard(int[][] numbers, List<String> solutionCoordinates) {
public HitoriBoard(int[][] numbers, List<String> solutionCoordinates, String boardName) { this.originalBoardData = numbers; // Oginaldaten speichern
this.board = new HitoriCell[numbers.length][numbers[0].length]; this.board = new HitoriCell[numbers.length][numbers[0].length];
this.solutionCoordinates = solutionCoordinates; this.solutionCoordinates = solutionCoordinates;
this.boardName = boardName;
initializeBoard(numbers); initializeBoard(numbers);
} }
/** public List<String> getSolutionCoordinates() {
* Initialisiert das Spielfeld mit den übergebenen Zahlen. return solutionCoordinates;
* }
* @param numbers 2D-Array mit den Zahlen des Spielfelds.
*/ public HitoriCell[][] getBoard() {
private void initializeBoard(int[][] numbers) { return board;
}
private void initializeBoard(int[][] numbers){
for (int i = 0; i < numbers.length; i++) { for (int i = 0; i < numbers.length; i++) {
for (int j = 0; j < numbers[i].length; j++) { for (int j = 0; j < numbers[i].length; j++) {
board[i][j] = new HitoriCell(numbers[i][j]); board[i][j] = new HitoriCell(numbers[i][j]);
@ -32,60 +35,32 @@ 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) { public HitoriCell getCell(int row, int col) {
return board[row][col]; return board[row][col];
} }
/**
* Gibt die Größe des Spielfelds zurück.
*
* @return Anzahl der Zeilen im Spielfeld.
*/
public int getSize() { public int getSize() {
return board.length; 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() { public void resetBoard() {
//initializeBoard(originalBoardData);
for (int i = 0; i < board.length; i++) { for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) { for (int j = 0; j < board[i].length; j++) {
board[i][j].setState(HitoriCell.CellState.GRAY); // Standardzustand 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]);
} }
} }
} }
/**
* Gibt das gesamte Spielfeld als 2D-Array zurück.
*
* @return 2D-Array mit den Zahlen des Spielfelds.
*/
public int[][] getNumbers() { public int[][] getNumbers() {
int[][] numbers = new int[board.length][board[0].length]; int[][] numbers = new int[board.length][board[0].length];
for (int i = 0; i < board.length; i++) { for (int i = 0; i < board.length; i++) {
@ -96,17 +71,4 @@ public class HitoriBoard {
return numbers; 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]);
}
}
}
} }

View File

@ -9,42 +9,30 @@ import java.util.*;
public class HitoriSolutionLoader { public class HitoriSolutionLoader {
public static List<String> loadSolution(String resourcePath) throws IOException { public static List<String> loadSolution(String resourcePath) throws IOException {
InputStream inputStream = HitoriSolutionLoader.class.getResourceAsStream(resourcePath);
if (inputStream == null) {
throw new IOException("Ressourcendatei nicht gefunden: " + resourcePath);
}
List<String> solutionCoordinates = new ArrayList<>(); List<String> solutionCoordinates = new ArrayList<>();
boolean solutionSectionFound = false; try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
try (InputStream inputStream = HitoriSolutionLoader.class.getResourceAsStream(resourcePath);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
if (inputStream == null) {
throw new IOException("Ressourcendatei nicht gefunden: " + resourcePath);
}
String line; String line;
boolean isSolutionSection = false;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
line = line.trim(); // Erkenne den Abschnitt für die Lösung
// Überprüfen, ob wir im Lösungsteil sind
if (line.startsWith("//Lösung")) { if (line.startsWith("//Lösung")) {
solutionSectionFound = true; isSolutionSection = true;
continue; continue; // Überspringe die Kommentarzeile
} }
if (solutionSectionFound) { if (isSolutionSection) {
if (line.isEmpty()) { solutionCoordinates.add(line.trim());
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; return solutionCoordinates;
} }
} }

View File

@ -5,6 +5,7 @@ import java.util.List;
/** /**
* Validates the Hitori board against the solution. * Validates the Hitori board against the solution.
*/ */
public class HitoriValidator { public class HitoriValidator {
private final HitoriBoard board; private final HitoriBoard board;
@ -18,6 +19,7 @@ public class HitoriValidator {
* @param solutionCoordinates The coordinates of the correct black cells in "row,column" format. * @param solutionCoordinates The coordinates of the correct black cells in "row,column" format.
* @return true if the current board matches the solution, false otherwise. * @return true if the current board matches the solution, false otherwise.
*/ */
public boolean validateBoard(List<String> solutionCoordinates) { public boolean validateBoard(List<String> solutionCoordinates) {
for (String coordinate : solutionCoordinates) { for (String coordinate : solutionCoordinates) {
String[] parts = coordinate.split(","); String[] parts = coordinate.split(",");
@ -31,15 +33,9 @@ public class HitoriValidator {
for (int i = 0; i < board.getSize(); i++) { for (int i = 0; i < board.getSize(); i++) {
for (int j = 0; j < board.getSize(); j++) { for (int j = 0; j < board.getSize(); j++) {
HitoriCell cell = board.getCell(i, j); if (board.getCell(i,j).getState() == HitoriCell.CellState.BLACK &&
if (cell.getState() == HitoriCell.CellState.BLACK &&
!solutionCoordinates.contains((i + 1) + "," + (j + 1))) { !solutionCoordinates.contains((i + 1) + "," + (j + 1))) {
return false; // Ein schwarzes Feld gehört nicht zur Lösung return false;
}
// Überprüfe, ob alle nicht schwarzen Felder weiß sind
if (cell.getState() != HitoriCell.CellState.BLACK && cell.getState() != HitoriCell.CellState.WHITE) {
return false; // Es gibt noch graue Felder
} }
} }
} }

View File

@ -27,7 +27,6 @@ public class GameBoard extends JPanel {
// Timer-Label oben hinzufügen // Timer-Label oben hinzufügen
timerLabel = new JLabel("Zeit: 0 Sekunden"); timerLabel = new JLabel("Zeit: 0 Sekunden");
timerLabel.setHorizontalAlignment(SwingConstants.CENTER); timerLabel.setHorizontalAlignment(SwingConstants.CENTER);
Setup.styleLabel(timerLabel);
add(timerLabel, BorderLayout.NORTH); add(timerLabel, BorderLayout.NORTH);
// Timer starten // Timer starten
@ -154,7 +153,13 @@ public class GameBoard extends JPanel {
public boolean validateCurrentBoard() { public boolean validateCurrentBoard() {
HitoriValidator validator = new HitoriValidator(board); HitoriValidator validator = new HitoriValidator(board);
return validator.validateBoard(board.getSolutionCoordinates()); if (validator.validateBoard(board.getSolutionCoordinates())) {
JOptionPane.showMessageDialog(this, "Das Spielfeld ist korrekt gelöst!", "Erfolg", JOptionPane.INFORMATION_MESSAGE);
return true;
} else {
JOptionPane.showMessageDialog(this, "Das Spielfeld enthält Fehler!", "Fehler", JOptionPane.ERROR_MESSAGE);
return false;
}
} }
private void showPauseMenu() { private void showPauseMenu() {
@ -229,12 +234,13 @@ public class GameBoard extends JPanel {
} }
} }
private void addHighscore() { private void saveHighscore() {
String playerName = JOptionPane.showInputDialog(this, String playerName = JOptionPane.showInputDialog(this,
"Bitte geben Sie Ihren Namen ein:", "Bitte geben Sie Ihren Namen ein:",
"Highscore speichern", "Highscore speichern",
JOptionPane.PLAIN_MESSAGE); JOptionPane.PLAIN_MESSAGE);
// Überprüfen, ob ein Name eingegeben wurde
if (playerName == null || playerName.trim().isEmpty()) { if (playerName == null || playerName.trim().isEmpty()) {
JOptionPane.showMessageDialog(this, JOptionPane.showMessageDialog(this,
"Highscore wurde nicht gespeichert. Kein Name eingegeben.", "Highscore wurde nicht gespeichert. Kein Name eingegeben.",
@ -243,27 +249,22 @@ public class GameBoard extends JPanel {
return; return;
} }
int elapsedTime = (int) ((System.currentTimeMillis() - startTime) / 1000); // Berechne die verstrichene Zeit
int elapsedTime = (int) ((System.currentTimeMillis() - startTime) / 1000); // Zeit in Sekunden
String boardName = board.getBoardName(); // Spielfeldname (oder eine Beschreibung)
String boardName = "Aktuelles Spielfeld"; // Falls ein spezifischer Name verfügbar ist, ersetzen
try { // Speichere den Highscore mit dem HighscoreManager
HighscoreManager manager = new HighscoreManager(); HighscoreManager.saveHighscore(playerName, elapsedTime, boardName);
manager.addHighscore(playerName, elapsedTime, boardName);
JOptionPane.showMessageDialog(this, // Zeige eine Bestätigung an
"Highscore erfolgreich gespeichert!", JOptionPane.showMessageDialog(this,
"Erfolg", "Highscore erfolgreich gespeichert!",
JOptionPane.INFORMATION_MESSAGE); "Erfolg",
} catch (Exception e) { JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(this,
"Fehler beim Speichern des Highscores: " + e.getMessage(),
"Fehler",
JOptionPane.ERROR_MESSAGE);
}
} }
private void returnToMainMenu() { private void returnToMainMenu() {
/// Eltern-Frame abrufen /// Eltern-Frame abrufen
JFrame parentFrame = (JFrame) SwingUtilities.getWindowAncestor(this); JFrame parentFrame = (JFrame) SwingUtilities.getWindowAncestor(this);

View File

@ -1,8 +1,6 @@
package PR2.HitoriSpiel.GUI; package PR2.HitoriSpiel.GUI;
import PR2.HitoriSpiel.Utils.HighscoreManager; import PR2.HitoriSpiel.Utils.HighscoreManager;
import PR2.HitoriSpiel.Utils.Setup;
import javax.swing.table.DefaultTableModel; import javax.swing.table.DefaultTableModel;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
@ -11,59 +9,36 @@ import java.util.List;
// aktueller Stand // aktueller Stand
public class HighscoreDialog extends JDialog { public class HighscoreDialog extends JDialog {
private final HighscoreManager highscoreManager; public HighscoreDialog(JFrame parent) {
private final DefaultTableModel tableModel; super(parent, "Highscoreliste", true);
public HighscoreDialog(JFrame parentFrame) {
super(parentFrame, "Highscoreliste", true);
this.highscoreManager = new HighscoreManager();
setLayout(new BorderLayout());
setSize(500, 400); setSize(500, 400);
setLocationRelativeTo(parentFrame); setLocationRelativeTo(parent);
Setup.stylePanel((JPanel) getContentPane()); // Hintergrundfarbe setzen
JLabel titleLabel = new JLabel("Highscores", SwingConstants.CENTER); // Tabelle erstellen
titleLabel.setFont(new Font("Arial", Font.BOLD, 20)); String[] columnNames = {"Name", "Zeit (Sek.)", "Spielfeld"};
Setup.styleLabel(titleLabel); // Schriftstil setzen DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);
add(titleLabel, BorderLayout.NORTH);
String[] columnNames = {"Name", "Punkte", "Spielfeld"}; // Highscores laden
tableModel = new DefaultTableModel(columnNames, 0); List<HighscoreManager.Highscore> highscores = HighscoreManager.loadHighscores();
JTable highscoreTable = new JTable(tableModel); for (HighscoreManager.Highscore highscore : highscores) {
highscoreTable.setFillsViewportHeight(true); tableModel.addRow(new Object[]{highscore.getName(), highscore.getTime(), highscore.getBoard()});
highscoreTable.setEnabled(false); // Tabelle nur zur Anzeige }
JScrollPane scrollPane = new JScrollPane(highscoreTable);
add(scrollPane, BorderLayout.CENTER);
// Schließen-Button JTable table = new JTable(tableModel);
JButton closeButton = Setup.createButton("Schließen", 120, 40); table.setEnabled(false); // Tabelle nur lesbar
closeButton.setFont(new Font(closeButton.getFont().getName(), closeButton.getFont().getStyle(), 14)); // Schriftgröße ändern table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
// Button zum Schließen des Dialogs
JButton closeButton = new JButton("Schließen");
closeButton.addActionListener(e -> dispose()); closeButton.addActionListener(e -> dispose());
JPanel buttonPanel = new JPanel(); JPanel buttonPanel = new JPanel();
Setup.stylePanel(buttonPanel); // Hintergrundstil setzen
buttonPanel.add(closeButton); buttonPanel.add(closeButton);
setLayout(new BorderLayout());
add(scrollPane, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH); 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();
} }
} }

View File

@ -111,9 +111,8 @@ public class StartMenu extends JPanel {
new HighscoreDialog((JFrame) SwingUtilities.getWindowAncestor(this)).setVisible(true); new HighscoreDialog((JFrame) SwingUtilities.getWindowAncestor(this)).setVisible(true);
} }
// Hilfsmethoden // Hilfsmethoden
private void loadGameBoard(GameBoard gameBoard, List<String> solutionCoordinates) { private void loadGameBoard(GameBoard gameBoard, List<String> solutionCoordinates) {
removeAll(); removeAll();
setLayout(new BorderLayout()); setLayout(new BorderLayout());
@ -130,8 +129,9 @@ public class StartMenu extends JPanel {
int[][] boardData = loadBoard(resourcePath); int[][] boardData = loadBoard(resourcePath);
List<String> solutionCoordinates = HitoriSolutionLoader.loadSolution(resourcePath); List<String> solutionCoordinates = HitoriSolutionLoader.loadSolution(resourcePath);
HitoriBoard hitoriBoard = new HitoriBoard(boardData, solutionCoordinates, selectedFile); // Stelle sicher, dass die Lösung korrekt geladen wird HitoriBoard hitoriBoard = new HitoriBoard(boardData, solutionCoordinates); // Stelle sicher, dass die Lösung korrekt geladen wird
GameBoard gameBoard = new GameBoard(hitoriBoard); GameBoard gameBoard = new GameBoard(hitoriBoard);
//GameBoard gameBoard = new GameBoard(new HitoriBoard(boardData, solutionCoordinates));
loadGameBoard(gameBoard, solutionCoordinates); loadGameBoard(gameBoard, solutionCoordinates);
System.out.println("Ausgewählte Datei: " + selectedFile); System.out.println("Ausgewählte Datei: " + selectedFile);
@ -141,4 +141,9 @@ public class StartMenu extends JPanel {
JOptionPane.showMessageDialog(this, "Fehler beim Laden des Spielfelds: " + e.getMessage(), "Fehler", JOptionPane.ERROR_MESSAGE); JOptionPane.showMessageDialog(this, "Fehler beim Laden des Spielfelds: " + e.getMessage(), "Fehler", JOptionPane.ERROR_MESSAGE);
} }
} }
} }

View File

@ -2,112 +2,72 @@ package PR2.HitoriSpiel.Utils;
import java.io.*; import java.io.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
// aktueller Stand
public class HighscoreManager { public class HighscoreManager {
private static final String HIGHSCORE_FILE = "highscores.txt"; 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 HighscoreManager() { public static class Highscore {
loadHighscores(); private final String name;
} private final int time;
private final String board;
// Highscore hinzufügen public Highscore(String name, int time, String board) {
public synchronized void addHighscore(String playerName, int score, String boardName) { this.name = name;
fileLock.lock(); this.time = time;
try { this.board = board;
highscoreList.add(new Highscore(playerName, score, boardName)); }
highscoreList.sort(Comparator.comparingInt(Highscore::getScore).reversed()); // Sortierung absteigend
saveHighscores(); public String getName() {
} finally { return name;
fileLock.unlock(); }
public int getTime() {
return time;
}
public String getBoard() {
return board;
} }
} }
// Highscores laden public static List<Highscore> loadHighscores() {
private void loadHighscores() { List<Highscore> highscores = new ArrayList<>();
fileLock.lock(); File file = new File(HIGHSCORE_FILE);
try (BufferedReader reader = new BufferedReader(new FileReader(HIGHSCORE_FILE))) {
if (!file.exists()) {
return highscores;
}
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line; String line;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
String[] parts = line.split(","); String[] parts = line.split(", ");
if (parts.length == 3) { // Name, Punkte, Spielfeld if (parts.length == 3) {
highscoreList.add(new Highscore(parts[0], Integer.parseInt(parts[1]), parts[2])); String name = parts[0];
int time = Integer.parseInt(parts[1]);
String board = parts[2];
highscores.add(new Highscore(name, time, board));
} }
} }
} 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 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) { } catch (IOException e) {
System.err.println("Fehler beim Speichern der Highscores: " + e.getMessage()); e.printStackTrace();
} finally {
fileLock.unlock();
} }
// Highscores nach Zeit sortieren (kürzeste Zeit zuerst)
highscores.sort(Comparator.comparingInt(Highscore::getTime));
return highscores;
} }
// Highscores abrufen public static void saveHighscore(String name, int time, String board) {
public List<Highscore> getHighscores() { try (BufferedWriter writer = new BufferedWriter(new FileWriter(HIGHSCORE_FILE, true))) {
return Collections.unmodifiableList(highscoreList); writer.write(name + ", " + time + ", " + board);
} writer.newLine();
} catch (IOException e) {
// Alte Highscores bereinigen e.printStackTrace();
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;
}
}
} }