Board flip feature implemented with proper highlight mapping

Quicksave
Justin 2025-06-19 22:28:10 +02:00
parent 0e417bdca6
commit 93570f2c1c
1 changed files with 42 additions and 11 deletions

View File

@ -10,6 +10,7 @@ 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.PieceDTO;
import de.hs_mannheim.informatik.chess.model.BoardDTO;
import de.hs_mannheim.informatik.chess.view.GameGui;
@ -46,22 +47,50 @@ public class Controller {
}
gui.getFlipBoardButton().addActionListener(e -> {
gui.setFlipped(!gui.isFlipped()); // Zustand wechseln
updateGuiBoard(); // Brett neu zeichnen (mit Flip!)
});
// 1. ALLE Highlights und Borders zurücksetzen (GUI-Koordinaten!)
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
resetFieldBackground(row, col);
gui.getField(row, col).setBorder(null);
}
}
highlightedFields.clear();
selectedRow = -1;
selectedCol = -1;
// 2. Flip-Zustand ändern
gui.setFlipped(!gui.isFlipped());
// 3. Board neu zeichnen
updateGuiBoard();
});
}
private void handleClick(int guiRow, int guiCol) {
int modelRow = flipRow(guiRow);
int modelCol = flipCol(guiCol);
// --- NEU: Figur am Feld?
BoardDTO boardDTO = engine.getBoardAsDTO();
PieceDTO piece = boardDTO.getBoard()[modelRow][modelCol];
// --- NEU: Ist eine Figur da und hat sie die aktuelle Farbe?
String amZug = engine.getCurrentPlayer(); // "WHITE" oder "BLACK"
if (selectedRow == -1 && selectedCol == -1) {
if (piece == null || !piece.getColor().equals(amZug)) {
// Falsche Farbe oder leeres Feld → abbrechen, keine Highlights!
return;
}
selectedRow = modelRow;
selectedCol = modelCol;
gui.getField(guiRow, guiCol).setBorder(BorderFactory.createLineBorder(Color.RED, 2));
// Mögliches Ziehlhighlight
String fromSquare = coordToChessNotation(modelRow, modelCol);
List<MoveDTO> moves = engine.getLegalDestinations(fromSquare);
for (MoveDTO move : moves) {
int guiToRow = gui.isFlipped() ? 7 - move.getToRow() : move.getToRow();
int guiToCol = gui.isFlipped() ? 7 - move.getToCol() : move.getToCol();
@ -69,12 +98,14 @@ public class Controller {
highlightedFields.add(new int[]{guiToRow, guiToCol});
}
} else {
// Reset highlights wie bisher
// ... wie gehabt ...
for (int[] xy : highlightedFields) {
resetFieldBackground(xy[0], xy[1]);
}
highlightedFields.clear();
gui.getField(flipRow(selectedRow), flipCol(selectedCol)).setBorder(null);
int oldGuiRow = gui.isFlipped() ? 7 - selectedRow : selectedRow;
int oldGuiCol = gui.isFlipped() ? 7 - selectedCol : selectedCol;
gui.getField(oldGuiRow, oldGuiCol).setBorder(null);
MoveDTO move = new MoveDTO(selectedRow, selectedCol, modelRow, modelCol);
handleMove(move);
@ -84,6 +115,8 @@ public class Controller {
}
public void handleMove(MoveDTO move) {
if (engine.move(move)) {
updateGuiBoard();
@ -106,11 +139,9 @@ public class Controller {
}
// Hilfsmethode, um von Koordinaten (row/col) auf z.B. "E2" zu kommen
private String coordToChessNotation(int x, int y) {
int row = gui.isFlipped() ? 7 - x : x;
int col = gui.isFlipped() ? 7 - y : y;
char file = (char)('A' + col);
int rank = 8 - row;
private String coordToChessNotation(int modelRow, int modelCol) {
char file = (char)('A' + modelCol);
int rank = 8 - modelRow;
return "" + file + rank;
}