Compare commits
6 Commits
8f7d815eb5
...
1b64e31951
Author | SHA1 | Date |
---|---|---|
|
1b64e31951 | |
|
2205f1bf04 | |
|
9267aec8e4 | |
|
27f1951fb5 | |
|
9d30065f9a | |
|
9df1c5e421 |
|
@ -54,7 +54,22 @@ public class HitoriBoard {
|
|||
}
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public int[][] getNumbers() {
|
||||
int[][] numbers = new int[board.length][board[0].length];
|
||||
for (int i = 0; i < board.length; i++) {
|
||||
for (int j = 0; j < board[i].length; j++) {
|
||||
numbers[i][j] = board[i][j].getNumber();
|
||||
}
|
||||
}
|
||||
return numbers;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ public class HitoriCell {
|
|||
public enum CellState {
|
||||
GRAY, WHITE, BLACK
|
||||
}
|
||||
private final int number;
|
||||
private int number;
|
||||
private CellState state;
|
||||
|
||||
public HitoriCell(int number) {
|
||||
|
@ -29,4 +29,8 @@ public class HitoriCell {
|
|||
this.state = state;
|
||||
}
|
||||
|
||||
public void setNumber(int number) {
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
package PR2.HitoriSpiel.Domain;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
public class StateManager {
|
||||
private final Stack<int[][]> undoStack = new Stack<>();
|
||||
private final Stack<int[][]> redoStack = new Stack<>();
|
||||
|
||||
public void saveState(int[][] state) {
|
||||
undoStack.push(copyArray(state));
|
||||
redoStack.clear(); // Redo-Stack leeren, da ein neuer Zustand hinzugefügt wurde
|
||||
}
|
||||
|
||||
public int[][] undo(int[][] currentState) {
|
||||
if (!undoStack.isEmpty()) {
|
||||
redoStack.push(copyArray(currentState)); // Aktuellen Zustand im Redo-Stack speichern
|
||||
return undoStack.pop();
|
||||
}
|
||||
return null; // Kein Zustand zum Zurücksetzen
|
||||
}
|
||||
|
||||
public int[][] redo(int[][] currentState) {
|
||||
if (!redoStack.isEmpty()) {
|
||||
undoStack.push(copyArray(currentState)); // Aktuellen Zustand im Undo-Stack speichern
|
||||
return redoStack.pop();
|
||||
}
|
||||
return null; // Kein Zustand zum Wiederherstellen
|
||||
}
|
||||
|
||||
private int[][] copyArray(int[][] original) {
|
||||
int[][] copy = new int[original.length][];
|
||||
for (int i = 0; i < original.length; i++) {
|
||||
copy[i] = original[i].clone();
|
||||
}
|
||||
return copy;
|
||||
}
|
||||
}
|
|
@ -1,38 +1,170 @@
|
|||
package PR2.HitoriSpiel.GUI;
|
||||
|
||||
import PR2.HitoriSpiel.Domain.StateManager;
|
||||
import PR2.HitoriSpiel.Domain.HitoriBoard;
|
||||
import PR2.HitoriSpiel.Domain.HitoriCell;
|
||||
import PR2.HitoriSpiel.Domain.HitoriValidator;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
public class GameBoard extends JPanel {
|
||||
private final HitoriBoard board;
|
||||
private Timer timer;
|
||||
private long startTime;
|
||||
private JLabel timerLabel;
|
||||
private final StateManager stateManager = new StateManager();
|
||||
|
||||
public class GameBoard extends JPanel {
|
||||
|
||||
private final HitoriBoard board; // Verbindung zur Logik
|
||||
|
||||
public GameBoard(HitoriBoard board) {
|
||||
this.board = board;
|
||||
setLayout(new GridLayout(board.getSize(), board.getSize()));
|
||||
setLayout(new BorderLayout());
|
||||
|
||||
// Buttons für jede Zelle erstellen
|
||||
// Timer-Label oben hinzufügen
|
||||
timerLabel = new JLabel("Zeit: 0 Sekunden");
|
||||
timerLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
add(timerLabel, BorderLayout.NORTH);
|
||||
|
||||
// Timer starten
|
||||
startTimer();
|
||||
|
||||
// Spielfeld erstellen
|
||||
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);
|
||||
add(button);
|
||||
gamePanel.add(button);
|
||||
}
|
||||
}
|
||||
add(gamePanel, BorderLayout.CENTER);
|
||||
|
||||
// Kontroll-Buttons unten hinzufügen
|
||||
JPanel controlPanel = createControlPanel();
|
||||
add(controlPanel, BorderLayout.SOUTH);
|
||||
}
|
||||
|
||||
private void startTimer() {
|
||||
startTime = System.currentTimeMillis();
|
||||
timer = new Timer(1000, e -> {
|
||||
long elapsedTime = (System.currentTimeMillis() - startTime) / 1000;
|
||||
timerLabel.setText("Zeit: " + elapsedTime + " Sekunden");
|
||||
});
|
||||
timer.start();
|
||||
}
|
||||
|
||||
private void stopTimer() {
|
||||
if (timer != null) {
|
||||
timer.stop();
|
||||
}
|
||||
}
|
||||
|
||||
private JPanel createControlPanel() {
|
||||
JPanel controlPanel = new JPanel();
|
||||
|
||||
JButton resetButton = createButton("Zurücksetzen", 200, 30);
|
||||
resetButton.addActionListener(e -> {
|
||||
saveStateForUndo();
|
||||
resetBoard();
|
||||
refreshBoard();
|
||||
});
|
||||
|
||||
JButton undoButton = createButton("Undo", 200, 30);
|
||||
undoButton.addActionListener(e -> {
|
||||
int[][] previousState = stateManager.undo(board.getNumbers());
|
||||
if (previousState != null) {
|
||||
board.setNumbers(previousState);
|
||||
refreshBoard();
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(this, "Kein Zustand zum Zurücksetzen vorhanden.", "Undo", JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
});
|
||||
|
||||
JButton redoButton = createButton("Redo", 200, 30);
|
||||
redoButton.addActionListener(e -> {
|
||||
int[][] nextState = stateManager.redo(board.getNumbers());
|
||||
if (nextState != null) {
|
||||
board.setNumbers(nextState);
|
||||
refreshBoard();
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(this, "Kein Zustand zum Wiederholen vorhanden.", "Redo", JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
});
|
||||
|
||||
JButton validateButton = createButton("Validieren", 200, 30);
|
||||
validateButton.addActionListener(e -> {
|
||||
if (validateCurrentBoard()) {
|
||||
stopTimer();
|
||||
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);
|
||||
}
|
||||
});
|
||||
|
||||
JButton pauseButton = createButton("Pause", 200, 30);
|
||||
pauseButton.addActionListener(e -> {
|
||||
stopTimer();
|
||||
showPauseMenu();
|
||||
});
|
||||
|
||||
|
||||
controlPanel.add(resetButton);
|
||||
controlPanel.add(undoButton);
|
||||
controlPanel.add(redoButton);
|
||||
controlPanel.add(validateButton);
|
||||
controlPanel.add(pauseButton);
|
||||
|
||||
return controlPanel;
|
||||
}
|
||||
|
||||
public void resetBoard() {
|
||||
// Spielfeldlogik zurücksetzen
|
||||
//stopTimer();
|
||||
//startTimer(); // Timer neu starten
|
||||
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);
|
||||
}
|
||||
}
|
||||
add(gamePanel, BorderLayout.CENTER);
|
||||
|
||||
// Kontroll-Buttons unten behalten
|
||||
JPanel controlPanel = createControlPanel();
|
||||
add(controlPanel, BorderLayout.SOUTH);
|
||||
|
||||
// Oberfläche neu laden
|
||||
revalidate();
|
||||
repaint();
|
||||
}
|
||||
|
||||
// TODO: Spiel validieren
|
||||
public boolean validateCurrentBoard() {
|
||||
return true;
|
||||
}
|
||||
|
||||
private void showPauseMenu() {
|
||||
PauseMenu pauseMenu = new PauseMenu(
|
||||
(JFrame) SwingUtilities.getWindowAncestor(this),
|
||||
e -> startTimer(), // Spiel fortsetzen
|
||||
e -> returnToMainMenu(), // Zum Hauptmenü
|
||||
e -> System.exit(0) // Spiel beenden
|
||||
);
|
||||
pauseMenu.setVisible(true);
|
||||
}
|
||||
|
||||
private JButton createCellButton(HitoriCell cell, int row, int col) {
|
||||
JButton button = new JButton(String.valueOf(cell.getNumber()));
|
||||
updateButtonState(button, cell);
|
||||
|
||||
// ActionListener für Benutzereinganen
|
||||
button.addActionListener((ActionEvent e) -> {
|
||||
button.setBackground(Color.LIGHT_GRAY);
|
||||
button.addActionListener(e -> {
|
||||
toggleCellState(cell);
|
||||
updateButtonState(button, cell);
|
||||
});
|
||||
|
@ -59,6 +191,7 @@ public class GameBoard extends JPanel {
|
|||
switch (cell.getState()) {
|
||||
case GRAY:
|
||||
button.setBackground(Color.LIGHT_GRAY);
|
||||
button.setText(String.valueOf(cell.getNumber()));
|
||||
break;
|
||||
case BLACK:
|
||||
button.setBackground(Color.BLACK);
|
||||
|
@ -66,33 +199,68 @@ public class GameBoard extends JPanel {
|
|||
break;
|
||||
case WHITE:
|
||||
button.setBackground(Color.WHITE);
|
||||
button.setText(String.valueOf(cell.getNumber()));
|
||||
button.setForeground(Color.BLACK);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void resetBoard() {
|
||||
board.resetBoard(); // Logik zurücksetzen
|
||||
removeAll(); // Entferne alle bisherigen Komponenten (Buttons)
|
||||
setLayout(new GridLayout(board.getSize(), board.getSize()));
|
||||
|
||||
// Neue Buttons erstellen und hinzufügen
|
||||
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);
|
||||
add(button);
|
||||
}
|
||||
}
|
||||
|
||||
revalidate(); // Layout neu laden
|
||||
repaint(); // Oberfläche neu zeichnen
|
||||
System.out.println("Spielfeld wurde aktualisiert");
|
||||
private void saveGame() {
|
||||
try {
|
||||
FileWriter writer = new FileWriter("hitori_savegame.txt");
|
||||
writer.write("Spielzeit: " + (System.currentTimeMillis() - startTime) / 1000 + " Sekunden\n");
|
||||
writer.write("Spielfeldzustand:\n");
|
||||
for (int i = 0; i < board.getSize(); i++) {
|
||||
for (int j = 0; j < board.getSize(); j++) {
|
||||
writer.write(board.getCell(i, j).getState().name() + " ");
|
||||
}
|
||||
writer.write("\n");
|
||||
}
|
||||
writer.close();
|
||||
JOptionPane.showMessageDialog(this, "Spiel erfolgreich gespeichert.");
|
||||
} catch (IOException ex) {
|
||||
JOptionPane.showMessageDialog(this, "Fehler beim Speichern des Spiels: " + ex.getMessage(), "Fehler", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean validateCurrentBoard() {
|
||||
HitoriValidator validator = new HitoriValidator(board);
|
||||
return validator.validateBoard(board.getSolutionCoordinates());
|
||||
private void returnToMainMenu() {
|
||||
// Eltern-Frame abrufen
|
||||
JFrame parentFrame = (JFrame) SwingUtilities.getWindowAncestor(this);
|
||||
|
||||
// Inhalt des Frames auf das Hauptmenü setzen
|
||||
parentFrame.getContentPane().removeAll();
|
||||
parentFrame.add(new StartMenu());
|
||||
parentFrame.revalidate();
|
||||
parentFrame.repaint();
|
||||
}
|
||||
|
||||
}
|
||||
private JButton createButton(String text, int width, int height) {
|
||||
JButton button = new JButton(text);
|
||||
button.setPreferredSize(new Dimension(width, height));
|
||||
return button;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
revalidate();
|
||||
repaint();
|
||||
}
|
||||
|
||||
private void saveStateForUndo() {
|
||||
stateManager.saveState(board.getNumbers());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
package PR2.HitoriSpiel.GUI;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class PauseMenu extends JDialog {
|
||||
|
||||
public PauseMenu(JFrame parent, ActionListener resumeAction, ActionListener mainMenuAction, ActionListener exitAction) {
|
||||
super(parent, "Pause", true);
|
||||
setLayout(new GridLayout(3, 1));
|
||||
setSize(300, 200);
|
||||
setLocationRelativeTo(parent);
|
||||
|
||||
// "Spiel fortsetzen"-Button
|
||||
JButton resumeButton = new JButton("Spiel fortsetzen");
|
||||
resumeButton.addActionListener(e -> {
|
||||
dispose();
|
||||
if (resumeAction != null) {
|
||||
resumeAction.actionPerformed(e);
|
||||
}
|
||||
});
|
||||
|
||||
// "Zum Hauptmenü"-Button
|
||||
JButton mainMenuButton = new JButton("Zum Hauptmenü");
|
||||
mainMenuButton.addActionListener(e -> {
|
||||
dispose();
|
||||
if (mainMenuAction != null) {
|
||||
mainMenuAction.actionPerformed(e);
|
||||
}
|
||||
});
|
||||
|
||||
// "Spiel beenden"-Button
|
||||
JButton exitButton = new JButton("Spiel beenden");
|
||||
exitButton.addActionListener(e -> {
|
||||
dispose();
|
||||
if (exitAction != null) {
|
||||
exitAction.actionPerformed(e);
|
||||
}
|
||||
});
|
||||
|
||||
// Buttons hinzufügen
|
||||
add(resumeButton);
|
||||
add(mainMenuButton);
|
||||
add(exitButton);
|
||||
}
|
||||
}
|
|
@ -8,6 +8,9 @@ import java.awt.*;
|
|||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
import static PR2.HitoriSpiel.GUI.BoardLoader.loadBoard;
|
||||
|
||||
public class StartMenu extends JFrame {
|
||||
private JPanel mainPanel;
|
||||
|
@ -95,7 +98,7 @@ public class StartMenu extends JFrame {
|
|||
System.out.println("Lade Datei von Pfad: " + resourcePath);
|
||||
|
||||
|
||||
int[][] boardData = BoardLoader.loadBoard(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
|
||||
|
@ -103,7 +106,7 @@ public class StartMenu extends JFrame {
|
|||
|
||||
loadGameBoard2(gameBoard, solutionCoordinates);
|
||||
//loadGameBoard(boardData);
|
||||
addGameControls(gameBoard);
|
||||
// addGameControls(gameBoard);
|
||||
|
||||
System.out.println("Verfügbare Spielfelder: " + boardFileNames);
|
||||
System.out.println("Ausgewählte Datei: " + selectedFile);
|
||||
|
@ -119,10 +122,46 @@ public class StartMenu extends JFrame {
|
|||
}
|
||||
|
||||
private void randomBoard() {
|
||||
// TODO: Logik zum Auswählen eines zufälligen Spielfeldes implementieren
|
||||
JOptionPane.showMessageDialog(this, "Zufälliges Spielfeld wurde angeklickt");
|
||||
// Lade alle verfügbaren Spielfeld-Dateien
|
||||
List<String> boardFileNames = BoardLoader.loadBoardsAsList();
|
||||
|
||||
// Überprüfe, ob Dateien verfügbar sind
|
||||
if (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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void highscorelist() {
|
||||
// TODO: Logik zur Anzeige der Highscoreliste implementieren
|
||||
JOptionPane.showMessageDialog(this, "Highscoreliste wurde angeklickt");
|
||||
|
@ -152,54 +191,5 @@ public class StartMenu extends JFrame {
|
|||
mainPanel.repaint();
|
||||
}
|
||||
|
||||
private void addGameControls(GameBoard gameBoard) {
|
||||
JPanel controlPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); // Buttons zentriert anordnen
|
||||
|
||||
// "Zurücksetzen"-Button
|
||||
JButton resetButton = createButton("Zurücksetzen", 200, 30);
|
||||
resetButton.addActionListener(e -> {
|
||||
System.out.println("Reset-Button gedrückt");
|
||||
gameBoard.resetBoard(); // Zurücksetzen des Spielfelds
|
||||
//JOptionPane.showMessageDialog(this, "Spielfeld zurückgesetzt!", "Info", JOptionPane.INFORMATION_MESSAGE);
|
||||
});
|
||||
|
||||
// "Validieren"-Button
|
||||
JButton validateButton = createButton("Validieren", 200, 30);
|
||||
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();
|
||||
}
|
||||
}*/
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue