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.PieceDTO;
|
||||||
import de.hs_mannheim.informatik.chess.model.BoardDTO;
|
import de.hs_mannheim.informatik.chess.model.BoardDTO;
|
||||||
import de.hs_mannheim.informatik.chess.view.GameGui;
|
import de.hs_mannheim.informatik.chess.view.GameGui;
|
||||||
|
import de.hs_mannheim.informatik.chess.view.MainGui;
|
||||||
|
|
||||||
public class GameController {
|
public class GameController {
|
||||||
GameGui gui;
|
GameGui gui;
|
||||||
ChessEngine engine;
|
ChessEngine engine;
|
||||||
|
GameEndCallback callback;
|
||||||
|
|
||||||
|
private boolean gameOver = false;
|
||||||
private int selectedRow = -1, selectedCol = -1;
|
private int selectedRow = -1, selectedCol = -1;
|
||||||
private List<int[]> highlightedFields = new ArrayList<>();
|
private List<int[]> highlightedFields = new ArrayList<>();
|
||||||
|
|
||||||
public GameController(GameGui gui, ChessEngine engine) {
|
public GameController(GameGui gui, ChessEngine engine, GameEndCallback callback) {
|
||||||
this.gui = gui;
|
this.gui = gui;
|
||||||
this.engine = engine;
|
this.engine = engine;
|
||||||
|
this.callback = callback;
|
||||||
engine.initTimers(3, 0);
|
engine.initTimers(3, 0);
|
||||||
time();
|
time();
|
||||||
initListeners();
|
initListeners();
|
||||||
|
@ -101,6 +106,9 @@ public class GameController {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleClick(int guiRow, int guiCol) {
|
private void handleClick(int guiRow, int guiCol) {
|
||||||
|
|
||||||
|
if (gameOver) return;
|
||||||
|
|
||||||
int modelRow = flipRow(guiRow);
|
int modelRow = flipRow(guiRow);
|
||||||
int modelCol = flipCol(guiCol);
|
int modelCol = flipCol(guiCol);
|
||||||
|
|
||||||
|
@ -146,6 +154,9 @@ public class GameController {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleMove(MoveDTO move) {
|
private void handleMove(MoveDTO move) {
|
||||||
|
|
||||||
|
if (gameOver) return;
|
||||||
|
|
||||||
BoardDTO boardDTO = engine.getBoardAsDTO();
|
BoardDTO boardDTO = engine.getBoardAsDTO();
|
||||||
PieceDTO piece = boardDTO.getBoard()[move.getFromRow()][move.getFromCol()];
|
PieceDTO piece = boardDTO.getBoard()[move.getFromRow()][move.getFromCol()];
|
||||||
boolean isPawn = piece != null && piece.getType().equals("PAWN");
|
boolean isPawn = piece != null && piece.getType().equals("PAWN");
|
||||||
|
@ -177,8 +188,12 @@ public class GameController {
|
||||||
if (engine.isMated()) {
|
if (engine.isMated()) {
|
||||||
String winner = engine.getCurrentPlayer().equals("WHITE") ? "SCHWARZ" : "WEIß";
|
String winner = engine.getCurrentPlayer().equals("WHITE") ? "SCHWARZ" : "WEIß";
|
||||||
gui.displayMessage(winner + " hat gewonnen (Schachmatt)!");
|
gui.displayMessage(winner + " hat gewonnen (Schachmatt)!");
|
||||||
|
gameOver = true;
|
||||||
|
askForRestart();
|
||||||
} else if (engine.isStalemate() || engine.isDraw()) {
|
} else if (engine.isStalemate() || engine.isDraw()) {
|
||||||
gui.displayMessage("Remis! (Stalemate oder andere Regel)");
|
gui.displayMessage("Remis! (Stalemate oder andere Regel)");
|
||||||
|
gameOver = true;
|
||||||
|
askForRestart();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!engine.isMated() && !engine.isStalemate() && !engine.isDraw()) {
|
if (!engine.isMated() && !engine.isStalemate() && !engine.isDraw()) {
|
||||||
|
@ -213,12 +228,30 @@ public class GameController {
|
||||||
|
|
||||||
// Timeout-Methode
|
// Timeout-Methode
|
||||||
private void onTimeout(String color) {
|
private void onTimeout(String color) {
|
||||||
|
if (gameOver) return; // Doppelt hält besser
|
||||||
|
gameOver = true;
|
||||||
String winner = color.equals("WHITE") ? "SCHWARZ" : "WEIß";
|
String winner = color.equals("WHITE") ? "SCHWARZ" : "WEIß";
|
||||||
gui.displayMessage(winner + " hat durch Zeit gewonnen!");
|
gui.displayMessage(winner + " hat durch Zeit gewonnen!");
|
||||||
engine.getWhiteTimer().stop();
|
engine.getWhiteTimer().stop();
|
||||||
engine.getBlackTimer().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) {
|
private void resetFieldBackground(int row, int col) {
|
||||||
Color LIGHT = new Color(0xe0e1dd);
|
Color LIGHT = new Color(0xe0e1dd);
|
||||||
Color DARK = new Color(0x778da9);
|
Color DARK = new Color(0x778da9);
|
||||||
|
|
|
@ -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();
|
mainGui.close();
|
||||||
GameGui gameGui = new GameGui();
|
GameGui gameGui = new GameGui();
|
||||||
ChessEngine engine = new ChessEngine();
|
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() {
|
private void startCreativeMode() {
|
||||||
|
@ -40,7 +48,7 @@ public class MainController {
|
||||||
ChessEngine engine = new ChessEngine();
|
ChessEngine engine = new ChessEngine();
|
||||||
new CreativeController(creativegui, engine);
|
new CreativeController(creativegui, engine);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startLoadGameMode() {
|
private void startLoadGameMode() {
|
||||||
JFileChooser chooser = new JFileChooser();
|
JFileChooser chooser = new JFileChooser();
|
||||||
int result = chooser.showOpenDialog(null);
|
int result = chooser.showOpenDialog(null);
|
||||||
|
|
Loading…
Reference in New Issue