From de01655fbb6fb259c54fb9e14041b4d8927a8d91 Mon Sep 17 00:00:00 2001 From: lrave Date: Thu, 22 May 2025 16:56:59 +0200 Subject: [PATCH] Promotion fixed --- src/backend/Board.java | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/backend/Board.java b/src/backend/Board.java index 9ff6c28..1967c86 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -2,9 +2,10 @@ package backend; import java.util.ArrayList; - +import java.util.Scanner; public class Board { - + + private final Scanner scanner = new Scanner(System.in); private int selectedX = -1; // negative value means impossible x and y so unselected private int selectedY = -1; @@ -186,6 +187,10 @@ public class Board { board[3][y].setY(y); } } + if (pieceToMove.getType() == PieceType.Pawn && (y == 0 || y == 7)) { + PieceType promotionType = askPromotionChoice(pieceToMove.isWhite()); + board[x][y] = new Piece(x, y, promotionType, pieceToMove.isWhite()); + } // Handle en passant capture if (pieceToMove.getType() == PieceType.Pawn && board[x][y] == null && x != selectedX) { @@ -290,6 +295,23 @@ public class Board { clearConsole(); System.out.println(toString()); } + + private PieceType askPromotionChoice(boolean isWhite) { + System.out.println("Choose piece to promote to:"); + System.out.println("Enter Q for Queen, R for Rook, B for Bishop, N for Knight"); + + while (true) { + String input = scanner.nextLine().trim().toUpperCase(); + switch (input) { + case "Q": return PieceType.Queen; + case "R": return PieceType.Rook; + case "B": return PieceType.Bishop; + case "N": return PieceType.Knight; + default: + System.out.println("Invalid choice. Please enter Q, R, B, or N."); + } + } + } public void undoLastMove() { // TODO