From aebfb7e38bc82422322e07baa3987a190a49b670 Mon Sep 17 00:00:00 2001 From: Justin Date: Tue, 24 Jun 2025 05:04:44 +0200 Subject: [PATCH] quicksave feature implemented --- .../chess/controller/GameController.java | 25 ++++++++++++- .../chess/controller/MainController.java | 36 ++++++++++++++++--- .../informatik/chess/model/ChessEngine.java | 34 ++++++++++++++++++ .../informatik/chess/model/Timer.java | 3 ++ .../informatik/chess/view/GameGui.java | 11 ++++-- 5 files changed, 100 insertions(+), 9 deletions(-) diff --git a/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/GameController.java b/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/GameController.java index 33ae675..57f7351 100644 --- a/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/GameController.java +++ b/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/GameController.java @@ -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, @@ -346,7 +364,7 @@ public class GameController { return "" + file + rank; } - + // Timeout-Methode private void onTimeout(String color) { if (gameOver) return; // Doppelt hält besser @@ -367,11 +385,16 @@ 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); diff --git a/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/MainController.java b/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/MainController.java index 7335839..3000378 100644 --- a/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/MainController.java +++ b/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/MainController.java @@ -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,20 +31,45 @@ public class MainController { } private void startNormalMode() { - - GameMode mode = GameModeSelector.selectGameMode(); + // 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(); - GameGui gameGui = new GameGui(); 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(); } public void onReturnToMenu() { - new MainController(); + new MainController(); } }; - new GameController(gameGui, engine, callback,mode); + new GameController(gameGui, engine, callback, mode); } private void startCreativeMode() { diff --git a/schach/src/main/java/de/hs_mannheim/informatik/chess/model/ChessEngine.java b/schach/src/main/java/de/hs_mannheim/informatik/chess/model/ChessEngine.java index b919a62..c198508 100644 --- a/schach/src/main/java/de/hs_mannheim/informatik/chess/model/ChessEngine.java +++ b/schach/src/main/java/de/hs_mannheim/informatik/chess/model/ChessEngine.java @@ -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 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 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())); diff --git a/schach/src/main/java/de/hs_mannheim/informatik/chess/model/Timer.java b/schach/src/main/java/de/hs_mannheim/informatik/chess/model/Timer.java index 6a26896..e8ce7a6 100644 --- a/schach/src/main/java/de/hs_mannheim/informatik/chess/model/Timer.java +++ b/schach/src/main/java/de/hs_mannheim/informatik/chess/model/Timer.java @@ -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; } diff --git a/schach/src/main/java/de/hs_mannheim/informatik/chess/view/GameGui.java b/schach/src/main/java/de/hs_mannheim/informatik/chess/view/GameGui.java index 36e8893..5c8e537 100644 --- a/schach/src/main/java/de/hs_mannheim/informatik/chess/view/GameGui.java +++ b/schach/src/main/java/de/hs_mannheim/informatik/chess/view/GameGui.java @@ -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; + } + }