From d0c267f7b071095958c9074499c9423f18d9a5c3 Mon Sep 17 00:00:00 2001 From: Justin Date: Tue, 24 Jun 2025 04:05:46 +0200 Subject: [PATCH] Major changes to project --- .../chess/controller/CreativeController.java | 2 +- .../chess/controller/GameController.java | 52 ++++++++++++++----- .../chess/controller/MainController.java | 28 ++++++---- .../informatik/chess/model/Timer.java | 4 ++ .../informatik/chess/view/PgnGui.java | 6 +++ 5 files changed, 66 insertions(+), 26 deletions(-) diff --git a/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/CreativeController.java b/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/CreativeController.java index 9d961e1..eacdbdf 100644 --- a/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/CreativeController.java +++ b/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/CreativeController.java @@ -29,7 +29,7 @@ public class CreativeController { map.put("WHITE_ROOK", 'R'); map.put("WHITE_BISHOP", 'B'); map.put("WHITE_KNIGHT", 'N'); - map.put("WHITE_PAWN", 'P'); + map.put("WHITE_PAWN", 'P'); return map; } 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 c42bc2d..33ae675 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 @@ -11,11 +11,12 @@ import javax.swing.BorderFactory; import com.github.bhlangonijr.chesslib.game.Game; import de.hs_mannheim.informatik.chess.model.ChessEngine; +import de.hs_mannheim.informatik.chess.model.GameMode; import de.hs_mannheim.informatik.chess.model.MoveDTO; import de.hs_mannheim.informatik.chess.model.PieceDTO; +import de.hs_mannheim.informatik.chess.model.Timer; import de.hs_mannheim.informatik.chess.model.BoardDTO; import de.hs_mannheim.informatik.chess.view.GameGui; -import de.hs_mannheim.informatik.chess.view.MainGui; public class GameController { GameGui gui; @@ -25,13 +26,24 @@ public class GameController { private boolean gameOver = false; private int selectedRow = -1, selectedCol = -1; private List highlightedFields = new ArrayList<>(); + private GameMode gameMode; + public GameController(GameGui gui, ChessEngine engine, GameEndCallback callback, GameMode gameMode) { + this(gui, engine, callback); // ruft anderen Konstruktor auf + this.gameMode = gameMode; + if (gameMode != null) { + engine.initTimers(gameMode.minutes, gameMode.incrementSeconds); + time(); + } + } + + // Für Creative/PGN-Mode (ohne Zeit) public GameController(GameGui gui, ChessEngine engine, GameEndCallback callback) { this.gui = gui; this.engine = engine; this.callback = callback; - engine.initTimers(3, 0); - time(); + this.gameMode = null; + // KEINE Timer initialisieren initListeners(); updateGuiBoard(); } @@ -126,7 +138,6 @@ public class GameController { // --- AUFGEBEN-BUTTON --- gui.getResignButton().addActionListener(e -> { if (gameOver) return; - int answer = javax.swing.JOptionPane.showConfirmDialog( null, "Willst du wirklich aufgeben?", @@ -137,16 +148,14 @@ public class GameController { gameOver = true; String winner = engine.getCurrentPlayer().equals("WHITE") ? "SCHWARZ" : "WEIß"; gui.displayMessage(winner + " gewinnt durch Aufgabe!"); - engine.getWhiteTimer().stop(); - engine.getBlackTimer().stop(); + if (engine.getWhiteTimer() != null) engine.getWhiteTimer().stop(); + if (engine.getBlackTimer() != null) engine.getBlackTimer().stop(); askForRestart(); } }); - // --- PATT-/REMIS-BUTTON --- gui.getDrawButton().addActionListener(e -> { if (gameOver) return; - int answer = javax.swing.JOptionPane.showConfirmDialog( null, "Remis anbieten? (Das Spiel endet sofort unentschieden)", @@ -156,8 +165,8 @@ public class GameController { if (answer == javax.swing.JOptionPane.YES_OPTION) { gameOver = true; gui.displayMessage("Remis! (durch Einigung)"); - engine.getWhiteTimer().stop(); - engine.getBlackTimer().stop(); + if (engine.getWhiteTimer() != null) engine.getWhiteTimer().stop(); + if (engine.getBlackTimer() != null) engine.getBlackTimer().stop(); askForRestart(); } }); @@ -279,6 +288,8 @@ public class GameController { updateGuiBoard(); gui.updateMoveList(engine.getMoveListStringsGrouped()); + + // ---- HIER ist die Matt/Patt/Remis-Prüfung ---- if (engine.isMated()) { @@ -305,12 +316,25 @@ public class GameController { } private void switchTimers() { + GameMode mode = engine.getGameMode(); + Timer whiteTimer = engine.getWhiteTimer(); + Timer blackTimer = engine.getBlackTimer(); + + // Wenn KEIN Modus (also kein Timer): NICHTS machen! + if (mode == null || whiteTimer == null || blackTimer == null) return; + if (engine.getCurrentPlayer().equals("WHITE")) { - engine.getBlackTimer().stop(); - engine.getWhiteTimer().start(); + if (mode.incrementSeconds > 0) { + blackTimer.addSeconds(mode.incrementSeconds); + } + blackTimer.stop(); + whiteTimer.start(); } else { - engine.getWhiteTimer().stop(); - engine.getBlackTimer().start(); + if (mode.incrementSeconds > 0) { + whiteTimer.addSeconds(mode.incrementSeconds); + } + whiteTimer.stop(); + blackTimer.start(); } } 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 dfd1c1c..7335839 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 @@ -5,6 +5,7 @@ import de.hs_mannheim.informatik.chess.view.PgnGui; import de.hs_mannheim.informatik.chess.view.PgnSelectionGui; import de.hs_mannheim.informatik.chess.view.CreativeGui; import de.hs_mannheim.informatik.chess.view.GameGui; +import de.hs_mannheim.informatik.chess.view.GameModeSelector; import java.io.IOException; import java.util.List; @@ -15,6 +16,7 @@ import javax.swing.JOptionPane; import com.github.bhlangonijr.chesslib.game.Game; import de.hs_mannheim.informatik.chess.model.ChessEngine; +import de.hs_mannheim.informatik.chess.model.GameMode; public class MainController { private MainGui mainGui; @@ -28,9 +30,11 @@ public class MainController { } private void startNormalMode() { + + GameMode mode = GameModeSelector.selectGameMode(); mainGui.close(); GameGui gameGui = new GameGui(); - ChessEngine engine = new ChessEngine(); + ChessEngine engine = new ChessEngine(mode); GameEndCallback callback = new GameEndCallback() { public void onNewGameRequested() { startNormalMode(); @@ -39,7 +43,7 @@ public class MainController { new MainController(); } }; - new GameController(gameGui, engine, callback); + new GameController(gameGui, engine, callback,mode); } private void startCreativeMode() { @@ -49,21 +53,22 @@ public class MainController { new CreativeController(creativegui, engine); creativegui.setStartGameCallback(fen -> { + // 1. Modus-Auswahl-Dialog zeigen! + GameMode mode = GameModeSelector.selectGameMode(); // (Dialog, wie beim Normal Mode) - ChessEngine newEngine = new ChessEngine(); + if (mode == null) return; // User hat abgebrochen → nichts machen + + + ChessEngine newEngine = new ChessEngine(mode); // Engine mit Modus (Timer) newEngine.setPositionFromFEN(fen); GameGui gameGui = new GameGui(); - GameEndCallback callback = new GameEndCallback() { - public void onNewGameRequested() { - startCreativeMode(); - } - public void onReturnToMenu() { - new MainController(); - } + public void onNewGameRequested() { startCreativeMode(); } + public void onReturnToMenu() { new MainController(); } }; - new GameController(gameGui, newEngine, callback); + new GameController(gameGui, newEngine, callback, mode); // mit Timer/ohne je nach Modus + creativegui.close(); }); } @@ -86,5 +91,6 @@ public class MainController { JOptionPane.showMessageDialog(null, "Fehler beim Laden der PGN-Datei:\n" + ex.getMessage()); } } + mainGui.close(); } } \ No newline at end of file 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 19441d6..6a26896 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 @@ -56,6 +56,10 @@ public class Timer { public int getSecondsLeft() { return secondsLeft; } + + public void addSeconds(int seconds) { + this.secondsLeft += seconds; + } } diff --git a/schach/src/main/java/de/hs_mannheim/informatik/chess/view/PgnGui.java b/schach/src/main/java/de/hs_mannheim/informatik/chess/view/PgnGui.java index 11e5840..ef67da5 100644 --- a/schach/src/main/java/de/hs_mannheim/informatik/chess/view/PgnGui.java +++ b/schach/src/main/java/de/hs_mannheim/informatik/chess/view/PgnGui.java @@ -58,6 +58,12 @@ public class PgnGui { frame.setDefaultCloseOperation(2); frame.setVisible(true); + frame.addWindowListener(new java.awt.event.WindowAdapter() { + @Override + public void windowClosed(java.awt.event.WindowEvent e) { + new de.hs_mannheim.informatik.chess.controller.MainController(); + } + }); return frame; }