From bf169743bf50bd27e057ec952036c9b94bdaa20b Mon Sep 17 00:00:00 2001 From: Juliette Date: Wed, 21 May 2025 22:22:03 +0200 Subject: [PATCH] Added in board a findmovesandcastlingmoves it has find moves and it adds the findcastling moves to it. Couldn't put everything into findmoves because it created an inifinte loop when caleed in chessrules... PROBLEMATIC!!! Only need to implemnt the actual castling part now! --- src/backend/Board.java | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/backend/Board.java b/src/backend/Board.java index 808a512..f6158f3 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -2,6 +2,7 @@ package backend; import java.util.ArrayList; + public class Board { private int colNum; @@ -169,7 +170,7 @@ public class Board { selectedY = y; highlight.clear(); - highlight.addAll(findMoves(p)); + highlight.addAll(findCastlingMoves(p)); //the NEW UPDATED ONE for CASTLING } else { selectedX = -1; selectedY = -1; @@ -354,6 +355,27 @@ public class Board { return moves; } + + public ArrayList findCastlingMoves(Piece p) { + + ArrayList moves = findMoves(p); //all the basic moves + + if (p.getType() == PieceType.King) { + ChessRulesCheck rules = new ChessRulesCheck(); + boolean white = p.isWhite(); + int rank = white ? 7 : 0, kx = p.getX(); + + if (kx == 4 && rules.canItCastle(this, white, true)) { + moves.add(new Position(6, rank)); + } + + if (kx == 4 && rules.canItCastle(this, white, false)) { + moves.add(new Position(2, rank)); + } + } + + return moves; + } private void extend(ArrayList moves, int x, int y, boolean isWhite, int dx, int dy) { int nx = x + dx;