Compare commits
2 Commits
c8bc85fe36
...
3a6a8f2d72
| Author | SHA1 | Date |
|---|---|---|
|
|
3a6a8f2d72 | |
|
|
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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,11 +47,12 @@ public class Piece {
|
|||
this.y = y;
|
||||
}
|
||||
|
||||
private boolean hasMoved = false;
|
||||
|
||||
public boolean hasMoved() {
|
||||
public boolean hasMoved() {//track if a piece has been moved in the past (for castling)
|
||||
return hasMoved;
|
||||
}
|
||||
|
||||
private boolean hasMoved = false;//initially, no piece has been moved
|
||||
|
||||
public void setMoved(boolean moved) {
|
||||
this.hasMoved = moved;
|
||||
|
|
|
|||
Loading…
Reference in New Issue