Reworked handleMove for pawn promotion feature

SavePgn
Justin 2025-06-22 23:13:07 +02:00
parent 31a917d586
commit dac360d411
1 changed files with 20 additions and 11 deletions

View File

@ -137,20 +137,29 @@ public class Controller {
}
}
public void handleMove(MoveDTO move) {
private void handleMove(MoveDTO move) {
BoardDTO boardDTO = engine.getBoardAsDTO();
PieceDTO piece = boardDTO.getBoard()[move.getFromRow()][move.getFromCol()];
boolean isPawn = piece != null && piece.getType().equals("PAWN");
boolean isWhitePromotion = isPawn && piece.getColor().equals("WHITE") && move.getToRow() == 0;
boolean isBlackPromotion = isPawn && piece.getColor().equals("BLACK") && move.getToRow() == 7;
if (isWhitePromotion || isBlackPromotion) {
String color = piece.getColor().equals("WHITE") ? "Weiß" : "Schwarz";
String promotion = gui.showPromotionDialog(color);
if (engine.moveWithPromotion(move, promotion)) {
updateGuiBoard();
gui.updateMoveList(engine.getMoveListStringsGrouped());
} else {
gui.displayMessage("Ungültiger Promotionszug!");
}
return;
}
// Normale Züge wie gehabt:
if (engine.move(move)) {
updateGuiBoard();
//Züge in der MoveList aktualisieren
gui.updateMoveList(engine.getMoveListStringsGrouped());
//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!");
}