Compare commits
No commits in common. "5d45348c03b041e4910e6f567da1318eda762289" and "3e1a7701768f84c19cc3a2d7b27f59aa5196d2fe" have entirely different histories.
5d45348c03
...
3e1a770176
|
|
@ -20,19 +20,13 @@ public class Controller {
|
||||||
private int selectedRow = -1, selectedCol = -1;
|
private int selectedRow = -1, selectedCol = -1;
|
||||||
private List<int[]> highlightedFields = new ArrayList<>();
|
private List<int[]> highlightedFields = new ArrayList<>();
|
||||||
|
|
||||||
private boolean gameOver = false;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public Controller(GameGui gui, ChessEngine engine) {
|
public Controller(GameGui gui, ChessEngine engine) {
|
||||||
this.gui = gui;
|
this.gui = gui;
|
||||||
this.engine = engine;
|
this.engine = engine;
|
||||||
this.engine.setTimeoutListener(this::handleTimeout); // Lamda-Verknüpfung
|
|
||||||
initListeners();
|
initListeners();
|
||||||
updateGuiBoard();
|
updateGuiBoard();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private int flipRow(int row) {
|
private int flipRow(int row) {
|
||||||
return gui.isFlipped() ? 7 - row : row;
|
return gui.isFlipped() ? 7 - row : row;
|
||||||
}
|
}
|
||||||
|
|
@ -102,8 +96,6 @@ public class Controller {
|
||||||
int modelRow = flipRow(guiRow);
|
int modelRow = flipRow(guiRow);
|
||||||
int modelCol = flipCol(guiCol);
|
int modelCol = flipCol(guiCol);
|
||||||
|
|
||||||
if (gameOver) return;
|
|
||||||
|
|
||||||
//Figur am Feld
|
//Figur am Feld
|
||||||
BoardDTO boardDTO = engine.getBoardAsDTO();
|
BoardDTO boardDTO = engine.getBoardAsDTO();
|
||||||
PieceDTO piece = boardDTO.getBoard()[modelRow][modelCol];
|
PieceDTO piece = boardDTO.getBoard()[modelRow][modelCol];
|
||||||
|
|
@ -149,10 +141,6 @@ 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());
|
||||||
|
|
||||||
|
|
@ -171,7 +159,6 @@ 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
|
||||||
|
|
@ -189,18 +176,4 @@ 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
|
|
||||||
private void handleTimeout(String player) {
|
|
||||||
if (gameOver) return;
|
|
||||||
|
|
||||||
gameOver = true;
|
|
||||||
|
|
||||||
String winner = player.equals("WHITE") ? "SCHWARZ" : "WEIß";
|
|
||||||
gui.displayMessage(winner + " hat gewonnen (Zeitüberschreitung)!");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,51 +0,0 @@
|
||||||
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,6 +1,5 @@
|
||||||
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;
|
||||||
|
|
@ -9,13 +8,10 @@ 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,58 +9,25 @@ 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 {
|
||||||
|
|
||||||
public interface TimeoutListener {
|
|
||||||
void onTimeout(String player);
|
|
||||||
}
|
|
||||||
|
|
||||||
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 TimeoutListener timeoutListener;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private int currentMoveIndex = 0;
|
private int currentMoveIndex = 0;
|
||||||
|
|
||||||
public ChessEngine() {
|
public ChessEngine() {
|
||||||
logging();
|
logging();
|
||||||
board = new Board();
|
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) {
|
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);
|
||||||
|
|
||||||
|
|
@ -72,8 +39,6 @@ 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);
|
||||||
|
|
@ -181,66 +146,30 @@ 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ß";
|
|
||||||
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() {
|
public void logging() {
|
||||||
|
|
||||||
// Eigener Handler nur für diese Klasse
|
// Eigener Handler nur für diese Klasse
|
||||||
|
|
@ -258,23 +187,4 @@ public class ChessEngine {
|
||||||
logger.info("ChessEngine wurde initialisiert.");
|
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,10 +27,6 @@ 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("<");
|
||||||
|
|
@ -191,31 +187,6 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -278,20 +249,4 @@ 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