Compare commits
No commits in common. "17bdd3e37f44e24c4496b386a0b1a3e63ec4016f" and "fe3a5dec5b613c97b4e01ad4e1e12985ed7c320b" have entirely different histories.
17bdd3e37f
...
fe3a5dec5b
|
|
@ -1,116 +1,5 @@
|
||||||
package de.hs_mannheim.informatik.chess.controller;
|
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 {
|
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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,11 @@
|
||||||
package de.hs_mannheim.informatik.chess.main;
|
package de.hs_mannheim.informatik.chess.main;
|
||||||
import de.hs_mannheim.informatik.chess.gui.Gui;
|
import de.hs_mannheim.informatik.chess.gui.Gui;
|
||||||
import de.hs_mannheim.informatik.chess.controller.Controller;
|
|
||||||
import de.hs_mannheim.informatik.chess.model.ChessEngine;
|
|
||||||
|
|
||||||
|
|
||||||
public class Main{
|
public class Main{
|
||||||
|
|
||||||
public static void main( String[] args ){
|
public static void main( String[] args ){
|
||||||
Gui gui = new Gui();
|
new Gui();
|
||||||
ChessEngine engine = new ChessEngine();
|
|
||||||
new Controller(gui, engine);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue