From 0e417bdca644c58097ff9c915963d979d204c219 Mon Sep 17 00:00:00 2001 From: Justin Date: Thu, 19 Jun 2025 21:57:17 +0200 Subject: [PATCH] Fixed flip board feature --- .../chess/controller/Controller.java | 52 +++++++++++-------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/Controller.java b/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/Controller.java index 184ca8a..ec1d867 100644 --- a/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/Controller.java +++ b/schach/src/main/java/de/hs_mannheim/informatik/chess/controller/Controller.java @@ -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 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) {