quicksave feature implemented

PMD-Korrektur
Justin 2025-06-24 05:04:44 +02:00
parent d0c267f7b0
commit aebfb7e38b
5 changed files with 100 additions and 9 deletions

View File

@ -35,6 +35,7 @@ public class GameController {
engine.initTimers(gameMode.minutes, gameMode.incrementSeconds);
time();
}
addWindowCloseListener();
}
// Für Creative/PGN-Mode (ohne Zeit)
@ -43,6 +44,7 @@ public class GameController {
this.engine = engine;
this.callback = callback;
this.gameMode = null;
addWindowCloseListener();
// KEINE Timer initialisieren
initListeners();
updateGuiBoard();
@ -61,6 +63,20 @@ public class GameController {
return gui.isFlipped() ? 7 - col : col;
}
private void addWindowCloseListener() {
if (gui.getFrame() != null) {
gui.getFrame().addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
if (!gameOver) {
engine.quicksave();
MainController.engineRAM = engine;
}
new MainController();
}
});
}
}
private void initListeners() {
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
@ -137,6 +153,7 @@ public class GameController {
// --- AUFGEBEN-BUTTON ---
gui.getResignButton().addActionListener(e -> {
engine.clearQuicksave();
if (gameOver) return;
int answer = javax.swing.JOptionPane.showConfirmDialog(
null,
@ -155,6 +172,7 @@ public class GameController {
});
gui.getDrawButton().addActionListener(e -> {
engine.clearQuicksave();
if (gameOver) return;
int answer = javax.swing.JOptionPane.showConfirmDialog(
null,
@ -367,12 +385,17 @@ public class GameController {
);
javax.swing.SwingUtilities.getWindowAncestor(gui.getField(0, 0)).dispose();
if (answer == javax.swing.JOptionPane.YES_OPTION) {
engine.clearQuicksave(); // Quicksave löschen!
MainController.engineRAM = null;
callback.onNewGameRequested();
} else {
engine.quicksave(); // Im RAM speichern!
MainController.engineRAM = engine; // Referenz für das nächste Mal
callback.onReturnToMenu();
}
}
private void resetFieldBackground(int row, int col) {
Color LIGHT = new Color(0xe0e1dd);
Color DARK = new Color(0x778da9);

View File

@ -20,6 +20,7 @@ import de.hs_mannheim.informatik.chess.model.GameMode;
public class MainController {
private MainGui mainGui;
static ChessEngine engineRAM = null;
public MainController() {
mainGui = new MainGui();
@ -30,11 +31,36 @@ public class MainController {
}
private void startNormalMode() {
GameMode mode = GameModeSelector.selectGameMode();
mainGui.close();
// Prüfe, ob es im RAM ein Quicksave gibt:
if (engineRAM != null && engineRAM.quickload()) {
int choice = JOptionPane.showConfirmDialog(
null,
"Letzte Partie fortsetzen?",
"Quicksave gefunden",
JOptionPane.YES_NO_OPTION
);
if (choice == JOptionPane.YES_OPTION) {
GameGui gameGui = new GameGui();
GameEndCallback callback = new GameEndCallback() {
public void onNewGameRequested() { startNormalMode(); }
public void onReturnToMenu() { new MainController(); }
};
new GameController(gameGui, engineRAM, callback);
mainGui.close();
return; // Fertig!
} else {
engineRAM.clearQuicksave();
engineRAM = null;
}
}
// Neues Spiel normal starten:
GameMode mode = GameModeSelector.selectGameMode();
if (mode == null) return;
mainGui.close();
ChessEngine engine = new ChessEngine(mode);
engineRAM = engine; // Für spätere Quicksaves merken!
GameGui gameGui = new GameGui();
GameEndCallback callback = new GameEndCallback() {
public void onNewGameRequested() {
startNormalMode();
@ -43,7 +69,7 @@ public class MainController {
new MainController();
}
};
new GameController(gameGui, engine, callback,mode);
new GameController(gameGui, engine, callback, mode);
}
private void startCreativeMode() {

View File

@ -1,5 +1,8 @@
package de.hs_mannheim.informatik.chess.model;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
@ -27,12 +30,18 @@ public class ChessEngine {
private List<Move> moves = new ArrayList<>();
private String initialFen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
private static final Logger logger = Logger.getLogger(ChessEngine.class.getName());
private int quicksaveWhiteTimeLeft = -1;
private int quicksaveBlackTimeLeft = -1;
private int currentMoveIndex = 0;
private Timer whiteTimer;
private Timer blackTimer;
private final GameMode mode;
private String quicksaveFen = null;
private List<Move> quicksaveMoves = null;
private int quicksaveMoveIndex = 0;
private Opening detectedOpening = null;
@ -131,6 +140,31 @@ public class ChessEngine {
return result;
}
public void quicksave() {
this.quicksaveFen = board.getFen();
this.quicksaveMoves = new ArrayList<>(moves);
this.quicksaveMoveIndex = currentMoveIndex;
if (whiteTimer != null) quicksaveWhiteTimeLeft = whiteTimer.getSecondsLeft();
if (blackTimer != null) quicksaveBlackTimeLeft = blackTimer.getSecondsLeft();
}
public boolean quickload() {
if (quicksaveFen == null) return false;
board = new Board();
board.loadFromFen(quicksaveFen);
moves = new ArrayList<>(quicksaveMoves);
currentMoveIndex = quicksaveMoveIndex;
if (whiteTimer != null && quicksaveWhiteTimeLeft != -1) whiteTimer.setSecondsLeft(quicksaveWhiteTimeLeft);
if (blackTimer != null && quicksaveBlackTimeLeft != -1) blackTimer.setSecondsLeft(quicksaveBlackTimeLeft);
return true;
}
public void clearQuicksave() {
quicksaveFen = null;
quicksaveMoves = null;
quicksaveMoveIndex = 0;
}
public PieceDTO getPieceAt(String square) {
logger.info("Hole Figur an Feld: " + square);
Piece piece = board.getPiece(Square.valueOf(square.toUpperCase()));

View File

@ -57,6 +57,9 @@ public class Timer {
return secondsLeft;
}
public void setSecondsLeft(int secondsLeft) {
this.secondsLeft = secondsLeft;
}
public void addSeconds(int seconds) {
this.secondsLeft += seconds;
}

View File

@ -26,6 +26,7 @@ import de.hs_mannheim.informatik.chess.model.BoardDTO;
import de.hs_mannheim.informatik.chess.model.PieceDTO;
public class GameGui {
JFrame frame;
private JLabel[][] fields = new JLabel[8][8];
private JButton flipBoardButton;
@ -43,7 +44,6 @@ public class GameGui {
private JButton resignButton;
private JButton drawButton;
private JButton quickSaveButton;
private JButton undoButton;
Color LIGHT = new Color(0xe0e1dd);
@ -59,13 +59,14 @@ public class GameGui {
public JFrame mainFrame() {
JFrame frame = new JFrame();
frame = new JFrame();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(false);
frame.setLocationRelativeTo(null);
frame.add(mainPanel());
frame.setDefaultCloseOperation(2);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setVisible(true);
return frame;
}
@ -378,4 +379,8 @@ public class GameGui {
JOptionPane.showMessageDialog(null, msg);
}
public JFrame getFrame() {
return frame;
}
}