Compare commits

...

6 Commits

Author SHA1 Message Date
Vickvick2002 b07fcb0ff5 Fehlerabfang hinzugefügt 2025-01-03 18:19:30 +01:00
Vickvick2002 ca2b8a8921 Code erweitert 2025-01-03 18:19:09 +01:00
Vickvick2002 44c80e9779 Code erweitert 2025-01-03 18:19:06 +01:00
Vickvick2002 0b615ba4cc Getter-Methode hinzugefügt 2025-01-03 18:18:59 +01:00
Vickvick2002 99f0b2627b Methode verbessert wegen Fehler 2025-01-03 18:18:41 +01:00
Vickvick2002 cb97bd32eb Klasse erweitert 2025-01-03 18:18:25 +01:00
6 changed files with 140 additions and 12 deletions

View File

@ -16,6 +16,15 @@ public class HitoriBoard {
initializeBoard(numbers); initializeBoard(numbers);
} }
public List<String> getSolutionCoordinates() {
return solutionCoordinates;
}
public HitoriCell[][] getBoard() {
return board;
}
private void initializeBoard(int[][] numbers){ 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++) {
@ -39,5 +48,10 @@ public class HitoriBoard {
board[i][j].setState(HitoriCell.CellState.GRAY); board[i][j].setState(HitoriCell.CellState.GRAY);
} }
} }
} }
} }

View File

@ -8,20 +8,31 @@ import java.util.*;
*/ */
public class HitoriSolutionLoader { public class HitoriSolutionLoader {
/** public static List<String> loadSolution(String resourcePath) throws IOException {
* Loads solution coordinates from a CSV file. InputStream inputStream = HitoriSolutionLoader.class.getResourceAsStream(resourcePath);
* @param filePath The path to the CSV file containing solution coordinates. if (inputStream == null) {
* @return A list of "row,column" formatted solution coordinates. throw new IOException("Ressourcendatei nicht gefunden: " + resourcePath);
* @throws IOException If the file cannot be read. }
*/
public static List<String> loadSolution(String filePath) throws IOException {
List<String> solutionCoordinates = new ArrayList<>(); List<String> solutionCoordinates = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line; String line;
boolean isSolutionSection = false;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
// Erkenne den Abschnitt für die Lösung
if (line.startsWith("//Lösung")) {
isSolutionSection = true;
continue; // Überspringe die Kommentarzeile
}
if (isSolutionSection) {
solutionCoordinates.add(line.trim()); solutionCoordinates.add(line.trim());
} }
} }
}
return solutionCoordinates; return solutionCoordinates;
} }
} }

View File

@ -97,4 +97,5 @@ public class BoardLoader {
// Konvertiere die Liste von Zeilen in ein 2D-Array // Konvertiere die Liste von Zeilen in ein 2D-Array
return rows.toArray(new int[0][0]); return rows.toArray(new int[0][0]);
} }
} }

View File

@ -2,11 +2,13 @@ package PR2.HitoriSpiel.GUI;
import PR2.HitoriSpiel.Domain.HitoriBoard; import PR2.HitoriSpiel.Domain.HitoriBoard;
import PR2.HitoriSpiel.Domain.HitoriCell; import PR2.HitoriSpiel.Domain.HitoriCell;
import PR2.HitoriSpiel.Domain.HitoriValidator;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
public class GameBoard extends JPanel { public class GameBoard extends JPanel {
private final HitoriBoard board; // Verbindung zur Logik private final HitoriBoard board; // Verbindung zur Logik
@ -38,6 +40,12 @@ public class GameBoard extends JPanel {
} }
private void toggleCellState(HitoriCell cell) { private void toggleCellState(HitoriCell cell) {
if (cell == null) {
System.err.println("Ungültige Zelle! Der Zustand kann nicht geändert werden.");
return;
}
if (cell.getState() == HitoriCell.CellState.GRAY) { if (cell.getState() == HitoriCell.CellState.GRAY) {
cell.setState(HitoriCell.CellState.BLACK); cell.setState(HitoriCell.CellState.BLACK);
} else if (cell.getState() == HitoriCell.CellState.BLACK) { } else if (cell.getState() == HitoriCell.CellState.BLACK) {
@ -62,4 +70,16 @@ public class GameBoard extends JPanel {
break; break;
} }
} }
public void resetBoard() {
board.resetBoard(); // Aufruf der Methode aus HitoriBoard
revalidate();
repaint();
}
public boolean validateCurrentBoard() {
HitoriValidator validator = new HitoriValidator(board);
return validator.validateBoard(board.getSolutionCoordinates());
}
} }

View File

@ -1,10 +1,11 @@
package PR2.HitoriSpiel.GUI; package PR2.HitoriSpiel.GUI;
import PR2.HitoriSpiel.Domain.HitoriBoard; import PR2.HitoriSpiel.Domain.HitoriBoard;
import PR2.HitoriSpiel.Domain.HitoriSolutionLoader;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.io.File; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.List; import java.util.List;
@ -93,8 +94,20 @@ public class StartMenu extends JFrame {
String resourcePath = "/persistent/Hitori_Spielfelder/" + selectedFile; String resourcePath = "/persistent/Hitori_Spielfelder/" + selectedFile;
System.out.println("Lade Datei von Pfad: " + resourcePath); System.out.println("Lade Datei von Pfad: " + resourcePath);
int[][] boardData = BoardLoader.loadBoard(resourcePath); int[][] boardData = BoardLoader.loadBoard(resourcePath);
List<String> solutionCoordinates = HitoriSolutionLoader.loadSolution(resourcePath);
HitoriBoard hitoriBoard = new HitoriBoard(boardData, solutionCoordinates); // Stelle sicher, dass die Lösung korrekt geladen wird
GameBoard gameBoard = new GameBoard(hitoriBoard);
loadGameBoard(boardData); loadGameBoard(boardData);
//addGameControls(gameBoard);
System.out.println("Verfügbare Spielfelder: " + boardFileNames);
System.out.println("Ausgewählte Datei: " + selectedFile);
System.out.println("Lade Datei von Pfad: " + resourcePath);
} catch (Exception ex) { } catch (Exception ex) {
System.err.println("Fehler beim Laden der Datei: " + ex.getMessage()); System.err.println("Fehler beim Laden der Datei: " + ex.getMessage());
@ -121,6 +134,14 @@ public class StartMenu extends JFrame {
return button; return button;
} }
/*private void loadGameBoard(GameBoard gameBoard) {
mainPanel.removeAll();
mainPanel.setLayout(new BorderLayout());
mainPanel.add(gameBoard, BorderLayout.CENTER);
mainPanel.revalidate();
mainPanel.repaint();
}*/
private void loadGameBoard(int[][] boardData) { private void loadGameBoard(int[][] boardData) {
// Neues Panel für das Spielfeld // Neues Panel für das Spielfeld
mainPanel.removeAll(); mainPanel.removeAll();
@ -130,4 +151,53 @@ public class StartMenu extends JFrame {
mainPanel.repaint(); mainPanel.repaint();
} }
private void addGameControls(GameBoard gameBoard) {
JPanel controlPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); // Buttons zentriert anordnen
// "Zurücksetzen"-Button
JButton resetButton = new JButton("Zurücksetzen");
resetButton.addActionListener(e -> {
gameBoard.resetBoard(); // Zurücksetzen des Spielfelds
JOptionPane.showMessageDialog(this, "Spielfeld zurückgesetzt!", "Info", JOptionPane.INFORMATION_MESSAGE);
});
// "Validieren"-Button
JButton validateButton = new JButton("Validieren");
validateButton.addActionListener(e -> {
boolean isValid = gameBoard.validateCurrentBoard(); // Prüfen, ob das Spielfeld korrekt gelöst ist
if (isValid) {
JOptionPane.showMessageDialog(this, "Das Spielfeld ist korrekt gelöst!", "Erfolg", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "Das Spielfeld enthält Fehler!", "Fehler", JOptionPane.ERROR_MESSAGE);
}
});
controlPanel.add(resetButton);
controlPanel.add(validateButton);
// Control-Panel unterhalb des Spielfelds hinzufügen
mainPanel.add(controlPanel, BorderLayout.SOUTH);
mainPanel.revalidate();
mainPanel.repaint();
}
/* private void loadAndShowGameBoard(String resourcePath) {
try {
int[][] boardData = BoardLoader.loadBoard(resourcePath);
List<String> solutionCoordinates = HitoriSolutionLoader.loadSolution(resourcePath);
HitoriBoard hitoriBoard = new HitoriBoard(boardData, solutionCoordinates);
GameBoard gameBoard = new GameBoard(hitoriBoard);
loadGameBoard(boardData);
addGameControls(gameBoard);
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, "Fehler beim Laden des Spielfelds: " + ex.getMessage(), "Fehler", JOptionPane.ERROR_MESSAGE);
ex.printStackTrace();
}
}*/
} }

View File

@ -4,9 +4,21 @@ import PR2.HitoriSpiel.GUI.StartMenu;
import javax.swing.*; import javax.swing.*;
/**
* Einstiegspunkt des Hitori-Spiels. Initialisiert die GUI und startet das Programm.
*/
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new StartMenu()); SwingUtilities.invokeLater(() -> {
try {
new StartMenu();
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Ein schwerer Fehler ist aufgetreten: " + e.getMessage(), "Fehler", JOptionPane.ERROR_MESSAGE);
}
});
} }
} }