From 58dac05f95172a1243db7c65d4f02363f335760c Mon Sep 17 00:00:00 2001 From: Yash Shah Date: Sat, 17 May 2025 13:28:11 +0200 Subject: [PATCH] Pawn Promotion - Automatic Queen Promotion as there is nothing in GUI to select it --- src/backend/Board.java | 8 ++++++++ src/backend/Piece.java | 6 ++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/backend/Board.java b/src/backend/Board.java index cabeac8..0659b05 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -517,6 +517,14 @@ public void undoLastMove() { // Update the moved piece's position piece.setPosition(move.getToX(), move.getToY()); + + if (piece.getType() == PieceType.Pawn) { + // white promotes on y == height‐1, 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 moveHistory.add(move); diff --git a/src/backend/Piece.java b/src/backend/Piece.java index 1490f6b..8090bc3 100644 --- a/src/backend/Piece.java +++ b/src/backend/Piece.java @@ -24,6 +24,9 @@ public class Piece { public void setY(int y) { this.y = y; } + public void setType(PieceType newType) { + this.type = newType; + } public PieceType getType() { return type; @@ -55,7 +58,6 @@ public class Piece { public void setHasMoved(boolean moved) { this.hasMoved = moved; } - - + }