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) {
ArrayList<Move> legalMoves = new ArrayList<>();
ArrayList<ArrayList<Boolean>> moves;
for (ArrayList<Piece> row : board) {
for (Piece p : row) {
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 x = 0; x < 8; x++) {
if (moves.get(y).get(x)) {

View File

@ -219,7 +219,10 @@ public class Board {
this.xm = x;
this.ym = y;
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, lastMove);//same as kingCheck

View File

@ -7,94 +7,106 @@ public class KingCheck {
private SoundEffect soundEffect = new SoundEffect();
private boolean soundShouldPlay = true;
public ArrayList<ArrayList<Boolean>> getLegalMoves(Piece piece, ArrayList<ArrayList<Piece>> board, Move lastMove) {
ArrayList<ArrayList<Boolean>> rawMoves = piece.getPossibleMoves(board,lastMove);
ArrayList<ArrayList<Boolean>> rawMoves = piece.getPossibleMoves(board,lastMove);
ArrayList<ArrayList<Boolean>> legalMoves = new ArrayList<>();
for (int y = 0; y < 8; y++) {
ArrayList<Boolean> row = new ArrayList<>();
for (int x = 0; x < 8; x++) {
row.add(false);
}
legalMoves.add(row);
}
ArrayList<ArrayList<Boolean>> legalMoves = new ArrayList<>();
for (int y = 0; y < 8; y++) {
ArrayList<Boolean> row = new ArrayList<>();
for (int x = 0; x < 8; x++) {
row.add(false);
}
legalMoves.add(row);
}
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
if (rawMoves.get(y).get(x)) {
Move move = new Move(piece.getX(), piece.getY(), x, y);
if (isMoveSafe(board, move, piece.isWhite())) {
legalMoves.get(y).set(x, true);
}
}
}
}
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
if (rawMoves.get(y).get(x)) {
Move move = new Move(piece.getX(), piece.getY(), x, y);
if (isMoveSafe(board, move, piece.isWhite())) {
legalMoves.get(y).set(x, true);
}
}
}
}
return legalMoves;
return legalMoves;
}
public boolean isKingInCheck(ArrayList<ArrayList<Piece>> board, boolean isWhiteTurn) {
int kingX = -1, kingY = -1;
int kingX = -1, kingY = -1;
// Find the king
for (ArrayList<Piece> row : board) {
for (Piece p : row) {
if (p != null && p.isWhite() == isWhiteTurn && p.getType() == PieceType.King) {
kingX = p.getX();
kingY = p.getY();
break;
}
}
}
// Find the king
for (ArrayList<Piece> row : board) {
for (Piece p : row) {
if (p != null && p.isWhite() == isWhiteTurn && p.getType() == PieceType.King) {
kingX = p.getX();
kingY = p.getY();
break;
}
}
}
if (kingX == -1 || kingY == -1) {
return true; // King not found, technically in check
}
if (kingX == -1 || kingY == -1) {
return true; // King not found, technically in check
}
// Check if any enemy piece can capture the king
for (ArrayList<Piece> row : board) {
for (Piece p : row) {
if (p != null && p.isWhite() != isWhiteTurn) {
ArrayList<ArrayList<Boolean>> moves = p.getPossibleMoves(board);
if (moves.get(kingY).get(kingX)) {
}
}
}
}
return false; // King is safe
// Check if any enemy piece can capture the king
for (ArrayList<Piece> row : board) {
for (Piece p : row) {
if (p != null && p.isWhite() != isWhiteTurn) {
ArrayList<ArrayList<Boolean>> moves = p.getPossibleMoves(board);
if (moves.get(kingY).get(kingX)) {
return true;
}
}
}
}
return false; // King is safe
}
private boolean isMoveSafe(ArrayList<ArrayList<Piece>> board, Move move, boolean isWhiteTurn) {
ArrayList<ArrayList<Piece>> simulatedBoard = simulateMove(board, move);
return !isKingInCheck(simulatedBoard, isWhiteTurn);
ArrayList<ArrayList<Piece>> simulatedBoard = simulateMove(board, move);
return !isKingInCheck(simulatedBoard, isWhiteTurn);
}
private ArrayList<ArrayList<Piece>> simulateMove(ArrayList<ArrayList<Piece>> board, Move move) {
ArrayList<ArrayList<Piece>> newBoard = deepCopyBoard(board);
ArrayList<ArrayList<Piece>> newBoard = deepCopyBoard(board);
Piece piece = newBoard.get(move.fromY).get(move.fromX);
newBoard.get(move.fromY).set(move.fromX, null);
piece.x = move.toX;
piece.y = move.toY;
newBoard.get(move.toY).set(move.toX, piece);
Piece piece = newBoard.get(move.fromY).get(move.fromX);
newBoard.get(move.fromY).set(move.fromX, null);
piece.x = move.toX;
piece.y = move.toY;
newBoard.get(move.toY).set(move.toX, piece);
return newBoard;
}
return newBoard;
}
private ArrayList<ArrayList<Piece>> deepCopyBoard(ArrayList<ArrayList<Piece>> original) {
ArrayList<ArrayList<Piece>> copy = new ArrayList<>();
for (ArrayList<Piece> row : original) {
ArrayList<Piece> newRow = new ArrayList<>();
for (Piece p : row) {
if (p != null) {
newRow.add(PieceCreation.createPiece(p.getX(), p.getY(), p.getType(), p.isWhite()));
} else {
newRow.add(null);
}
}
copy.add(newRow);
}
return copy;
}
ArrayList<ArrayList<Piece>> copy = new ArrayList<>();
for (ArrayList<Piece> row : original) {
ArrayList<Piece> newRow = new ArrayList<>();
for (Piece p : row) {
if (p != null) {
newRow.add(PieceCreation.createPiece(p.getX(), p.getY(), p.getType(), p.isWhite()));
} else {
newRow.add(null);
}
}
copy.add(newRow);
}
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;
}
}