promotion without undo
This commit is contained in:
parent
f75359ffb6
commit
989df81c07
|
|
@ -13,6 +13,7 @@ public class Board {
|
|||
private Integer selectedY = null;
|
||||
private ArrayList<int[]> highlightedSquares = new ArrayList<>();
|
||||
private ArrayList<Move> moveHistory = new ArrayList<>();
|
||||
private int[] enPassantTarget = null;
|
||||
|
||||
|
||||
/*public Board(int colNum, int lineNum) {
|
||||
|
|
@ -203,8 +204,38 @@ public class Board {
|
|||
selectedX = null;
|
||||
selectedY = null;
|
||||
highlightedSquares.clear();
|
||||
|
||||
// Check for en passant capture BEFORE updating enPassantTarget!
|
||||
if (selectedPiece.getType() == PieceType.Pawn &&
|
||||
enPassantTarget != null &&
|
||||
x == enPassantTarget[0] &&
|
||||
y == enPassantTarget[1]) {
|
||||
|
||||
int capturedY = selectedPiece.isWhite() ? y - 1 : y + 1;
|
||||
Piece capturedPawn = getPieceAt(x, capturedY);
|
||||
|
||||
// Validate that it's an enemy pawn that just moved two steps
|
||||
if (capturedPawn != null &&
|
||||
capturedPawn.getType() == PieceType.Pawn &&
|
||||
capturedPawn.isWhite() != selectedPiece.isWhite()) {
|
||||
|
||||
// Remove the pawn captured en passant
|
||||
pieces.remove(capturedPawn);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Update enPassantTarget
|
||||
if (selectedPiece.getType() == PieceType.Pawn && selectedY != null && Math.abs(y - selectedY) == 2) {
|
||||
int middleY = (y + selectedY) / 2;
|
||||
enPassantTarget = new int[]{x, middleY};
|
||||
} else {
|
||||
enPassantTarget = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public boolean isSelected(int x, int y) {
|
||||
return selectedX != null && selectedY != null && selectedX == x && selectedY == y;
|
||||
}
|
||||
|
|
@ -416,19 +447,18 @@ public ArrayList<int[]> computeLegalMoves(Piece piece) {
|
|||
PieceType type = piece.getType();
|
||||
|
||||
if (type == PieceType.Pawn) {
|
||||
int dir = piece.isWhite() ? -1 : 1; // reverse the -1 and 1 to match the board
|
||||
int dir = piece.isWhite() ? -1 : 1;
|
||||
int nextY = y + dir;
|
||||
boolean isPromoting =(piece.isWhite() && nextY == 7)||(!piece.isWhite() && nextY==0);
|
||||
|
||||
|
||||
// Move forward if square is empty
|
||||
if (isEmpty(x, nextY)) {
|
||||
moves.add(new int[]{x, nextY});
|
||||
|
||||
// Check for first move double step
|
||||
int startRow = piece.isWhite()? 6:1; // same here i reversed 6 and 1
|
||||
|
||||
// First move double step
|
||||
int startRow = piece.isWhite() ? 6 : 1;
|
||||
int twoStepsY = y + 2 * dir;
|
||||
if (y== startRow && isEmpty(x,twoStepsY)) {
|
||||
moves.add(new int[] {x, twoStepsY});
|
||||
if (y == startRow && isEmpty(x, twoStepsY)) {
|
||||
moves.add(new int[]{x, twoStepsY});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -439,15 +469,23 @@ public ArrayList<int[]> computeLegalMoves(Piece piece) {
|
|||
if (isEnemy(x + 1, nextY, piece.isWhite())) {
|
||||
moves.add(new int[]{x + 1, nextY});
|
||||
}
|
||||
//promotion à revoir ca marche pas
|
||||
if (isPromoting) {
|
||||
piece.setType(PieceType.Queen);
|
||||
piece.setY(nextY);
|
||||
|
||||
// En passant
|
||||
if (enPassantTarget != null) {
|
||||
int targetX = enPassantTarget[0];
|
||||
int targetY = enPassantTarget[1];
|
||||
if (nextY == targetY && Math.abs(x - targetX) == 1) {
|
||||
moves.add(new int[]{targetX, targetY});
|
||||
}
|
||||
// en passant
|
||||
|
||||
}
|
||||
|
||||
// Promotion check (you shouldn't promote here — only on move execution)
|
||||
// Do NOT change piece type inside this method
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (type == PieceType.Rook) {
|
||||
// Directions : haut, bas, gauche, droite
|
||||
|
|
@ -538,10 +576,13 @@ public ArrayList<int[]> computeLegalMoves(Piece piece) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
return moves;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -24,4 +24,6 @@ public class Move {
|
|||
public boolean isWhite() { return isWhite; }
|
||||
public PieceType getType() { return type; }
|
||||
public Piece getCapturedPiece() { return capturedPiece; }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue