Reworked some classes to work with game modes
parent
9df9d5bbe9
commit
18c8e73836
|
@ -0,0 +1,42 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="optional" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||
<attributes>
|
||||
<attribute name="test" value="true"/>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
|
||||
<attributes>
|
||||
<attribute name="test" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="optional" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
|
@ -28,24 +28,22 @@ public class GameController {
|
|||
private List<int[]> highlightedFields = new ArrayList<>();
|
||||
private GameMode gameMode;
|
||||
|
||||
public GameController(GameGui gui, ChessEngine engine, GameEndCallback callback) {
|
||||
this.gameMode = null;
|
||||
this.gui = gui;
|
||||
this.engine = engine;
|
||||
this.callback = callback;
|
||||
engine.initTimers(gameMode.minutes, gameMode.incrementSeconds);
|
||||
time();
|
||||
initListeners();
|
||||
updateGuiBoard();
|
||||
}
|
||||
|
||||
public GameController(GameGui gui, ChessEngine engine, GameEndCallback callback, GameMode gameMode) {
|
||||
this.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(gameMode.minutes, gameMode.incrementSeconds);
|
||||
time();
|
||||
this.gameMode = null;
|
||||
// KEINE Timer initialisieren
|
||||
initListeners();
|
||||
updateGuiBoard();
|
||||
}
|
||||
|
@ -140,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?",
|
||||
|
@ -151,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)",
|
||||
|
@ -170,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();
|
||||
}
|
||||
});
|
||||
|
@ -321,26 +316,26 @@ public class GameController {
|
|||
}
|
||||
|
||||
private void switchTimers() {
|
||||
GameMode mode = engine.getGameMode(); // muss Engine speichern
|
||||
Timer whiteTimer = engine.getWhiteTimer();
|
||||
Timer blackTimer = engine.getBlackTimer();
|
||||
GameMode mode = engine.getGameMode();
|
||||
Timer whiteTimer = engine.getWhiteTimer();
|
||||
Timer blackTimer = engine.getBlackTimer();
|
||||
|
||||
if (engine.getCurrentPlayer().equals("WHITE")) {
|
||||
// Der letzte Spieler war Schwarz – ihm ggf. Inkrement geben
|
||||
if (mode.incrementSeconds > 0) {
|
||||
blackTimer.addSeconds(mode.incrementSeconds);
|
||||
}
|
||||
blackTimer.stop();
|
||||
whiteTimer.start();
|
||||
} else {
|
||||
// Der letzte Spieler war Weiß – ihm ggf. Inkrement geben
|
||||
if (mode.incrementSeconds > 0) {
|
||||
whiteTimer.addSeconds(mode.incrementSeconds);
|
||||
}
|
||||
whiteTimer.stop();
|
||||
blackTimer.start();
|
||||
}
|
||||
|
||||
// Wenn KEIN Modus (also kein Timer): NICHTS machen!
|
||||
if (mode == null || whiteTimer == null || blackTimer == null) return;
|
||||
|
||||
if (engine.getCurrentPlayer().equals("WHITE")) {
|
||||
if (mode.incrementSeconds > 0) {
|
||||
blackTimer.addSeconds(mode.incrementSeconds);
|
||||
}
|
||||
blackTimer.stop();
|
||||
whiteTimer.start();
|
||||
} else {
|
||||
if (mode.incrementSeconds > 0) {
|
||||
whiteTimer.addSeconds(mode.incrementSeconds);
|
||||
}
|
||||
whiteTimer.stop();
|
||||
blackTimer.start();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -53,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();
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -90,5 +91,6 @@ public class MainController {
|
|||
JOptionPane.showMessageDialog(null, "Fehler beim Laden der PGN-Datei:\n" + ex.getMessage());
|
||||
}
|
||||
}
|
||||
mainGui.close();
|
||||
}
|
||||
}
|
|
@ -19,6 +19,8 @@ public class CreativeGui {
|
|||
private JButton updateBtn;
|
||||
private JButton flipBoardButton;
|
||||
|
||||
private boolean closedByUser = true;
|
||||
|
||||
private StartGameCallback startGameCallback;
|
||||
|
||||
private JButton startGameButton;
|
||||
|
@ -64,7 +66,16 @@ public class CreativeGui {
|
|||
gbc.insets = new Insets(5, 0, 5, 5);
|
||||
gbc.fill = GridBagConstraints.BOTH;
|
||||
mainPanel.add(fenPanel(), gbc);
|
||||
|
||||
|
||||
frame.addWindowListener(new java.awt.event.WindowAdapter() {
|
||||
@Override
|
||||
public void windowClosed(java.awt.event.WindowEvent e) {
|
||||
if (closedByUser) {
|
||||
new de.hs_mannheim.informatik.chess.controller.MainController();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
frame.setContentPane(mainPanel);
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
@ -207,6 +218,7 @@ public class CreativeGui {
|
|||
|
||||
startGameButton.addActionListener(e -> {
|
||||
if (startGameCallback != null) {
|
||||
setClosedByUser(false);
|
||||
startGameCallback.onStartGame(getFenText());
|
||||
}
|
||||
});
|
||||
|
@ -222,6 +234,9 @@ public class CreativeGui {
|
|||
public JButton getUpdateButton() { return updateBtn; }
|
||||
public void setSelectedPiece(String piece) { selectedPiece = piece; }
|
||||
public String getSelectedPiece() { return selectedPiece; }
|
||||
public void setClosedByUser(boolean b) {
|
||||
this.closedByUser = b;
|
||||
}
|
||||
|
||||
public JButton getStartGameButton() { return startGameButton; }
|
||||
|
||||
|
@ -232,4 +247,8 @@ public class CreativeGui {
|
|||
public boolean isFlipped() { return isFlipped; }
|
||||
public void setFlipped(boolean f) { isFlipped = f; }
|
||||
public JButton getFlipBoardButton() { return flipBoardButton; }
|
||||
|
||||
public void close() {
|
||||
frame.dispose();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue