From 10373c8e927e9fa23b33ad568757c6d3bdfd977b Mon Sep 17 00:00:00 2001 From: willo Date: Sat, 17 May 2025 17:53:21 +0200 Subject: [PATCH] - added the castling method --- src/backend/MovePiece.java | 6 ++++ src/backend/MovementCapabilities.java | 52 +++++++++++++++++++++++++-- src/backend/Piece.java | 2 +- 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/src/backend/MovePiece.java b/src/backend/MovePiece.java index ff406e2..52039df 100644 --- a/src/backend/MovePiece.java +++ b/src/backend/MovePiece.java @@ -173,6 +173,12 @@ public class MovePiece { return true; } } + + // Castling move + MovementCapabilities mc = new MovementCapabilities(); + if (mc.castling(piece, x, y, board)) { + return true; + } return false; } diff --git a/src/backend/MovementCapabilities.java b/src/backend/MovementCapabilities.java index e2e3457..ba7f572 100644 --- a/src/backend/MovementCapabilities.java +++ b/src/backend/MovementCapabilities.java @@ -50,11 +50,57 @@ public class MovementCapabilities { public boolean castling(Piece king, int x, int y, Board board) { - if (king.getType()!= PieceType.King|| king.getdidMove()) { - return false; - + if (king.getType()!= PieceType.King|| king.getDidMove()) { + return false; } + int currentX = king.getX(); + int currentY = king.getY(); + boolean isWhite = king.isWhite(); // getting coordinates and color of the king + + + if (y != currentY) { // checking to see if the king has moved during the game yet + return false; + } + + if (x==currentX+2) { // moving the king to the right of the rook AKA "kingside castling" + Piece rook = board.getPiece(7, currentY); + + // writing each condition separately instead of in the if loop for more clarity + boolean isRook = rook != null && rook.getType()== PieceType.Rook; // check if rook exists + boolean rookMoved = !rook.getDidMove(); + boolean emptySpaceRight = board.getPiece(5, currentY)== null && board.getPiece(6, currentY)== null; + + if (isRook && rookMoved) { + if (emptySpaceRight) { + + board.movePiece(currentX, currentY, 6, currentY); // moving the king to ... + board.movePiece(7, currentY, 5, currentY); // moving the rook to ... + + return true; + } + } + } + + if (x==currentX-2) { // moving the king to the left of the rook AKA "queenside castling") + Piece rook = board.getPiece(0, currentY); + + boolean isRook = rook != null && rook.getType()== PieceType.Rook; + boolean rookMoved = !rook.getDidMove(); + boolean emptySpaceLeft = board.getPiece(3, currentY) == null && board.getPiece(2, currentY) == null && board.getPiece(1, currentY)== null; + + if (isRook && rookMoved) { + if (emptySpaceLeft) { + board.movePiece(currentX, currentY, 2, currentY); // moving king to... + board.movePiece(0,currentY,3,currentY); // moving rook to... + return true; + + } + + } + } + + return false; } } diff --git a/src/backend/Piece.java b/src/backend/Piece.java index b30bd53..2063e20 100644 --- a/src/backend/Piece.java +++ b/src/backend/Piece.java @@ -39,7 +39,7 @@ public class Piece { return white; } - public boolean getdidMove() { // added getter + public boolean getDidMove() { // added getter return didMove; }