castling wiiiiii
Merge branch 'master' of https://gitarero.ecam.fr/louise.berteloot/OOP_2A5_Project.git
This commit is contained in:
parent
6e899924bd
commit
c8bc85fe36
|
|
@ -188,6 +188,13 @@ public class Board {
|
|||
return;
|
||||
}
|
||||
|
||||
int originalX = selectedPiece.getX();
|
||||
int originalY = selectedPiece.getY();
|
||||
|
||||
// Detect castling before moving the king
|
||||
boolean isCastling = selectedPiece.getType() == PieceType.King && Math.abs(x - originalX) == 2;
|
||||
|
||||
|
||||
|
||||
if (clickedPiece != null) {
|
||||
System.out.println("Capturing piece at: " + x + ", " + y);
|
||||
|
|
@ -198,6 +205,27 @@ public class Board {
|
|||
selectedPiece.setX(x);
|
||||
selectedPiece.setY(y);
|
||||
|
||||
if (isCastling) {
|
||||
int row = selectedPiece.isWhite() ? 7 : 0;
|
||||
if (x > originalX) {
|
||||
// King-side castling
|
||||
Piece rook = getPieceAt(7, row);
|
||||
if (rook != null) {
|
||||
rook.setX(5);
|
||||
rook.setY(row);
|
||||
rook.setMoved(true);
|
||||
}
|
||||
} else {
|
||||
// Queen-side castling
|
||||
Piece rook = getPieceAt(0, row);
|
||||
if (rook != null) {
|
||||
rook.setX(3);
|
||||
rook.setY(row);
|
||||
rook.setMoved(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (selectedPiece.getType() == PieceType.Pawn && Math.abs(y - selectedY) == 2) {
|
||||
pawnDoubleStep = true; //boolean to check if pawn has been moved 2 at start
|
||||
xCoordinatePawn = x; //get its coordinates
|
||||
|
|
@ -226,26 +254,8 @@ public class Board {
|
|||
}
|
||||
}
|
||||
}
|
||||
if (selectedPiece.getType() == PieceType.King && Math.abs(x - selectedX) == 2) {
|
||||
int row = selectedPiece.isWhite() ? 7 : 0;
|
||||
if (x > selectedX) {
|
||||
// King-side castling
|
||||
Piece rook = getPieceAt(7, row);
|
||||
if (rook != null) {
|
||||
rook.setX(5);
|
||||
rook.setY(row);
|
||||
rook.setMoved(true);
|
||||
}
|
||||
} else {
|
||||
// Queen-side castling
|
||||
Piece rook = getPieceAt(0, row);
|
||||
if (rook != null) {
|
||||
rook.setX(3);
|
||||
rook.setY(row);
|
||||
rook.setMoved(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public boolean isSelected(int x, int y) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue