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