Fixed flip board feature

Quicksave
Justin 2025-06-19 21:57:17 +02:00
parent 7da371cd9b
commit 0e417bdca6
1 changed files with 30 additions and 22 deletions

View File

@ -26,13 +26,20 @@ public class Controller {
updateGuiBoard();
}
private int flipRow(int row) {
return gui.isFlipped() ? 7 - row : row;
}
private int flipCol(int col) {
return gui.isFlipped() ? 7 - col : col;
}
private void initListeners() {
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
final int r = row, c = col;
gui.getField(row, col).addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
handleClick(r, c);
handleClick(r, c); // NICHT flippen!
}
});
}
@ -44,41 +51,39 @@ 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 guiRow, int guiCol) {
int modelRow = flipRow(guiRow);
int modelCol = flipCol(guiCol);
// Mögliche Ziele highlighten (DTOs!)
String fromSquare = coordToChessNotation(row, col); // z.B. "E2"
if (selectedRow == -1 && selectedCol == -1) {
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 toRow = move.getToRow();
int toCol = move.getToCol();
gui.getField(toRow, toCol).setBackground(Color.YELLOW);
highlightedFields.add(new int[]{toRow, toCol});
int guiToRow = gui.isFlipped() ? 7 - move.getToRow() : move.getToRow();
int guiToCol = gui.isFlipped() ? 7 - move.getToCol() : move.getToCol();
gui.getField(guiToRow, guiToCol).setBackground(Color.YELLOW);
highlightedFields.add(new int[]{guiToRow, guiToCol});
}
} else {
// Zweites Feld: Zug probieren
// Highlight entfernen
// Reset highlights wie bisher
for (int[] xy : highlightedFields) {
resetFieldBackground(xy[0], xy[1]);
}
highlightedFields.clear();
gui.getField(flipRow(selectedRow), flipCol(selectedCol)).setBorder(null);
gui.getField(selectedRow, selectedCol).setBorder(null);
// **Neues MoveDTO statt String-Kram!**
MoveDTO move = new MoveDTO(selectedRow, selectedCol, row, col);
MoveDTO move = new MoveDTO(selectedRow, selectedCol, modelRow, modelCol);
handleMove(move);
selectedRow = -1;
selectedCol = -1;
}
}
public void handleMove(MoveDTO move) {
if (engine.move(move)) {
updateGuiBoard();
@ -102,11 +107,14 @@ public class Controller {
// 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;
int row = gui.isFlipped() ? 7 - x : x;
int col = gui.isFlipped() ? 7 - y : y;
char file = (char)('A' + col);
int rank = 8 - row;
return "" + file + rank;
}
// ... resetFieldBackground wie gehabt ...
private void resetFieldBackground(int row, int col) {
if ((row + col) % 2 == 0) {