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 7f6b602..82843d7 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 @@ -13,16 +13,21 @@ import de.hs_mannheim.informatik.chess.model.MoveDTO; import de.hs_mannheim.informatik.chess.model.PieceDTO; 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; ChessEngine engine; + GameEndCallback callback; + + private boolean gameOver = false; private int selectedRow = -1, selectedCol = -1; private List highlightedFields = new ArrayList<>(); - public GameController(GameGui gui, ChessEngine engine) { + public GameController(GameGui gui, ChessEngine engine, GameEndCallback callback) { this.gui = gui; this.engine = engine; + this.callback = callback; engine.initTimers(3, 0); time(); initListeners(); @@ -101,6 +106,9 @@ public class GameController { } private void handleClick(int guiRow, int guiCol) { + + if (gameOver) return; + int modelRow = flipRow(guiRow); int modelCol = flipCol(guiCol); @@ -146,6 +154,9 @@ public class GameController { } private void handleMove(MoveDTO move) { + + if (gameOver) return; + BoardDTO boardDTO = engine.getBoardAsDTO(); PieceDTO piece = boardDTO.getBoard()[move.getFromRow()][move.getFromCol()]; boolean isPawn = piece != null && piece.getType().equals("PAWN"); @@ -177,8 +188,12 @@ public class GameController { if (engine.isMated()) { String winner = engine.getCurrentPlayer().equals("WHITE") ? "SCHWARZ" : "WEIß"; gui.displayMessage(winner + " hat gewonnen (Schachmatt)!"); + gameOver = true; + askForRestart(); } else if (engine.isStalemate() || engine.isDraw()) { gui.displayMessage("Remis! (Stalemate oder andere Regel)"); + gameOver = true; + askForRestart(); } if (!engine.isMated() && !engine.isStalemate() && !engine.isDraw()) { @@ -213,12 +228,30 @@ public class GameController { // Timeout-Methode private void onTimeout(String color) { + if (gameOver) return; // Doppelt hält besser + gameOver = true; String winner = color.equals("WHITE") ? "SCHWARZ" : "WEIß"; gui.displayMessage(winner + " hat durch Zeit gewonnen!"); engine.getWhiteTimer().stop(); engine.getBlackTimer().stop(); + askForRestart(); } - + + private void askForRestart() { + int answer = javax.swing.JOptionPane.showConfirmDialog( + null, + "Neue Partie starten?", + "Spiel beendet", + javax.swing.JOptionPane.YES_NO_OPTION + ); + javax.swing.SwingUtilities.getWindowAncestor(gui.getField(0, 0)).dispose(); + if (answer == javax.swing.JOptionPane.YES_OPTION) { + callback.onNewGameRequested(); + } else { + callback.onReturnToMenu(); + } + } + private void resetFieldBackground(int row, int col) { Color LIGHT = new Color(0xe0e1dd); Color DARK = new Color(0x778da9); diff --git a/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/GameEndCallback.java b/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/GameEndCallback.java new file mode 100644 index 0000000..daed890 --- /dev/null +++ b/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/GameEndCallback.java @@ -0,0 +1,6 @@ +package de.hs_mannheim.informatik.chess.controller; + +public interface GameEndCallback { + void onNewGameRequested(); + void onReturnToMenu(); +} 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 16110d7..5607a08 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 @@ -31,7 +31,15 @@ public class MainController { mainGui.close(); GameGui gameGui = new GameGui(); ChessEngine engine = new ChessEngine(); - new GameController(gameGui, engine); + GameEndCallback callback = new GameEndCallback() { + public void onNewGameRequested() { + startNormalMode(); + } + public void onReturnToMenu() { + new MainController(); + } + }; + new GameController(gameGui, engine, callback); } private void startCreativeMode() { @@ -40,7 +48,7 @@ public class MainController { ChessEngine engine = new ChessEngine(); new CreativeController(creativegui, engine); } - + private void startLoadGameMode() { JFileChooser chooser = new JFileChooser(); int result = chooser.showOpenDialog(null);