castling notes
Merge branch 'master' of https://gitarero.ecam.fr/louise.berteloot/OOP_2A5_Project.git
This commit is contained in:
parent
c8bc85fe36
commit
9a8639a2f1
|
|
@ -188,10 +188,11 @@ public class Board {
|
|||
return;
|
||||
}
|
||||
|
||||
//store for castling
|
||||
int originalX = selectedPiece.getX();
|
||||
int originalY = selectedPiece.getY();
|
||||
|
||||
// Detect castling before moving the king
|
||||
// detects castling
|
||||
boolean isCastling = selectedPiece.getType() == PieceType.King && Math.abs(x - originalX) == 2;
|
||||
|
||||
|
||||
|
|
@ -205,21 +206,19 @@ 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);
|
||||
if (isCastling) {//checks if the king is eligible for castling
|
||||
int row = selectedPiece.isWhite() ? 7 : 0;//deduces the row depending on weither the selected king is on white or not (if white, its in row 7, if its black its row 0)
|
||||
if (x > originalX) {// means we are moving the king towards the right (king side castling)
|
||||
Piece rook = getPieceAt(7, row);//selects the correct rook to be castled depending on weither the king went right or left (here selects right rook)
|
||||
if (rook != null) {//if the rook is there
|
||||
rook.setX(5);//moves the rook to the square to the left of the king
|
||||
rook.setY(row);//same row
|
||||
rook.setMoved(true);//the rook has been moved once so wont be used for castling anymore
|
||||
}
|
||||
} else {
|
||||
// Queen-side castling
|
||||
Piece rook = getPieceAt(0, row);
|
||||
} else {//king moves to the left this time (queen side castling)
|
||||
Piece rook = getPieceAt(0, row);//selects the rook on the left
|
||||
if (rook != null) {
|
||||
rook.setX(3);
|
||||
rook.setX(3);//moves rook to the right of the king
|
||||
rook.setY(row);
|
||||
rook.setMoved(true);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue