Implement core Controller functionality
parent
8dcbe0a5ca
commit
17bdd3e37f
|
@ -1,14 +1,116 @@
|
|||
package de.hs_mannheim.informatik.chess.controller;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.swing.BorderFactory;
|
||||
|
||||
import de.hs_mannheim.informatik.chess.gui.Gui;
|
||||
import de.hs_mannheim.informatik.chess.model.ChessEngine;
|
||||
|
||||
public class Controller {
|
||||
Gui gui;
|
||||
ChessEngine engine;
|
||||
private int selectedRow = -1, selectedCol = -1;
|
||||
private List<int[]> highlightedFields = new ArrayList<>();
|
||||
|
||||
public Controller(Gui gui, ChessEngine engine) {
|
||||
this.gui = gui;
|
||||
this.engine = engine;
|
||||
initListeners();
|
||||
updateGuiBoard();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
//Mögliche Ziele highlighten
|
||||
String from = coordToChessNotation(row, col);
|
||||
List<String> targets = engine.getLegalDestinations(from);
|
||||
for (String t : targets) {
|
||||
int[] xy = chessNotationToCoord(t);
|
||||
gui.getField(xy[0], xy[1]).setBackground(Color.YELLOW);
|
||||
highlightedFields.add(xy);
|
||||
}
|
||||
} else {
|
||||
// Zweites Feld: Zug probieren
|
||||
// Highlight entfernen
|
||||
for (int[] xy : highlightedFields) {
|
||||
resetFieldBackground(xy[0], xy[1]);
|
||||
}
|
||||
highlightedFields.clear();
|
||||
|
||||
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!
|
||||
if (engine.isMated()) {
|
||||
String winner = engine.getCurrentPlayer().equals("WHITE") ? "SCHWARZ" : "WEIß";
|
||||
gui.displayMessage(winner + " hat gewonnen (Schachmatt)!");
|
||||
// Optional: Hier Spiel beenden/disable oder restart anbieten
|
||||
} else if (engine.isStalemate() || engine.isDraw()) {
|
||||
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) {
|
||||
// (0,0) -> "A8", (7,7) -> "H1"
|
||||
char file = (char)('A' + y); // ACHTUNG! col == y == FILE, row == x == RANK
|
||||
int rank = 8 - x;
|
||||
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};
|
||||
}
|
||||
|
||||
private void resetFieldBackground(int row, int col) {
|
||||
if ((row + col) % 2 == 0) {
|
||||
gui.getField(row, col).setBackground(new Color(0x778da9));
|
||||
} else {
|
||||
gui.getField(row, col).setBackground(new Color(0xe0e1dd));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue