Countdown fertig implementiert und wird in der GUI angezeitg, Wechsel
zwischen dan spielenr ist möglich.Timer
parent
3e1a770176
commit
ccbdf1c559
|
|
@ -141,6 +141,10 @@ public class Controller {
|
||||||
if (engine.move(move)) {
|
if (engine.move(move)) {
|
||||||
updateGuiBoard();
|
updateGuiBoard();
|
||||||
|
|
||||||
|
|
||||||
|
String currentPlayer = engine.getCurrentPlayer();
|
||||||
|
|
||||||
|
|
||||||
//Züge in der MoveList aktualisieren
|
//Züge in der MoveList aktualisieren
|
||||||
gui.updateMoveList(engine.getMoveListStringsGrouped());
|
gui.updateMoveList(engine.getMoveListStringsGrouped());
|
||||||
|
|
||||||
|
|
@ -159,6 +163,7 @@ public class Controller {
|
||||||
public void updateGuiBoard() {
|
public void updateGuiBoard() {
|
||||||
BoardDTO board = engine.getBoardAsDTO();
|
BoardDTO board = engine.getBoardAsDTO();
|
||||||
gui.updateBoard(board);
|
gui.updateBoard(board);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hilfsmethode, um von Koordinaten (row/col) auf z.B. "E2" zu kommen
|
// 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));
|
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
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package de.hs_mannheim.informatik.chess.main;
|
package de.hs_mannheim.informatik.chess.main;
|
||||||
import de.hs_mannheim.informatik.chess.controller.Controller;
|
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.model.ChessEngine;
|
||||||
import de.hs_mannheim.informatik.chess.view.GameGui;
|
import de.hs_mannheim.informatik.chess.view.GameGui;
|
||||||
import de.hs_mannheim.informatik.chess.view.MainGui;
|
import de.hs_mannheim.informatik.chess.view.MainGui;
|
||||||
|
|
@ -8,10 +9,13 @@ import de.hs_mannheim.informatik.chess.view.MainGui;
|
||||||
public class Main{
|
public class Main{
|
||||||
|
|
||||||
public static void main( String[] args ){
|
public static void main( String[] args ){
|
||||||
|
|
||||||
|
|
||||||
new MainGui(() -> {
|
new MainGui(() -> {
|
||||||
//Wenn "Normal Modus" gedrückt wird
|
//Wenn "Normal Modus" gedrückt wird
|
||||||
GameGui gui = new GameGui();
|
GameGui gui = new GameGui();
|
||||||
ChessEngine engine = new ChessEngine();
|
ChessEngine engine = new ChessEngine();
|
||||||
|
engine.setGui(gui);
|
||||||
new Controller(gui, engine);
|
new Controller(gui, engine);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,25 +9,46 @@ import java.util.logging.SimpleFormatter;
|
||||||
|
|
||||||
import com.github.bhlangonijr.chesslib.Board;
|
import com.github.bhlangonijr.chesslib.Board;
|
||||||
import com.github.bhlangonijr.chesslib.Piece;
|
import com.github.bhlangonijr.chesslib.Piece;
|
||||||
|
import com.github.bhlangonijr.chesslib.Side;
|
||||||
import com.github.bhlangonijr.chesslib.Square;
|
import com.github.bhlangonijr.chesslib.Square;
|
||||||
import com.github.bhlangonijr.chesslib.move.Move;
|
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 class ChessEngine {
|
||||||
private Board board;
|
private Board board;
|
||||||
private List<Move> moves = new ArrayList<>();
|
private List<Move> moves = new ArrayList<>();
|
||||||
private static final Logger logger = Logger.getLogger(ChessEngine.class.getName());
|
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;
|
private int currentMoveIndex = 0;
|
||||||
|
|
||||||
public ChessEngine() {
|
public ChessEngine() {
|
||||||
logging();
|
logging();
|
||||||
board = new Board();
|
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) {
|
public boolean move(MoveDTO move) {
|
||||||
String from = "" + (char)('A' + move.getFromCol()) + (8 - move.getFromRow());
|
String from = "" + (char)('A' + move.getFromCol()) + (8 - move.getFromRow());
|
||||||
String to = "" + (char)('A' + move.getToCol()) + (8 - move.getToRow());
|
String to = "" + (char)('A' + move.getToCol()) + (8 - move.getToRow());
|
||||||
Move libMove = new Move(Square.valueOf(from), Square.valueOf(to));
|
Move libMove = new Move(Square.valueOf(from), Square.valueOf(to));
|
||||||
|
|
||||||
|
// switchTimers();
|
||||||
if (board.legalMoves().contains(libMove)) {
|
if (board.legalMoves().contains(libMove)) {
|
||||||
board.doMove(libMove);
|
board.doMove(libMove);
|
||||||
|
|
||||||
|
|
@ -39,6 +60,8 @@ public class ChessEngine {
|
||||||
moves.add(libMove);
|
moves.add(libMove);
|
||||||
currentMoveIndex++;
|
currentMoveIndex++;
|
||||||
logger.info("Zug erfolgreich durchgeführt: " + libMove);
|
logger.info("Zug erfolgreich durchgeführt: " + libMove);
|
||||||
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
logger.warning("Ungültiger Zug: " + libMove);
|
logger.warning("Ungültiger Zug: " + libMove);
|
||||||
|
|
@ -146,30 +169,69 @@ public class ChessEngine {
|
||||||
return new PieceDTO(type, color, symbol);
|
return new PieceDTO(type, color, symbol);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public boolean isMated() {
|
public boolean isMated() {
|
||||||
boolean mated = board.isMated();
|
boolean mated = board.isMated();
|
||||||
logger.info("isMated() = " + mated);
|
logger.info("isMated() = " + mated);
|
||||||
|
whiteTimer.stop();
|
||||||
|
blackTimer.stop();
|
||||||
return mated;
|
return mated;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isStalemate() {
|
public boolean isStalemate() {
|
||||||
boolean stale = board.isStaleMate();
|
boolean stale = board.isStaleMate();
|
||||||
logger.info("isStalemate() = " + stale);
|
logger.info("isStalemate() = " + stale);
|
||||||
|
whiteTimer.stop();
|
||||||
|
blackTimer.stop();
|
||||||
return stale;
|
return stale;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isDraw() {
|
public boolean isDraw() {
|
||||||
boolean draw = board.isDraw();
|
boolean draw = board.isDraw();
|
||||||
logger.info("isDraw() = " + draw);
|
logger.info("isDraw() = " + draw);
|
||||||
|
whiteTimer.stop();
|
||||||
|
blackTimer.stop();
|
||||||
return draw;
|
return draw;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getCurrentPlayer() {
|
public String getCurrentPlayer() {
|
||||||
String player = board.getSideToMove().toString();
|
String player = board.getSideToMove().toString();
|
||||||
logger.info("Am Zug: " + player);
|
logger.info("Am Zug: " + player);
|
||||||
|
switchTimers(player);
|
||||||
return 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() {
|
public void logging() {
|
||||||
|
|
||||||
// Eigener Handler nur für diese Klasse
|
// Eigener Handler nur für diese Klasse
|
||||||
|
|
@ -187,4 +249,8 @@ public class ChessEngine {
|
||||||
logger.info("ChessEngine wurde initialisiert.");
|
logger.info("ChessEngine wurde initialisiert.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,10 @@ public class GameGui {
|
||||||
private JLabel[][] fields = new JLabel[8][8];
|
private JLabel[][] fields = new JLabel[8][8];
|
||||||
private JButton flipBoardButton;
|
private JButton flipBoardButton;
|
||||||
private boolean isFlipped = false;
|
private boolean isFlipped = false;
|
||||||
|
private JLabel whiteTimerLabel;
|
||||||
|
private JLabel blackTimerLabel;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
JButton btnFirst = new JButton("|<");
|
JButton btnFirst = new JButton("|<");
|
||||||
JButton btnPrev = new JButton("<");
|
JButton btnPrev = new JButton("<");
|
||||||
|
|
@ -187,6 +191,31 @@ public class GameGui {
|
||||||
// Unten ins BorderLayout
|
// Unten ins BorderLayout
|
||||||
statsPanel.add(buttonPanel, BorderLayout.SOUTH);
|
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;
|
return statsPanel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -249,4 +278,20 @@ public class GameGui {
|
||||||
JOptionPane.showMessageDialog(null, msg);
|
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