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); engine.initTimers(gameMode.minutes, gameMode.incrementSeconds);
time(); time();
} }
addWindowCloseListener();
} }
// Für Creative/PGN-Mode (ohne Zeit) // Für Creative/PGN-Mode (ohne Zeit)
@ -43,6 +44,7 @@ public class GameController {
this.engine = engine; this.engine = engine;
this.callback = callback; this.callback = callback;
this.gameMode = null; this.gameMode = null;
addWindowCloseListener();
// KEINE Timer initialisieren // KEINE Timer initialisieren
initListeners(); initListeners();
updateGuiBoard(); updateGuiBoard();
@ -61,6 +63,20 @@ public class GameController {
return gui.isFlipped() ? 7 - col : col; 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() { private void initListeners() {
for (int row = 0; row < 8; row++) { for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) { for (int col = 0; col < 8; col++) {
@ -137,6 +153,7 @@ public class GameController {
// --- AUFGEBEN-BUTTON --- // --- AUFGEBEN-BUTTON ---
gui.getResignButton().addActionListener(e -> { gui.getResignButton().addActionListener(e -> {
engine.clearQuicksave();
if (gameOver) return; if (gameOver) return;
int answer = javax.swing.JOptionPane.showConfirmDialog( int answer = javax.swing.JOptionPane.showConfirmDialog(
null, null,
@ -155,6 +172,7 @@ public class GameController {
}); });
gui.getDrawButton().addActionListener(e -> { gui.getDrawButton().addActionListener(e -> {
engine.clearQuicksave();
if (gameOver) return; if (gameOver) return;
int answer = javax.swing.JOptionPane.showConfirmDialog( int answer = javax.swing.JOptionPane.showConfirmDialog(
null, null,
@ -346,7 +364,7 @@ public class GameController {
return "" + file + rank; return "" + file + rank;
} }
// Timeout-Methode // Timeout-Methode
private void onTimeout(String color) { private void onTimeout(String color) {
if (gameOver) return; // Doppelt hält besser if (gameOver) return; // Doppelt hält besser
@ -367,11 +385,16 @@ public class GameController {
); );
javax.swing.SwingUtilities.getWindowAncestor(gui.getField(0, 0)).dispose(); javax.swing.SwingUtilities.getWindowAncestor(gui.getField(0, 0)).dispose();
if (answer == javax.swing.JOptionPane.YES_OPTION) { if (answer == javax.swing.JOptionPane.YES_OPTION) {
engine.clearQuicksave(); // Quicksave löschen!
MainController.engineRAM = null;
callback.onNewGameRequested(); callback.onNewGameRequested();
} else { } else {
engine.quicksave(); // Im RAM speichern!
MainController.engineRAM = engine; // Referenz für das nächste Mal
callback.onReturnToMenu(); callback.onReturnToMenu();
} }
} }
private void resetFieldBackground(int row, int col) { private void resetFieldBackground(int row, int col) {
Color LIGHT = new Color(0xe0e1dd); Color LIGHT = new Color(0xe0e1dd);

View File

@ -20,6 +20,7 @@ import de.hs_mannheim.informatik.chess.model.GameMode;
public class MainController { public class MainController {
private MainGui mainGui; private MainGui mainGui;
static ChessEngine engineRAM = null;
public MainController() { public MainController() {
mainGui = new MainGui(); mainGui = new MainGui();
@ -30,20 +31,45 @@ public class MainController {
} }
private void startNormalMode() { private void startNormalMode() {
// Prüfe, ob es im RAM ein Quicksave gibt:
GameMode mode = GameModeSelector.selectGameMode(); 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(); mainGui.close();
GameGui gameGui = new GameGui();
ChessEngine engine = new ChessEngine(mode); ChessEngine engine = new ChessEngine(mode);
engineRAM = engine; // Für spätere Quicksaves merken!
GameGui gameGui = new GameGui();
GameEndCallback callback = new GameEndCallback() { GameEndCallback callback = new GameEndCallback() {
public void onNewGameRequested() { public void onNewGameRequested() {
startNormalMode(); startNormalMode();
} }
public void onReturnToMenu() { public void onReturnToMenu() {
new MainController(); new MainController();
} }
}; };
new GameController(gameGui, engine, callback,mode); new GameController(gameGui, engine, callback, mode);
} }
private void startCreativeMode() { private void startCreativeMode() {

View File

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

View File

@ -57,6 +57,9 @@ public class Timer {
return secondsLeft; return secondsLeft;
} }
public void setSecondsLeft(int secondsLeft) {
this.secondsLeft = secondsLeft;
}
public void addSeconds(int seconds) { public void addSeconds(int seconds) {
this.secondsLeft += 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; import de.hs_mannheim.informatik.chess.model.PieceDTO;
public class GameGui { public class GameGui {
JFrame frame;
private JLabel[][] fields = new JLabel[8][8]; private JLabel[][] fields = new JLabel[8][8];
private JButton flipBoardButton; private JButton flipBoardButton;
@ -43,7 +44,6 @@ public class GameGui {
private JButton resignButton; private JButton resignButton;
private JButton drawButton; private JButton drawButton;
private JButton quickSaveButton;
private JButton undoButton; private JButton undoButton;
Color LIGHT = new Color(0xe0e1dd); Color LIGHT = new Color(0xe0e1dd);
@ -59,13 +59,14 @@ public class GameGui {
public JFrame mainFrame() { public JFrame mainFrame() {
JFrame frame = new JFrame(); frame = new JFrame();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH); frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(false); frame.setUndecorated(false);
frame.setLocationRelativeTo(null); frame.setLocationRelativeTo(null);
frame.add(mainPanel()); frame.add(mainPanel());
frame.setDefaultCloseOperation(2); frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setVisible(true); frame.setVisible(true);
return frame; return frame;
} }
@ -378,4 +379,8 @@ public class GameGui {
JOptionPane.showMessageDialog(null, msg); JOptionPane.showMessageDialog(null, msg);
} }
public JFrame getFrame() {
return frame;
}
} }