casling check

This commit is contained in:
Romain MURPHY 2025-05-13 14:28:11 +02:00
parent bac93938e8
commit 51cb78c256
3 changed files with 91 additions and 74 deletions

View File

@ -187,11 +187,13 @@ public class AutoPlayer {
public ArrayList<Move> getAllLegalMoves(ArrayList<ArrayList<Piece>> board, boolean isWhiteTurn, Move lastMove) { public ArrayList<Move> getAllLegalMoves(ArrayList<ArrayList<Piece>> board, boolean isWhiteTurn, Move lastMove) {
ArrayList<Move> legalMoves = new ArrayList<>(); ArrayList<Move> legalMoves = new ArrayList<>();
ArrayList<ArrayList<Boolean>> moves;
for (ArrayList<Piece> row : board) { for (ArrayList<Piece> row : board) {
for (Piece p : row) { for (Piece p : row) {
if (p != null && p.isWhite() == isWhiteTurn) { if (p != null && p.isWhite() == isWhiteTurn) {
ArrayList<ArrayList<Boolean>> moves = kingCheck.getLegalMoves(p, board, lastMove); if (p.getType() == PieceType.King && p.getX() == 4 && (p.getY() == 0 || p.getY() == 7)) {
moves = kingCheck.checkCastling(p, board,lastMove, isWhiteTurn);
} else {moves = kingCheck.getLegalMoves(p, board, lastMove);}
for (int y = 0; y < 8; y++) { for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) { for (int x = 0; x < 8; x++) {
if (moves.get(y).get(x)) { if (moves.get(y).get(x)) {

View File

@ -219,7 +219,10 @@ public class Board {
this.xm = x; this.xm = x;
this.ym = y; this.ym = y;
select = true; select = true;
possibleMoves = kingCheck.getLegalMoves(board.get(y).get(x), board,lastMove); Piece pieceToMove = this.board.get(y).get(x);
if (pieceToMove.getType() == PieceType.King && x == 4 && (y == 0 || y == 7)) {
possibleMoves = kingCheck.checkCastling(board.get(y).get(x), board,lastMove, turnColor);
} else {possibleMoves = kingCheck.getLegalMoves(pieceToMove, board, lastMove);}
// possibleMoves = board.get(y).get(x).getPossibleMoves(board); // possibleMoves = board.get(y).get(x).getPossibleMoves(board);
// possibleMoves = board.get(y).get(x).getPossibleMoves(board, lastMove);//same as kingCheck // possibleMoves = board.get(y).get(x).getPossibleMoves(board, lastMove);//same as kingCheck

View File

@ -56,7 +56,7 @@ public class KingCheck {
if (p != null && p.isWhite() != isWhiteTurn) { if (p != null && p.isWhite() != isWhiteTurn) {
ArrayList<ArrayList<Boolean>> moves = p.getPossibleMoves(board); ArrayList<ArrayList<Boolean>> moves = p.getPossibleMoves(board);
if (moves.get(kingY).get(kingX)) { if (moves.get(kingY).get(kingX)) {
return true;
} }
} }
} }
@ -97,4 +97,16 @@ public class KingCheck {
} }
return copy; return copy;
} }
public ArrayList<ArrayList<Boolean>> checkCastling(Piece piece, ArrayList<ArrayList<Piece>> board, Move lastMove, boolean isWhiteTurn) {
ArrayList<ArrayList<Boolean>> possibleMoves = getLegalMoves(piece, board, lastMove);
if (isKingInCheck(board,isWhiteTurn)) {
int x = piece.getX();
int y = piece.getY();
possibleMoves.get(y).set(x+2,false);
possibleMoves.get(y).set(x-2,false);
}
return possibleMoves;
}
} }