diff --git a/src/backend/Board.java b/src/backend/Board.java index 2974327..4258072 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -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); }