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!
This commit is contained in:
Juliette 2025-05-21 22:22:03 +02:00
parent 3ac5c282a5
commit bf169743bf
1 changed files with 23 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package backend;
import java.util.ArrayList; import java.util.ArrayList;
public class Board { public class Board {
private int colNum; private int colNum;
@ -169,7 +170,7 @@ public class Board {
selectedY = y; selectedY = y;
highlight.clear(); highlight.clear();
highlight.addAll(findMoves(p)); highlight.addAll(findCastlingMoves(p)); //the NEW UPDATED ONE for CASTLING
} else { } else {
selectedX = -1; selectedX = -1;
selectedY = -1; selectedY = -1;
@ -354,6 +355,27 @@ public class Board {
return moves; return moves;
} }
public ArrayList<Position> findCastlingMoves(Piece p) {
ArrayList<Position> 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<Position> moves, int x, int y, boolean isWhite, int dx, int dy) { private void extend(ArrayList<Position> moves, int x, int y, boolean isWhite, int dx, int dy) {
int nx = x + dx; int nx = x + dx;