Compare commits

...

5 Commits

Author SHA1 Message Date
Vickvick2002 078d653921 GameBoard Code erweitert und verbessert 2025-01-04 13:50:22 +01:00
Vickvick2002 6462424563 HitoriBoard Code verändert 2025-01-04 13:50:11 +01:00
Vickvick2002 152b50fc69 HitoriSolutionLoader Code verändert 2025-01-04 13:49:54 +01:00
Vickvick2002 43c73b0289 Main Code verbessert 2025-01-04 13:49:40 +01:00
Vickvick2002 3b6e3a45a7 StartMenu Code erweitert 2025-01-04 13:49:29 +01:00
5 changed files with 69 additions and 103 deletions

View File

@ -49,7 +49,6 @@ public class HitoriBoard {
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
System.out.println("Zelle Test (" + i + "," + j + ") zurückgesetzt auf GRAY");
}
}
}

View File

@ -34,5 +34,4 @@ public class HitoriSolutionLoader {
return solutionCoordinates;
}
}

View File

@ -64,14 +64,14 @@ public class GameBoard extends JPanel {
private JPanel createControlPanel() {
JPanel controlPanel = new JPanel();
JButton resetButton = createButton("Zurücksetzen", 200, 30);
JButton resetButton = createButton("Zurücksetzen", 150, 30);
resetButton.addActionListener(e -> {
saveStateForUndo();
resetBoard();
refreshBoard();
});
JButton undoButton = createButton("Undo", 200, 30);
JButton undoButton = createButton("Undo", 80, 30);
undoButton.addActionListener(e -> {
int[][] previousState = stateManager.undo(board.getNumbers());
if (previousState != null) {
@ -82,7 +82,7 @@ public class GameBoard extends JPanel {
}
});
JButton redoButton = createButton("Redo", 200, 30);
JButton redoButton = createButton("Redo", 80, 30);
redoButton.addActionListener(e -> {
int[][] nextState = stateManager.redo(board.getNumbers());
if (nextState != null) {
@ -93,7 +93,7 @@ public class GameBoard extends JPanel {
}
});
JButton validateButton = createButton("Validieren", 200, 30);
JButton validateButton = createButton("Validieren", 100, 30);
validateButton.addActionListener(e -> {
if (validateCurrentBoard()) {
stopTimer();
@ -103,7 +103,7 @@ public class GameBoard extends JPanel {
}
});
JButton pauseButton = createButton("Pause", 200, 30);
JButton pauseButton = createButton("Pause", 100, 30);
pauseButton.addActionListener(e -> {
stopTimer();
showPauseMenu();
@ -120,28 +120,31 @@ public class GameBoard extends JPanel {
}
public void resetBoard() {
// Spielfeldlogik zurücksetzen
//stopTimer();
//startTimer(); // Timer neu starten
// Spiellogik zurücksetzen
board.resetBoard();
removeAll();
add(timerLabel, BorderLayout.NORTH);
JPanel gamePanel = new JPanel(new GridLayout(board.getSize(), board.getSize()));
for (int i = 0; i < board.getSize(); i++) {
for (int j = 0; j < board.getSize(); j++) {
HitoriCell cell = board.getCell(i, j);
JButton button = createCellButton(cell, i, j);
gamePanel.add(button);
// Spielfeld (CENTER) entfernen und neu erstellen
if (getLayout() instanceof BorderLayout) {
BorderLayout layout = (BorderLayout) getLayout();
// Komponente im CENTER entfernen
Component centerComponent = layout.getLayoutComponent(BorderLayout.CENTER);
if (centerComponent != null) {
remove(centerComponent);
}
// Neues Spielfeld hinzufügen
add(createGamePanel(), BorderLayout.CENTER);
// Kontroll-Buttons (SOUTH) entfernen und neu hinzufügen
Component southComponent = layout.getLayoutComponent(BorderLayout.SOUTH);
if (southComponent != null) {
remove(southComponent);
}
add(createControlPanel(), BorderLayout.SOUTH);
}
add(gamePanel, BorderLayout.CENTER);
// Kontroll-Buttons unten behalten
JPanel controlPanel = createControlPanel();
add(controlPanel, BorderLayout.SOUTH);
// Oberfläche neu laden
// Oberfläche aktualisieren
revalidate();
repaint();
}
@ -241,19 +244,8 @@ public class GameBoard extends JPanel {
}
private void refreshBoard() {
removeAll();
JPanel gamePanel = new JPanel(new GridLayout(board.getSize(), board.getSize()));
for (int i = 0; i < board.getSize(); i++) {
for (int j = 0; j < board.getSize(); j++) {
HitoriCell cell = board.getCell(i, j);
JButton button = createCellButton(cell, i, j);
gamePanel.add(button);
}
}
add(gamePanel, BorderLayout.CENTER);
JPanel controlPanel = createControlPanel();
add(controlPanel, BorderLayout.SOUTH);
remove(1); // Entferne das aktuelle Spielfeld im CENTER
add(createGamePanel(), BorderLayout.CENTER); // Füge das neue Spielfeld hinzu
revalidate();
repaint();
@ -263,4 +255,17 @@ public class GameBoard extends JPanel {
stateManager.saveState(board.getNumbers());
}
private JPanel createGamePanel() {
JPanel gamePanel = new JPanel(new GridLayout(board.getSize(), board.getSize()));
for (int i = 0; i < board.getSize(); i++) {
for (int j = 0; j < board.getSize(); j++) {
HitoriCell cell = board.getCell(i, j);
JButton button = createCellButton(cell, i, j);
gamePanel.add(button);
}
}
System.out.println("Spielpanel erstellt mit " + (board.getSize() * board.getSize()) + " Buttons.");
return gamePanel;
}
}

View File

@ -5,6 +5,7 @@ import PR2.HitoriSpiel.Domain.HitoriSolutionLoader;
import javax.swing.*;
import java.awt.*;
import javax.swing.JPanel;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
@ -68,8 +69,8 @@ public class StartMenu extends JFrame {
List<String> boardFileNames = BoardLoader.loadBoardsAsList();
System.out.println("Verfügbare Spielfelder: " + boardFileNames);
if (boardFileNames.isEmpty()) {
JOptionPane.showMessageDialog(this, "Keine Spielfelder gefunden.", "Information", JOptionPane.INFORMATION_MESSAGE);
if (boardFileNames == null || boardFileNames.isEmpty()) {
JOptionPane.showMessageDialog(this, "Keine Spielfelder gefunden.", "Fehler", JOptionPane.ERROR_MESSAGE);
return;
}
@ -91,74 +92,23 @@ public class StartMenu extends JFrame {
if (option == JOptionPane.OK_OPTION) {
String selectedFile = (String) boardSelector.getSelectedItem();
System.out.println("Ausgewählte Datei: " + selectedFile);
if (selectedFile != null) {
try {
String resourcePath = "/persistent/Hitori_Spielfelder/" + selectedFile;
System.out.println("Lade Datei von Pfad: " + resourcePath);
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
GameBoard gameBoard = new GameBoard(hitoriBoard);
loadGameBoard2(gameBoard, solutionCoordinates);
//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) {
System.err.println("Fehler beim Laden der Datei: " + ex.getMessage());
JOptionPane.showMessageDialog(this, "Fehler beim Laden des Spielfelds: " + ex.getMessage(), "Fehler", JOptionPane.ERROR_MESSAGE);
}
// Lade das ausgewählte Spielfeld
System.out.println("Ich bin drin.");
loadAndDisplayBoard(selectedFile);
}
}
}
private void randomBoard() {
// Lade alle verfügbaren Spielfeld-Dateien
List<String> boardFileNames = BoardLoader.loadBoardsAsList();
// Überprüfe, ob Dateien verfügbar sind
if (boardFileNames.isEmpty()) {
if (boardFileNames == null || boardFileNames.isEmpty()) {
JOptionPane.showMessageDialog(this, "Keine Spielfelder gefunden.", "Fehler", JOptionPane.ERROR_MESSAGE);
return;
}
// Wähle eine zufällige Datei
String randomFile = boardFileNames.get(new Random().nextInt(boardFileNames.size()));
System.out.println("Zufällige Datei gewählt: " + randomFile);
try {
// Lade das zufällige Spielfeld
String resourcePath = "/persistent/Hitori_Spielfelder/" + randomFile;
System.out.println("Lade Datei von Pfad: " + resourcePath);
InputStream inputStream = getClass().getResourceAsStream(resourcePath);
if (inputStream == null) {
throw new IOException("Ressourcendatei nicht gefunden: " + resourcePath);
}
// Spielfeld-Daten laden
int[][] boardData = BoardLoader.loadBoard(resourcePath);
List<String> solutionCoordinates = HitoriSolutionLoader.loadSolution(resourcePath);
// Spielfeld erstellen und anzeigen
HitoriBoard hitoriBoard = new HitoriBoard(boardData, solutionCoordinates);
GameBoard gameBoard = new GameBoard(hitoriBoard);
loadGameBoard2(gameBoard, solutionCoordinates);
} catch (IOException e) {
// Fehler beim Laden des Spielfelds
JOptionPane.showMessageDialog(this, "Fehler beim Laden des Spielfelds: " + e.getMessage(), "Fehler", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
loadAndDisplayBoard(randomFile);
}
@ -174,7 +124,7 @@ public class StartMenu extends JFrame {
return button;
}
private void loadGameBoard2(GameBoard gameBoard,List<String> solutionCoordinates) {
private void loadGameBoard(GameBoard gameBoard, List<String> solutionCoordinates) {
mainPanel.removeAll();
mainPanel.setLayout(new BorderLayout());
mainPanel.add(gameBoard, BorderLayout.CENTER);
@ -182,13 +132,25 @@ public class StartMenu extends JFrame {
mainPanel.repaint();
}
private void loadGameBoard(int[][] boardData) {
// Neues Panel für das Spielfeld
mainPanel.removeAll();
mainPanel.setLayout(new BorderLayout());
mainPanel.add(new GameBoard(new HitoriBoard(boardData, List.of())), BorderLayout.CENTER);
mainPanel.revalidate();
mainPanel.repaint();
private void loadAndDisplayBoard(String selectedFile) {
try {
String resourcePath = "/persistent/Hitori_Spielfelder/" + selectedFile;
System.out.println("Lade Datei von Pfad: " + resourcePath);
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
GameBoard gameBoard = new GameBoard(hitoriBoard);
//GameBoard gameBoard = new GameBoard(new HitoriBoard(boardData, solutionCoordinates));
loadGameBoard(gameBoard, solutionCoordinates);
System.out.println("Ausgewählte Datei: " + selectedFile);
System.out.println("Lade Datei von Pfad: " + resourcePath);
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Fehler beim Laden des Spielfelds: " + e.getMessage(), "Fehler", JOptionPane.ERROR_MESSAGE);
}
}

View File

@ -5,6 +5,7 @@ import PR2.HitoriSpiel.GUI.StartMenu;
import javax.swing.*;
/**
* Einstiegspunkt des Hitori-Spiels. Initialisiert die GUI und startet das Programm.
*/