Switch Controller to use DTOs for board updates
parent
9adb0db66b
commit
ed573d3338
|
@ -9,22 +9,25 @@ import java.util.List;
|
|||
import javax.swing.BorderFactory;
|
||||
|
||||
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;
|
||||
|
||||
public class Controller {
|
||||
Gui gui;
|
||||
ChessEngine engine;
|
||||
private int selectedRow = -1, selectedCol = -1;
|
||||
private List<int[]> highlightedFields = new ArrayList<>();
|
||||
|
||||
public Controller(Gui gui, ChessEngine engine) {
|
||||
this.gui = gui;
|
||||
this.engine = engine;
|
||||
initListeners();
|
||||
Gui gui;
|
||||
ChessEngine engine;
|
||||
private int selectedRow = -1, selectedCol = -1;
|
||||
private List<int[]> highlightedFields = new ArrayList<>();
|
||||
|
||||
public Controller(Gui gui, ChessEngine engine) {
|
||||
this.gui = gui;
|
||||
this.engine = engine;
|
||||
initListeners();
|
||||
updateGuiBoard();
|
||||
}
|
||||
|
||||
private void initListeners() {
|
||||
}
|
||||
|
||||
private void initListeners() {
|
||||
for (int row = 0; row < 8; row++) {
|
||||
for (int col = 0; col < 8; col++) {
|
||||
final int r = row, c = col;
|
||||
|
@ -36,76 +39,71 @@ public class Controller {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void handleClick(int row, int col) {
|
||||
if (selectedRow == -1 && selectedCol == -1) {
|
||||
// Erstes Feld gewählt
|
||||
selectedRow = row;
|
||||
selectedCol = col;
|
||||
gui.getField(row, col).setBorder(BorderFactory.createLineBorder(Color.RED, 2));
|
||||
|
||||
private void handleClick(int row, int col) {
|
||||
if (selectedRow == -1 && selectedCol == -1) {
|
||||
// Erstes Feld gewählt
|
||||
selectedRow = row;
|
||||
selectedCol = col;
|
||||
gui.getField(row, col).setBorder(BorderFactory.createLineBorder(Color.RED, 2));
|
||||
|
||||
//Mögliche Ziele highlighten
|
||||
String from = coordToChessNotation(row, col);
|
||||
List<String> targets = engine.getLegalDestinations(from);
|
||||
for (String t : targets) {
|
||||
int[] xy = chessNotationToCoord(t);
|
||||
gui.getField(xy[0], xy[1]).setBackground(Color.YELLOW);
|
||||
highlightedFields.add(xy);
|
||||
}
|
||||
} else {
|
||||
// Zweites Feld: Zug probieren
|
||||
// Highlight entfernen
|
||||
for (int[] xy : highlightedFields) {
|
||||
resetFieldBackground(xy[0], xy[1]);
|
||||
}
|
||||
highlightedFields.clear();
|
||||
// Mögliche Ziele highlighten (DTOs!)
|
||||
String fromSquare = coordToChessNotation(row, col); // z.B. "E2"
|
||||
List<MoveDTO> moves = engine.getLegalDestinations(fromSquare);
|
||||
for (MoveDTO move : moves) {
|
||||
int toRow = move.getToRow();
|
||||
int toCol = move.getToCol();
|
||||
gui.getField(toRow, toCol).setBackground(Color.YELLOW);
|
||||
highlightedFields.add(new int[]{toRow, toCol});
|
||||
}
|
||||
} else {
|
||||
// Zweites Feld: Zug probieren
|
||||
// Highlight entfernen
|
||||
for (int[] xy : highlightedFields) {
|
||||
resetFieldBackground(xy[0], xy[1]);
|
||||
}
|
||||
highlightedFields.clear();
|
||||
|
||||
gui.getField(selectedRow, selectedCol).setBorder(null);
|
||||
handleMove(selectedRow, selectedCol, row, col);
|
||||
selectedRow = -1;
|
||||
selectedCol = -1;
|
||||
}
|
||||
}
|
||||
|
||||
public void handleMove(int fromX, int fromY, int toX, int toY) {
|
||||
String from = coordToChessNotation(fromX, fromY);
|
||||
String to = coordToChessNotation(toX, toY);
|
||||
if (engine.move(from, to)) {
|
||||
updateGuiBoard();
|
||||
gui.getField(selectedRow, selectedCol).setBorder(null);
|
||||
|
||||
// Jetzt: Spielstatus prüfen!
|
||||
if (engine.isMated()) {
|
||||
String winner = engine.getCurrentPlayer().equals("WHITE") ? "SCHWARZ" : "WEIß";
|
||||
gui.displayMessage(winner + " hat gewonnen (Schachmatt)!");
|
||||
// Optional: Hier Spiel beenden/disable oder restart anbieten
|
||||
} else if (engine.isStalemate() || engine.isDraw()) {
|
||||
gui.displayMessage("Remis! (Stalemate oder andere Regel)");
|
||||
}
|
||||
} else {
|
||||
gui.displayMessage("Ungültiger Zug!");
|
||||
}
|
||||
}
|
||||
|
||||
public void updateGuiBoard() {
|
||||
gui.updateBoard(engine.getBoardUnicode());
|
||||
// **Neues MoveDTO statt String-Kram!**
|
||||
MoveDTO move = new MoveDTO(selectedRow, selectedCol, row, col);
|
||||
handleMove(move);
|
||||
|
||||
selectedRow = -1;
|
||||
selectedCol = -1;
|
||||
}
|
||||
}
|
||||
|
||||
private String coordToChessNotation(int x, int y) {
|
||||
// (0,0) -> "A8", (7,7) -> "H1"
|
||||
char file = (char)('A' + y); // ACHTUNG! col == y == FILE, row == x == RANK
|
||||
|
||||
public void handleMove(MoveDTO move) {
|
||||
if (engine.move(move)) {
|
||||
updateGuiBoard();
|
||||
|
||||
// Jetzt: Spielstatus prüfen!
|
||||
if (engine.isMated()) {
|
||||
String winner = engine.getCurrentPlayer().equals("WHITE") ? "SCHWARZ" : "WEIß";
|
||||
gui.displayMessage(winner + " hat gewonnen (Schachmatt)!");
|
||||
} else if (engine.isStalemate() || engine.isDraw()) {
|
||||
gui.displayMessage("Remis! (Stalemate oder andere Regel)");
|
||||
}
|
||||
} else {
|
||||
gui.displayMessage("Ungültiger Zug!");
|
||||
}
|
||||
}
|
||||
|
||||
public void updateGuiBoard() {
|
||||
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) {
|
||||
char file = (char)('A' + y);
|
||||
int rank = 8 - x;
|
||||
return "" + file + rank;
|
||||
}
|
||||
|
||||
private int[] chessNotationToCoord(String square) {
|
||||
// "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};
|
||||
}
|
||||
|
||||
// ... resetFieldBackground wie gehabt ...
|
||||
private void resetFieldBackground(int row, int col) {
|
||||
if ((row + col) % 2 == 0) {
|
||||
gui.getField(row, col).setBackground(new Color(0x778da9));
|
||||
|
|
Loading…
Reference in New Issue