promotion update

This commit is contained in:
Romain MURPHY 2025-04-30 17:05:03 +02:00
parent ebf8687fa5
commit 13ec9fb4e8
2 changed files with 35 additions and 5 deletions

View File

@ -3,6 +3,8 @@ package backend;
import java.util.ArrayList;
import java.util.LinkedList;
import javax.swing.JOptionPane;
public class Board {
private KingCheck kingCheck = new KingCheck();
private int width = 8;
@ -151,22 +153,45 @@ public class Board {
}
}
}
board.get(this.ym).set(this.xm,null);
this.setPiece(x,y,pieceToMove.getType(),pieceToMove.isWhite());
Piece movedPiece = this.getPiece(x, y);
if (movedPiece instanceof Pawn) {
int promotionRow = movedPiece.isWhite() ? 0 : 7;
if (y == promotionRow) {
// Replace pawn with promoted piece
// System.out.println()
this.setPiece(x,y,PieceType.Queen,pieceToMove.isWhite());
Object[] choices = { "Queen", "Rook", "Bishop", "Knight" };
String selected = (String) JOptionPane.showInputDialog(
null,
"Choose a piece for promotion:",
"Pawn Promotion",
JOptionPane.PLAIN_MESSAGE,
null,
choices,
"Queen"
);
Piece promoted;
switch (selected) {
case "Rook":
promoted = new Rook(x, y, false);
break;
case "Bishop":
promoted = new Bishop(x, y, false);
break;
case "Knight":
promoted = new Knight(x, y, false);
break;
default:
promoted = new Queen(x, y, false);
}
board.get(y).set(x, promoted);
this.setPiece(x,y,promoted.getType(),pieceToMove.isWhite());
// You can replace Queen with any piece type as needed
}
}
if (movedPiece != null) {
movedPiece.setMoved(true);
}
board.get(this.ym).set(this.xm,null);
}
}

View File

@ -275,6 +275,11 @@ public class MyInterface extends JFrame {
public void showGameOverMessage(String message) {
JOptionPane.showMessageDialog(this, message, "Game Over", JOptionPane.INFORMATION_MESSAGE);
game.toggleAI(false);
game.toggleAI(true);
chckbxWhiteAI.setSelected(false);
chckbxBlackAI.setSelected(false);
}
private int askDifficulty() {
Object[] options = {"Easy", "Medium", "Hard"};