New Callback class to open new GameGui after a chess game
parent
268f5e2398
commit
cdc418afb4
|
@ -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<int[]> 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,10 +228,28 @@ 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) {
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
package de.hs_mannheim.informatik.chess.controller;
|
||||
|
||||
public interface GameEndCallback {
|
||||
void onNewGameRequested();
|
||||
void onReturnToMenu();
|
||||
}
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in New Issue