Compare commits
3 Commits
Author | SHA1 | Date |
---|---|---|
|
5d45348c03 | |
|
94bb556288 | |
|
ccbdf1c559 |
|
@ -20,13 +20,19 @@ public class Controller {
|
|||
private int selectedRow = -1, selectedCol = -1;
|
||||
private List<int[]> highlightedFields = new ArrayList<>();
|
||||
|
||||
private boolean gameOver = false;
|
||||
|
||||
|
||||
|
||||
public Controller(GameGui gui, ChessEngine engine) {
|
||||
this.gui = gui;
|
||||
this.engine = engine;
|
||||
this.engine.setTimeoutListener(this::handleTimeout); // Lamda-Verknüpfung
|
||||
initListeners();
|
||||
updateGuiBoard();
|
||||
}
|
||||
|
||||
|
||||
private int flipRow(int row) {
|
||||
return gui.isFlipped() ? 7 - row : row;
|
||||
}
|
||||
|
@ -96,6 +102,8 @@ public class Controller {
|
|||
int modelRow = flipRow(guiRow);
|
||||
int modelCol = flipCol(guiCol);
|
||||
|
||||
if (gameOver) return;
|
||||
|
||||
//Figur am Feld
|
||||
BoardDTO boardDTO = engine.getBoardAsDTO();
|
||||
PieceDTO piece = boardDTO.getBoard()[modelRow][modelCol];
|
||||
|
@ -141,6 +149,10 @@ public class Controller {
|
|||
if (engine.move(move)) {
|
||||
updateGuiBoard();
|
||||
|
||||
|
||||
String currentPlayer = engine.getCurrentPlayer();
|
||||
|
||||
|
||||
//Züge in der MoveList aktualisieren
|
||||
gui.updateMoveList(engine.getMoveListStringsGrouped());
|
||||
|
||||
|
@ -159,6 +171,7 @@ public class Controller {
|
|||
public void updateGuiBoard() {
|
||||
BoardDTO board = engine.getBoardAsDTO();
|
||||
gui.updateBoard(board);
|
||||
|
||||
}
|
||||
|
||||
// Hilfsmethode, um von Koordinaten (row/col) auf z.B. "E2" zu kommen
|
||||
|
@ -176,4 +189,18 @@ public class Controller {
|
|||
gui.getField(row, col).setBackground(new Color(0xe0e1dd));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//Time is Up Methode um zu steuern was Passiert wenn einer der Spieler die Zeit überschritten hat
|
||||
private void handleTimeout(String player) {
|
||||
if (gameOver) return;
|
||||
|
||||
gameOver = true;
|
||||
|
||||
String winner = player.equals("WHITE") ? "SCHWARZ" : "WEIß";
|
||||
gui.displayMessage(winner + " hat gewonnen (Zeitüberschreitung)!");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
package de.hs_mannheim.informatik.chess.controller;
|
||||
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import javax.swing.Timer;
|
||||
|
||||
public class CountdownTimer {
|
||||
private Timer timer;
|
||||
private int secondsLeft;
|
||||
private Runnable onTimeout;
|
||||
private Consumer<Integer> onTick; // <- neue Callback-Funktion
|
||||
|
||||
public CountdownTimer(int minutes, int seconds) {
|
||||
this.secondsLeft = minutes * 60 + seconds;
|
||||
}
|
||||
|
||||
public void setOnTimeout(Runnable onTimeout) {
|
||||
this.onTimeout = onTimeout;
|
||||
}
|
||||
|
||||
public void setOnTick(Consumer<Integer> onTick) {
|
||||
this.onTick = onTick;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
if (timer != null) timer.stop();
|
||||
timer = new Timer(1000, e -> {
|
||||
secondsLeft--;
|
||||
if (onTick != null) onTick.accept(secondsLeft); // <- tick callback
|
||||
if (secondsLeft <= 0) {
|
||||
stop();
|
||||
if (onTimeout != null) onTimeout.run();
|
||||
}
|
||||
});
|
||||
timer.start();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
if (timer != null) {
|
||||
timer.stop();
|
||||
}
|
||||
}
|
||||
|
||||
public void reset(int minutes, int seconds) {
|
||||
stop();
|
||||
this.secondsLeft = minutes * 60 + seconds;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
package de.hs_mannheim.informatik.chess.main;
|
||||
import de.hs_mannheim.informatik.chess.controller.Controller;
|
||||
import de.hs_mannheim.informatik.chess.controller.CountdownTimer;
|
||||
import de.hs_mannheim.informatik.chess.model.ChessEngine;
|
||||
import de.hs_mannheim.informatik.chess.view.GameGui;
|
||||
import de.hs_mannheim.informatik.chess.view.MainGui;
|
||||
|
@ -8,10 +9,13 @@ import de.hs_mannheim.informatik.chess.view.MainGui;
|
|||
public class Main{
|
||||
|
||||
public static void main( String[] args ){
|
||||
|
||||
|
||||
new MainGui(() -> {
|
||||
//Wenn "Normal Modus" gedrückt wird
|
||||
GameGui gui = new GameGui();
|
||||
ChessEngine engine = new ChessEngine();
|
||||
engine.setGui(gui);
|
||||
new Controller(gui, engine);
|
||||
});
|
||||
|
||||
|
|
|
@ -9,25 +9,58 @@ import java.util.logging.SimpleFormatter;
|
|||
|
||||
import com.github.bhlangonijr.chesslib.Board;
|
||||
import com.github.bhlangonijr.chesslib.Piece;
|
||||
import com.github.bhlangonijr.chesslib.Side;
|
||||
import com.github.bhlangonijr.chesslib.Square;
|
||||
import com.github.bhlangonijr.chesslib.move.Move;
|
||||
|
||||
import de.hs_mannheim.informatik.chess.controller.CountdownTimer;
|
||||
import de.hs_mannheim.informatik.chess.view.*;
|
||||
|
||||
public class ChessEngine {
|
||||
|
||||
public interface TimeoutListener {
|
||||
void onTimeout(String player);
|
||||
}
|
||||
|
||||
private Board board;
|
||||
private List<Move> moves = new ArrayList<>();
|
||||
private static final Logger logger = Logger.getLogger(ChessEngine.class.getName());
|
||||
|
||||
private GameGui gameGui;
|
||||
|
||||
private CountdownTimer whiteTimer = new CountdownTimer(1,0);
|
||||
private CountdownTimer blackTimer = new CountdownTimer(3,0);
|
||||
|
||||
private TimeoutListener timeoutListener;
|
||||
|
||||
|
||||
|
||||
private int currentMoveIndex = 0;
|
||||
|
||||
public ChessEngine() {
|
||||
logging();
|
||||
board = new Board();
|
||||
whiteTimer.start();
|
||||
|
||||
if (timeoutListener != null) {
|
||||
timeoutListener.onTimeout("WHITE"); // oder "BLACK"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void setGui(GameGui gui) {
|
||||
this.gameGui = gui;
|
||||
whiteTimer.setOnTick(secs -> gameGui.updateWhiteTimerLabel(secs));
|
||||
blackTimer.setOnTick(secs -> gameGui.updateBlackTimerLabel(secs));
|
||||
}
|
||||
|
||||
public boolean move(MoveDTO move) {
|
||||
String from = "" + (char)('A' + move.getFromCol()) + (8 - move.getFromRow());
|
||||
String to = "" + (char)('A' + move.getToCol()) + (8 - move.getToRow());
|
||||
Move libMove = new Move(Square.valueOf(from), Square.valueOf(to));
|
||||
|
||||
// switchTimers();
|
||||
if (board.legalMoves().contains(libMove)) {
|
||||
board.doMove(libMove);
|
||||
|
||||
|
@ -39,6 +72,8 @@ public class ChessEngine {
|
|||
moves.add(libMove);
|
||||
currentMoveIndex++;
|
||||
logger.info("Zug erfolgreich durchgeführt: " + libMove);
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
logger.warning("Ungültiger Zug: " + libMove);
|
||||
|
@ -146,30 +181,66 @@ public class ChessEngine {
|
|||
return new PieceDTO(type, color, symbol);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public boolean isMated() {
|
||||
boolean mated = board.isMated();
|
||||
logger.info("isMated() = " + mated);
|
||||
whiteTimer.stop();
|
||||
blackTimer.stop();
|
||||
return mated;
|
||||
}
|
||||
|
||||
public boolean isStalemate() {
|
||||
boolean stale = board.isStaleMate();
|
||||
logger.info("isStalemate() = " + stale);
|
||||
whiteTimer.stop();
|
||||
blackTimer.stop();
|
||||
return stale;
|
||||
}
|
||||
|
||||
public boolean isDraw() {
|
||||
boolean draw = board.isDraw();
|
||||
logger.info("isDraw() = " + draw);
|
||||
whiteTimer.stop();
|
||||
blackTimer.stop();
|
||||
return draw;
|
||||
}
|
||||
|
||||
public String getCurrentPlayer() {
|
||||
String player = board.getSideToMove().toString();
|
||||
logger.info("Am Zug: " + player);
|
||||
switchTimers(player);
|
||||
return player;
|
||||
}
|
||||
|
||||
public void onTimeout(String player) {
|
||||
String winner = player.equals("WHITE") ? "SCHWARZ" : "WEIß";
|
||||
if (gameGui != null) {
|
||||
gameGui.displayMessage(winner + " hat gewonnen (Zeitüberschreitung)!");
|
||||
} else {
|
||||
System.err.println("Fehler: GameGui ist null beim Timeout von " + player);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private void switchTimers(String player) {
|
||||
|
||||
if (player.equals("WHITE")) {
|
||||
blackTimer.stop();
|
||||
whiteTimer.start();
|
||||
} else {
|
||||
whiteTimer.stop();
|
||||
blackTimer.start();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void logging() {
|
||||
|
||||
// Eigener Handler nur für diese Klasse
|
||||
|
@ -187,4 +258,23 @@ public class ChessEngine {
|
|||
logger.info("ChessEngine wurde initialisiert.");
|
||||
}
|
||||
|
||||
public void setTimeoutListener(TimeoutListener listener) {
|
||||
this.timeoutListener = listener;
|
||||
|
||||
whiteTimer.setOnTimeout(() -> {
|
||||
if (timeoutListener != null) timeoutListener.onTimeout("WHITE");
|
||||
});
|
||||
|
||||
blackTimer.setOnTimeout(() -> {
|
||||
if (timeoutListener != null) timeoutListener.onTimeout("BLACK");
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -27,6 +27,10 @@ public class GameGui {
|
|||
private JLabel[][] fields = new JLabel[8][8];
|
||||
private JButton flipBoardButton;
|
||||
private boolean isFlipped = false;
|
||||
private JLabel whiteTimerLabel;
|
||||
private JLabel blackTimerLabel;
|
||||
|
||||
|
||||
|
||||
JButton btnFirst = new JButton("|<");
|
||||
JButton btnPrev = new JButton("<");
|
||||
|
@ -187,6 +191,31 @@ public class GameGui {
|
|||
// Unten ins BorderLayout
|
||||
statsPanel.add(buttonPanel, BorderLayout.SOUTH);
|
||||
|
||||
// Timer-Labels
|
||||
whiteTimerLabel = new JLabel("Weiß: 03:00", SwingConstants.CENTER);
|
||||
blackTimerLabel = new JLabel("Schwarz: 03:00", SwingConstants.CENTER);
|
||||
|
||||
// Styling
|
||||
whiteTimerLabel.setFont(new Font("SansSerif", Font.BOLD, 24));
|
||||
whiteTimerLabel.setForeground(Color.WHITE);
|
||||
blackTimerLabel.setFont(new Font("SansSerif", Font.BOLD, 24));
|
||||
blackTimerLabel.setForeground(Color.WHITE);
|
||||
|
||||
// Optional: Hintergrundfarbe für Sichtbarkeit
|
||||
whiteTimerLabel.setBackground(new Color(0x1b263b));
|
||||
blackTimerLabel.setBackground(new Color(0x1b263b));
|
||||
whiteTimerLabel.setOpaque(true);
|
||||
blackTimerLabel.setOpaque(true);
|
||||
|
||||
// Panel oben im statsPanel
|
||||
JPanel timerPanel = new JPanel(new GridLayout(2, 1));
|
||||
timerPanel.setBackground(new Color(0x0d1b2a));
|
||||
timerPanel.add(whiteTimerLabel);
|
||||
timerPanel.add(blackTimerLabel);
|
||||
|
||||
statsPanel.add(timerPanel, BorderLayout.NORTH);
|
||||
|
||||
|
||||
return statsPanel;
|
||||
}
|
||||
|
||||
|
@ -249,4 +278,20 @@ public class GameGui {
|
|||
JOptionPane.showMessageDialog(null, msg);
|
||||
}
|
||||
|
||||
|
||||
public void updateWhiteTimerLabel(int secondsLeft) {
|
||||
whiteTimerLabel.setText("Weiß: " + formatTime(secondsLeft));
|
||||
}
|
||||
|
||||
public void updateBlackTimerLabel(int secondsLeft) {
|
||||
blackTimerLabel.setText("Schwarz: " + formatTime(secondsLeft));
|
||||
}
|
||||
|
||||
private String formatTime(int seconds) {
|
||||
int min = seconds / 60;
|
||||
int sec = seconds % 60;
|
||||
return String.format("%02d:%02d", min, sec);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue