Switch Controller to use DTOs for board updates
parent
9adb0db66b
commit
ed573d3338
|
|
@ -9,22 +9,25 @@ import java.util.List;
|
||||||
import javax.swing.BorderFactory;
|
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.BoardDTO;
|
||||||
|
import de.hs_mannheim.informatik.chess.model.PieceDTO;
|
||||||
import de.hs_mannheim.informatik.chess.view.Gui;
|
import de.hs_mannheim.informatik.chess.view.Gui;
|
||||||
|
|
||||||
public class Controller {
|
public class Controller {
|
||||||
Gui gui;
|
Gui gui;
|
||||||
ChessEngine engine;
|
ChessEngine engine;
|
||||||
private int selectedRow = -1, selectedCol = -1;
|
private int selectedRow = -1, selectedCol = -1;
|
||||||
private List<int[]> highlightedFields = new ArrayList<>();
|
private List<int[]> highlightedFields = new ArrayList<>();
|
||||||
|
|
||||||
public Controller(Gui gui, ChessEngine engine) {
|
public Controller(Gui gui, ChessEngine engine) {
|
||||||
this.gui = gui;
|
this.gui = gui;
|
||||||
this.engine = engine;
|
this.engine = engine;
|
||||||
initListeners();
|
initListeners();
|
||||||
updateGuiBoard();
|
updateGuiBoard();
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
|
@ -36,76 +39,71 @@ public class Controller {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleClick(int row, int col) {
|
private void handleClick(int row, int col) {
|
||||||
if (selectedRow == -1 && selectedCol == -1) {
|
if (selectedRow == -1 && selectedCol == -1) {
|
||||||
// Erstes Feld gewählt
|
// Erstes Feld gewählt
|
||||||
selectedRow = row;
|
selectedRow = row;
|
||||||
selectedCol = col;
|
selectedCol = col;
|
||||||
gui.getField(row, col).setBorder(BorderFactory.createLineBorder(Color.RED, 2));
|
gui.getField(row, col).setBorder(BorderFactory.createLineBorder(Color.RED, 2));
|
||||||
|
|
||||||
//Mögliche Ziele highlighten
|
// Mögliche Ziele highlighten (DTOs!)
|
||||||
String from = coordToChessNotation(row, col);
|
String fromSquare = coordToChessNotation(row, col); // z.B. "E2"
|
||||||
List<String> targets = engine.getLegalDestinations(from);
|
List<MoveDTO> moves = engine.getLegalDestinations(fromSquare);
|
||||||
for (String t : targets) {
|
for (MoveDTO move : moves) {
|
||||||
int[] xy = chessNotationToCoord(t);
|
int toRow = move.getToRow();
|
||||||
gui.getField(xy[0], xy[1]).setBackground(Color.YELLOW);
|
int toCol = move.getToCol();
|
||||||
highlightedFields.add(xy);
|
gui.getField(toRow, toCol).setBackground(Color.YELLOW);
|
||||||
}
|
highlightedFields.add(new int[]{toRow, toCol});
|
||||||
} else {
|
}
|
||||||
// Zweites Feld: Zug probieren
|
} else {
|
||||||
// Highlight entfernen
|
// Zweites Feld: Zug probieren
|
||||||
for (int[] xy : highlightedFields) {
|
// Highlight entfernen
|
||||||
resetFieldBackground(xy[0], xy[1]);
|
for (int[] xy : highlightedFields) {
|
||||||
}
|
resetFieldBackground(xy[0], xy[1]);
|
||||||
highlightedFields.clear();
|
}
|
||||||
|
highlightedFields.clear();
|
||||||
|
|
||||||
gui.getField(selectedRow, selectedCol).setBorder(null);
|
gui.getField(selectedRow, selectedCol).setBorder(null);
|
||||||
handleMove(selectedRow, selectedCol, row, col);
|
|
||||||
selectedRow = -1;
|
|
||||||
selectedCol = -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void handleMove(int fromX, int fromY, int toX, int toY) {
|
|
||||||
String from = coordToChessNotation(fromX, fromY);
|
|
||||||
String to = coordToChessNotation(toX, toY);
|
|
||||||
if (engine.move(from, to)) {
|
|
||||||
updateGuiBoard();
|
|
||||||
|
|
||||||
// Jetzt: Spielstatus prüfen!
|
// **Neues MoveDTO statt String-Kram!**
|
||||||
if (engine.isMated()) {
|
MoveDTO move = new MoveDTO(selectedRow, selectedCol, row, col);
|
||||||
String winner = engine.getCurrentPlayer().equals("WHITE") ? "SCHWARZ" : "WEIß";
|
handleMove(move);
|
||||||
gui.displayMessage(winner + " hat gewonnen (Schachmatt)!");
|
|
||||||
// Optional: Hier Spiel beenden/disable oder restart anbieten
|
selectedRow = -1;
|
||||||
} else if (engine.isStalemate() || engine.isDraw()) {
|
selectedCol = -1;
|
||||||
gui.displayMessage("Remis! (Stalemate oder andere Regel)");
|
}
|
||||||
}
|
|
||||||
} else {
|
|
||||||
gui.displayMessage("Ungültiger Zug!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void updateGuiBoard() {
|
|
||||||
gui.updateBoard(engine.getBoardUnicode());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String coordToChessNotation(int x, int y) {
|
public void handleMove(MoveDTO move) {
|
||||||
// (0,0) -> "A8", (7,7) -> "H1"
|
if (engine.move(move)) {
|
||||||
char file = (char)('A' + y); // ACHTUNG! col == y == FILE, row == x == RANK
|
updateGuiBoard();
|
||||||
|
|
||||||
|
// Jetzt: Spielstatus prüfen!
|
||||||
|
if (engine.isMated()) {
|
||||||
|
String winner = engine.getCurrentPlayer().equals("WHITE") ? "SCHWARZ" : "WEIß";
|
||||||
|
gui.displayMessage(winner + " hat gewonnen (Schachmatt)!");
|
||||||
|
} else if (engine.isStalemate() || engine.isDraw()) {
|
||||||
|
gui.displayMessage("Remis! (Stalemate oder andere Regel)");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
gui.displayMessage("Ungültiger Zug!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateGuiBoard() {
|
||||||
|
BoardDTO board = engine.getBoardAsDTO();
|
||||||
|
gui.updateBoard(board); // Passe die GUI an, damit sie ein BoardDTO nimmt!
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 rank = 8 - x;
|
||||||
return "" + file + rank;
|
return "" + file + rank;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int[] chessNotationToCoord(String square) {
|
|
||||||
// "A8" -> (0,0), "H1" -> (7,7)
|
|
||||||
char file = square.charAt(0);
|
|
||||||
int rank = square.charAt(1) - '0';
|
|
||||||
int x = 8 - rank;
|
|
||||||
int y = file - 'A';
|
|
||||||
return new int[] {x, y};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// ... 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) {
|
||||||
gui.getField(row, col).setBackground(new Color(0x778da9));
|
gui.getField(row, col).setBackground(new Color(0x778da9));
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue