New method moveWithPromotion implemented in ChessEngine

SavePgn
Justin 2025-06-22 23:04:20 +02:00
parent f72883710f
commit 31a917d586
1 changed files with 31 additions and 0 deletions

View File

@ -87,6 +87,37 @@ public class ChessEngine {
return convertPieceToDTO(piece);
}
public boolean moveWithPromotion(MoveDTO move, String promotionPiece) {
String from = "" + (char)('A' + move.getFromCol()) + (8 - move.getFromRow());
String to = "" + (char)('A' + move.getToCol()) + (8 - move.getToRow());
// Die Farbe bestimmen!
boolean isWhite = (8 - move.getFromRow()) < (8 - move.getToRow());
Piece promotion;
switch (promotionPiece) {
case "ROOK": promotion = isWhite ? Piece.WHITE_ROOK : Piece.BLACK_ROOK; break;
case "KNIGHT": promotion = isWhite ? Piece.WHITE_KNIGHT : Piece.BLACK_KNIGHT; break;
case "BISHOP": promotion = isWhite ? Piece.WHITE_BISHOP : Piece.BLACK_BISHOP; break;
default: promotion = isWhite ? Piece.WHITE_QUEEN : Piece.BLACK_QUEEN;
}
Move libMove = new Move(Square.valueOf(from), Square.valueOf(to), promotion);
if (board.legalMoves().contains(libMove)) {
board.doMove(libMove);
if (currentMoveIndex < moves.size()) {
moves = new ArrayList<>(moves.subList(0, currentMoveIndex));
}
moves.add(libMove);
currentMoveIndex++;
logger.info("Promotionszug durchgeführt: " + libMove);
return true;
}
logger.warning("Ungültiger Promotionszug: " + libMove);
return false;
}
public BoardDTO getBoardAsDTO() {
logger.info("Erstelle DTO-Abbild des Boards");
PieceDTO[][] dtoBoard = new PieceDTO[8][8];