Fixed flip board feature
parent
7da371cd9b
commit
0e417bdca6
|
|
@ -26,13 +26,20 @@ public class Controller {
|
||||||
updateGuiBoard();
|
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() {
|
private void initListeners() {
|
||||||
for (int row = 0; row < 8; row++) {
|
for (int row = 0; row < 8; row++) {
|
||||||
for (int col = 0; col < 8; col++) {
|
for (int col = 0; col < 8; col++) {
|
||||||
final int r = row, c = col;
|
final int r = row, c = col;
|
||||||
gui.getField(row, col).addMouseListener(new MouseAdapter() {
|
gui.getField(row, col).addMouseListener(new MouseAdapter() {
|
||||||
public void mousePressed(MouseEvent e) {
|
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) {
|
private void handleClick(int guiRow, int guiCol) {
|
||||||
if (selectedRow == -1 && selectedCol == -1) {
|
int modelRow = flipRow(guiRow);
|
||||||
// Erstes Feld gewählt
|
int modelCol = flipCol(guiCol);
|
||||||
selectedRow = row;
|
|
||||||
selectedCol = col;
|
|
||||||
gui.getField(row, col).setBorder(BorderFactory.createLineBorder(Color.RED, 2));
|
|
||||||
|
|
||||||
// Mögliche Ziele highlighten (DTOs!)
|
if (selectedRow == -1 && selectedCol == -1) {
|
||||||
String fromSquare = coordToChessNotation(row, col); // z.B. "E2"
|
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);
|
List<MoveDTO> moves = engine.getLegalDestinations(fromSquare);
|
||||||
for (MoveDTO move : moves) {
|
for (MoveDTO move : moves) {
|
||||||
int toRow = move.getToRow();
|
int guiToRow = gui.isFlipped() ? 7 - move.getToRow() : move.getToRow();
|
||||||
int toCol = move.getToCol();
|
int guiToCol = gui.isFlipped() ? 7 - move.getToCol() : move.getToCol();
|
||||||
gui.getField(toRow, toCol).setBackground(Color.YELLOW);
|
gui.getField(guiToRow, guiToCol).setBackground(Color.YELLOW);
|
||||||
highlightedFields.add(new int[]{toRow, toCol});
|
highlightedFields.add(new int[]{guiToRow, guiToCol});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Zweites Feld: Zug probieren
|
// Reset highlights wie bisher
|
||||||
// Highlight entfernen
|
|
||||||
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);
|
||||||
|
|
||||||
gui.getField(selectedRow, selectedCol).setBorder(null);
|
MoveDTO move = new MoveDTO(selectedRow, selectedCol, modelRow, modelCol);
|
||||||
|
|
||||||
// **Neues MoveDTO statt String-Kram!**
|
|
||||||
MoveDTO move = new MoveDTO(selectedRow, selectedCol, row, col);
|
|
||||||
handleMove(move);
|
handleMove(move);
|
||||||
|
|
||||||
selectedRow = -1;
|
selectedRow = -1;
|
||||||
selectedCol = -1;
|
selectedCol = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void handleMove(MoveDTO move) {
|
public void handleMove(MoveDTO move) {
|
||||||
if (engine.move(move)) {
|
if (engine.move(move)) {
|
||||||
updateGuiBoard();
|
updateGuiBoard();
|
||||||
|
|
@ -102,11 +107,14 @@ 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 x, int y) {
|
||||||
char file = (char)('A' + y);
|
int row = gui.isFlipped() ? 7 - x : x;
|
||||||
int rank = 8 - x;
|
int col = gui.isFlipped() ? 7 - y : y;
|
||||||
|
char file = (char)('A' + col);
|
||||||
|
int rank = 8 - row;
|
||||||
return "" + file + rank;
|
return "" + file + rank;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// ... resetFieldBackground wie gehabt ...
|
// ... resetFieldBackground wie gehabt ...
|
||||||
private void resetFieldBackground(int row, int col) {
|
private void resetFieldBackground(int row, int col) {
|
||||||
if ((row + col) % 2 == 0) {
|
if ((row + col) % 2 == 0) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue