From ccbdf1c559610f4b944ed68d09fff57479d6e6c4 Mon Sep 17 00:00:00 2001 From: thomasmuller Date: Mon, 23 Jun 2025 15:56:13 +0200 Subject: [PATCH] =?UTF-8?q?Countdown=20fertig=20implementiert=20und=20wird?= =?UTF-8?q?=20in=20der=20GUI=20angezeitg,=20Wechsel=20zwischen=20dan=20spi?= =?UTF-8?q?elenr=20ist=20m=C3=B6glich.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chess/controller/Controller.java | 10 +++ .../informatik/chess/main/Main.java | 8 ++- .../informatik/chess/model/ChessEngine.java | 66 +++++++++++++++++++ .../informatik/chess/view/GameGui.java | 45 +++++++++++++ 4 files changed, 127 insertions(+), 2 deletions(-) diff --git a/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/Controller.java b/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/Controller.java index 162df94..8e62a78 100644 --- a/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/Controller.java +++ b/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/Controller.java @@ -141,6 +141,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 +163,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 +181,9 @@ 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 + + } diff --git a/schach/src/main/java/de/hs_mannheim/informatik/chess/main/Main.java b/schach/src/main/java/de/hs_mannheim/informatik/chess/main/Main.java index 94d5753..f2c5170 100644 --- a/schach/src/main/java/de/hs_mannheim/informatik/chess/main/Main.java +++ b/schach/src/main/java/de/hs_mannheim/informatik/chess/main/Main.java @@ -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,12 +9,15 @@ 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(); - new Controller(gui, engine); + engine.setGui(gui); + new Controller(gui, engine); }); - + } } diff --git a/schach/src/main/java/de/hs_mannheim/informatik/chess/model/ChessEngine.java b/schach/src/main/java/de/hs_mannheim/informatik/chess/model/ChessEngine.java index e7d3c9f..60c29e3 100644 --- a/schach/src/main/java/de/hs_mannheim/informatik/chess/model/ChessEngine.java +++ b/schach/src/main/java/de/hs_mannheim/informatik/chess/model/ChessEngine.java @@ -9,25 +9,46 @@ 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 { private Board board; private List 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 int currentMoveIndex = 0; public ChessEngine() { logging(); board = new Board(); + whiteTimer.start(); + } + + + 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 +60,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 +169,69 @@ 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ß"; + gameGui.displayMessage(winner + " hat gewonnen (Zeitüberschreitung)!"); + whiteTimer.stop(); + blackTimer.stop(); + } + + + + + 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 +249,8 @@ public class ChessEngine { logger.info("ChessEngine wurde initialisiert."); } + + + + } diff --git a/schach/src/main/java/de/hs_mannheim/informatik/chess/view/GameGui.java b/schach/src/main/java/de/hs_mannheim/informatik/chess/view/GameGui.java index 76f4b97..cfee2f1 100644 --- a/schach/src/main/java/de/hs_mannheim/informatik/chess/view/GameGui.java +++ b/schach/src/main/java/de/hs_mannheim/informatik/chess/view/GameGui.java @@ -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("<"); @@ -186,6 +190,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); + } + + }