Pawn Promotion - Automatic Queen Promotion as there is nothing in GUI to

select it
This commit is contained in:
Yash Shah 2025-05-17 13:28:11 +02:00
parent 317260609a
commit 58dac05f95
2 changed files with 12 additions and 2 deletions

View File

@ -517,6 +517,14 @@ public void undoLastMove() {
// Update the moved piece's position // Update the moved piece's position
piece.setPosition(move.getToX(), move.getToY()); piece.setPosition(move.getToX(), move.getToY());
if (piece.getType() == PieceType.Pawn) {
// white promotes on y == height1, black on y == 0
int promotionRank = piece.isWhite() ? height - 1 : 0;
if (piece.getY() == promotionRank) {
piece.setType(PieceType.Queen);
}
}
// Save move in history for undo // Save move in history for undo
moveHistory.add(move); moveHistory.add(move);

View File

@ -24,6 +24,9 @@ public class Piece {
public void setY(int y) { public void setY(int y) {
this.y = y; this.y = y;
} }
public void setType(PieceType newType) {
this.type = newType;
}
public PieceType getType() { public PieceType getType() {
return type; return type;
@ -55,7 +58,6 @@ public class Piece {
public void setHasMoved(boolean moved) { public void setHasMoved(boolean moved) {
this.hasMoved = moved; this.hasMoved = moved;
} }
} }