Switch Controller to use DTOs for board updates
parent
9adb0db66b
commit
ed573d3338
|
|
@ -9,6 +9,9 @@ import java.util.List;
|
||||||
import javax.swing.BorderFactory;
|
import javax.swing.BorderFactory;
|
||||||
|
|
||||||
import de.hs_mannheim.informatik.chess.model.ChessEngine;
|
import de.hs_mannheim.informatik.chess.model.ChessEngine;
|
||||||
|
import de.hs_mannheim.informatik.chess.model.MoveDTO;
|
||||||
|
import de.hs_mannheim.informatik.chess.model.BoardDTO;
|
||||||
|
import de.hs_mannheim.informatik.chess.model.PieceDTO;
|
||||||
import de.hs_mannheim.informatik.chess.view.Gui;
|
import de.hs_mannheim.informatik.chess.view.Gui;
|
||||||
|
|
||||||
public class Controller {
|
public class Controller {
|
||||||
|
|
@ -44,13 +47,14 @@ public class Controller {
|
||||||
selectedCol = col;
|
selectedCol = col;
|
||||||
gui.getField(row, col).setBorder(BorderFactory.createLineBorder(Color.RED, 2));
|
gui.getField(row, col).setBorder(BorderFactory.createLineBorder(Color.RED, 2));
|
||||||
|
|
||||||
//Mögliche Ziele highlighten
|
// Mögliche Ziele highlighten (DTOs!)
|
||||||
String from = coordToChessNotation(row, col);
|
String fromSquare = coordToChessNotation(row, col); // z.B. "E2"
|
||||||
List<String> targets = engine.getLegalDestinations(from);
|
List<MoveDTO> moves = engine.getLegalDestinations(fromSquare);
|
||||||
for (String t : targets) {
|
for (MoveDTO move : moves) {
|
||||||
int[] xy = chessNotationToCoord(t);
|
int toRow = move.getToRow();
|
||||||
gui.getField(xy[0], xy[1]).setBackground(Color.YELLOW);
|
int toCol = move.getToCol();
|
||||||
highlightedFields.add(xy);
|
gui.getField(toRow, toCol).setBackground(Color.YELLOW);
|
||||||
|
highlightedFields.add(new int[]{toRow, toCol});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Zweites Feld: Zug probieren
|
// Zweites Feld: Zug probieren
|
||||||
|
|
@ -61,23 +65,24 @@ public class Controller {
|
||||||
highlightedFields.clear();
|
highlightedFields.clear();
|
||||||
|
|
||||||
gui.getField(selectedRow, selectedCol).setBorder(null);
|
gui.getField(selectedRow, selectedCol).setBorder(null);
|
||||||
handleMove(selectedRow, selectedCol, row, col);
|
|
||||||
|
// **Neues MoveDTO statt String-Kram!**
|
||||||
|
MoveDTO move = new MoveDTO(selectedRow, selectedCol, row, col);
|
||||||
|
handleMove(move);
|
||||||
|
|
||||||
selectedRow = -1;
|
selectedRow = -1;
|
||||||
selectedCol = -1;
|
selectedCol = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handleMove(int fromX, int fromY, int toX, int toY) {
|
public void handleMove(MoveDTO move) {
|
||||||
String from = coordToChessNotation(fromX, fromY);
|
if (engine.move(move)) {
|
||||||
String to = coordToChessNotation(toX, toY);
|
|
||||||
if (engine.move(from, to)) {
|
|
||||||
updateGuiBoard();
|
updateGuiBoard();
|
||||||
|
|
||||||
// Jetzt: Spielstatus prüfen!
|
// Jetzt: Spielstatus prüfen!
|
||||||
if (engine.isMated()) {
|
if (engine.isMated()) {
|
||||||
String winner = engine.getCurrentPlayer().equals("WHITE") ? "SCHWARZ" : "WEIß";
|
String winner = engine.getCurrentPlayer().equals("WHITE") ? "SCHWARZ" : "WEIß";
|
||||||
gui.displayMessage(winner + " hat gewonnen (Schachmatt)!");
|
gui.displayMessage(winner + " hat gewonnen (Schachmatt)!");
|
||||||
// Optional: Hier Spiel beenden/disable oder restart anbieten
|
|
||||||
} else if (engine.isStalemate() || engine.isDraw()) {
|
} else if (engine.isStalemate() || engine.isDraw()) {
|
||||||
gui.displayMessage("Remis! (Stalemate oder andere Regel)");
|
gui.displayMessage("Remis! (Stalemate oder andere Regel)");
|
||||||
}
|
}
|
||||||
|
|
@ -87,25 +92,18 @@ public class Controller {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateGuiBoard() {
|
public void updateGuiBoard() {
|
||||||
gui.updateBoard(engine.getBoardUnicode());
|
BoardDTO board = engine.getBoardAsDTO();
|
||||||
|
gui.updateBoard(board); // Passe die GUI an, damit sie ein BoardDTO nimmt!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Hilfsmethode, um von Koordinaten (row/col) auf z.B. "E2" zu kommen
|
||||||
private String coordToChessNotation(int x, int y) {
|
private String coordToChessNotation(int x, int y) {
|
||||||
// (0,0) -> "A8", (7,7) -> "H1"
|
char file = (char)('A' + y);
|
||||||
char file = (char)('A' + y); // ACHTUNG! col == y == FILE, row == x == RANK
|
|
||||||
int rank = 8 - x;
|
int rank = 8 - x;
|
||||||
return "" + file + rank;
|
return "" + file + rank;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int[] chessNotationToCoord(String square) {
|
// ... resetFieldBackground wie gehabt ...
|
||||||
// "A8" -> (0,0), "H1" -> (7,7)
|
|
||||||
char file = square.charAt(0);
|
|
||||||
int rank = square.charAt(1) - '0';
|
|
||||||
int x = 8 - rank;
|
|
||||||
int y = file - 'A';
|
|
||||||
return new int[] {x, y};
|
|
||||||
}
|
|
||||||
|
|
||||||
private void resetFieldBackground(int row, int col) {
|
private void resetFieldBackground(int row, int col) {
|
||||||
if ((row + col) % 2 == 0) {
|
if ((row + col) % 2 == 0) {
|
||||||
gui.getField(row, col).setBackground(new Color(0x778da9));
|
gui.getField(row, col).setBackground(new Color(0x778da9));
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue