Countdown fertig implementiert und wird in der GUI angezeitg, Wechsel

zwischen dan spielenr ist möglich.
Timer
thomasmuller 2025-06-23 15:56:13 +02:00
parent 3e1a770176
commit ccbdf1c559
4 changed files with 127 additions and 2 deletions

View File

@ -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
}

View File

@ -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);
});
}
}

View File

@ -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<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 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.");
}
}

View File

@ -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);
}
}